Posible error in: "Basic JavaScript: Profile Lookup"

  • The first three pass with the if (so it’s correct)
  • After then if do not pass the first “if” continue with the other “else if” (so if the first “if” is true do not continue)

If i “write” only one if “the first three test pass”, but if I add "else if " only the last test pass, although I think that is correct my answer,

Why do you think there is an error? Please explain what you mean.

There are some errors in your code.

I edit the post, thanks for the response.

You have code of the form

for (let i = 0; i < bound; i++) {
  if (condition1) {
    return "something";
  } else if (condition2) {
    return "something else";
  } else if (condition3) {
    return "something other else";
  }
}

As soon as you meet one of the three conditions, you return from the function and stop executing the loop. This is causing your loop to behave differently that you expect.

So, the first test:
1:

  • It comply the first “if”, so the function return result and stop the function, so do not have to continue with the others (because are “else if”).

In the fourth test:
4:

  • Do not comply the first if so it continues
  • It comply the second “else if” (no such contact), so in return “No such contact”, and stop the function.

It works this way, doesn’t it?

Thanks for the help.

Right, but that means that you return “No such contact” the first time you find a contact that does not have a matching name, which means that you have not checked the full contact list with your for loop.

1 Like

It sounds logic, thanks for the help I will check it.

1 Like

It worked, thanks for the help

1 Like