1

This code is returning an error and the 2nd 'else if' statement:

   function likes(names) {

  if (names.length == 0) {
    return "no one likes this"
  } else if (names.length == 1) {
    return names[0] + " likes this"
  };

  // the above is running fine if I remove the second to else if statements. 

  else if (names.length == 2) {
    return names[0] + " and " /*error starts here*/ + names[0] + " like this"
  };
  else if (names.length == 3) {
    return names[0] + ", " + names[1] + " and  " + names[2] + " like this"
  };
  else if (names.lenght > 3) {
    return names[0] + ", " + names[1] + " and  " + names.length - 1 + "others like this};
  }
  console.log(likes(["james", "pete"]))

I guess it's a problem with how I am joining my strings together but I can't seem to figure it out.

Sorry, still learning. Appreciate any feedback.

2
  • 1
    remove ; after if and else-if statements Commented May 3, 2020 at 7:54
  • try using 10bestdesign.com/dirtymarkup/js to validate you JS code Commented May 3, 2020 at 7:55

2 Answers 2

1

You are adding ; after every else if which is breaking the code. Also in this line there is no closing "

return names[0] + ", " + names[1] + " and  " + names.length - 1 + "others like this}

function likes(names) {

  if (names.length == 0) {
    return "no one likes this"
  } else if (names.length == 1) {
    return names[0] + " likes this"
  }

  // the above is running fine if I remove the second to else if statements. 
  else if (names.length == 2) {
    return names[0] + " and " /*error starts here*/ + names[0] + " like this"
  } else if (names.length == 3) {
    return names[0] + ", " + names[1] + " and  " + names[2] + " like this"
  } else if (names.lenght > 3) {
    return names[0] + ", " + names[1] + " and  " + names.length - 1 + "others like this"
  }
}

console.log(likes(["james", "pete"]))

Sign up to request clarification or add additional context in comments.

Comments

0

Problems:

  1. unwanted semicolons in end of each if statement };
  2. And last statement not closing properly with "

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 " /*error starts here*/ + names[0] + " like this"
  } else if (names.length == 3) {
    return names[0] + ", " + names[1] + " and  " + names[2] + " like this"
  } else if (names.lenght > 3) {
    return names[0] + ", " + names[1] + " and  " + (names.length - 1) + "others like this"
  }
}
console.log(likes(["james", "pete"]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.