Who likes it? Problem ;_;

Tell us what’s happening:

Help me with this codewars problem ;_; I keep getting the following

image

Your code so far

let array1 = [‘Alex’, ‘Jacob’, ‘Mark’, ‘Max’]

function likes (names) {
if (names===0)
{
return(“noone likes this”);
}

if (names===1)
{
return(names[0]+", like this");
}
if (names===2)
{
return(names[0]+" and “+names[1]+” like this");
}
if (names===3)
{
return(name[0]+", “+names[1]+”, and “+names[2]+” like this");
}
if (names > 3)
{
return (names[0]+", “+names[1]+”, “+ names[2]+” and “+ names.length-3+ " others like this”);
}

}
console.log(array1);
console.log(likes(array1));

// Your code above this line

Link to the challenge:

You’re comparing names (the array) with numbers. Maybe you meant comparing the array’s length?

1 Like

function likes(names) {
if (names.length===0)
{
return( ‘no one likes this’);
}

if (names.length===1)
{
return(names[0]+" likes this");
}
if (names.length===2)
{
return(names[0]+" and “+names[1]+” like this");
}
if (names.length===3)
{
return(name[0]+"," + names[1]+ “, and " +names[2]+ " like this”);
}
if (names.length > 3)

{
return (names[0]+", “+names[1]+”, "+ names[2]+ "and “+ names.length-3+ " others like this”);
}

}

names= [‘Alex’, ‘Jacob’, ‘Mark’, ‘Max’]

likes(names)

You should use return like this:
function likes(names) {
if (names.length == 0) {
return “no one likes this”;
} else if (names.length == 1) {
return names[0] + " likes this";
} else if (names.length == 2) {
return names[0] + " and " + names[1] + " like this";
} else if (names.length == 3) {
return names[0] + “, " + names[1] + " and " + names[2] + " like this”;
} else {
return names[0] + “, " + names[1] + " and " + (names.length - 2) + " others like this”;
}
}

This would look cleaner with a switch statement.

No programmer would approve so many “if-else” and so much repetition.