samedi 8 mai 2021

index === arr.length - 1 returns false every time

I have a function resFormat(restrictions) which takes an array of restrictions as a parameter and returns a string which contains diet restrictions. Eg:- [res1,res2,res3] becomes res1, res2, res3

    resFormat(restrictions){
        let result="",i
        for (i in restrictions){
            if(i===restrictions.length-1){ //always returns false 
                result += restrictions[i]
                continue
            }          
            result += restrictions[i]+", "
        }
        return result
    }
Here i===restrictions.length-1 should return true when the loop reaches the last element of the array but it always returns false.

Expected output: res1, res2, res3

Actual output res1, res2, res3,

Aucun commentaire:

Enregistrer un commentaire