mardi 11 février 2020

Counting characters of elements in an array using array[i].length

I'm working on a problem I encountered in codewars (Friend or Foe), namely I'm supposed to write a function which returns only these elements from an array which have 4 characters. However, when I run the code in the browser I get an error, which says:

VM167:7 Uncaught TypeError: Cannot read property 'length' of undefined at friend (:7:20) at :1:1

const friends = ["Tom", "Alan", "Anthony", "Mike"];

function friend(friends){
    const myFriends = [];
    var i;
    for(i=0; i<friends.length; i++){
        var numberOfLetters = friends[i];
        if(numberOfLetters.length == 4){
            myFriends.push(friends[i]);
        }
    }
    return myFriends;
}

I'm just starting the course in Javascript, could you please tell me what is this issue related to?

Aucun commentaire:

Enregistrer un commentaire