0
while (true) {
  if (memberName == subMemberGroup.indexOf("Leonardo", "Catherine", "Luther", "Bruce", "Amy") !== -1) {
    console.log("\nMember's name exists in database. Please enter a new name.");
    var memberName = input.question("Please enter member's name: ");
  } else break;
}

I am trying to loop through to see if "memberName" that the user has input matches any of the following strings in the "subMemberGroup" array.

However, when I tried using while loop, it keeps saying that Member's name exist even thought the "memberName" that the user input does not match the strings in "subMemberGroup" array. I tried using if statement with a break but it still does not work.

The reason why I used while loop is because I want my code to re-prompt the user the enter a new name if the "memberName" matches the strings in "subMemberGroup" array.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What is `input.question()`? – Barmar Jul 13 '22 at 20:07
  • input.question is basically just prompting the user a question –  Jul 13 '22 at 20:08
  • 1
    `if (memberName == subMemberGroup.indexOf("Leonardo", "Catherine", "Luther", "Bruce", "Amy") !== -1)` makes no sense. – Barmar Jul 13 '22 at 20:08
  • the reason why I did that was so that I am able to check if the user input matches any of the strings in the array –  Jul 13 '22 at 20:09
  • `memberName == something` returns `true` or `false`. Why are you comparing that with `-1`? – Barmar Jul 13 '22 at 20:09
  • If you want to know if a string is in an array, use `array.includes(string)` – Barmar Jul 13 '22 at 20:10
  • `indexOf()` only takes one argument, why are you calling it with 5 names? What is `subMemberGroup`? – Barmar Jul 13 '22 at 20:11
  • `while (true) { memberName = input.question(...); if (subMemberGroup.includes(memberName)){ display error; } else { break }}` – Barmar Jul 13 '22 at 20:12
  • subMemberGroup contains the arrays ["Leonardo", "Catherine", "Luther", "Bruce", "Amy"]. So i am comparing if the user's input matches any of the string in the array –  Jul 13 '22 at 20:12
  • Why do you think you need to put the contents of the array in the arguments? They're already in the array. The argument should be what you're testing. – Barmar Jul 13 '22 at 20:13
  • memberName contains the user's input. from there, I would like to compare if memberName matches any of the strings in subMemberGroup array. That was my logic behind my code above. Sorry if it was confusing. –  Jul 13 '22 at 20:15
  • The error im getting is that even though memberName does not equals to any of the strings in subMemberGroup array, it still prompts that the name has existed which makes no sense. –  Jul 13 '22 at 20:16
  • I can't reproduce: https://jsfiddle.net/barmar/qkvg6dx8/1/ – Barmar Jul 13 '22 at 20:21
  • Well I just want to know why the while loop doesn't break when the if condition is false. –  Jul 13 '22 at 20:23
  • from my code above, it doesnt make sense if I put john and it still goes through the loop even though its false –  Jul 13 '22 at 20:24
  • Did you use my condition? Your condition is simply wrong. – Barmar Jul 13 '22 at 20:27
  • I tried using your condition but im not sure why is it wrong still. while (true) { if (subMemberGroup.includes(memberName)) { console.log ("\nMember's name exists in database. Please enter a new name."); var memberName = input.question("Please enter member's name: "); } else break; } –  Jul 13 '22 at 20:32
  • You're asking for input *after* the `if` check. You need to get it first. See my jsfiddle. – Barmar Jul 13 '22 at 20:40
  • is there another way of doing it? –  Jul 13 '22 at 20:40
  • I tried your coding, but it doesnt give me the right output –  Jul 13 '22 at 20:41
  • 1
    Did you try my fiddle? If you enter `Luther` it repeats. If you enter `Joe` it stops. What else do you expect? – Barmar Jul 13 '22 at 20:43
  • Yes, I tried your fiddle. But when I applied it to my viual studio code, it doesnt give me the same result compared to when im using the fiddle. –  Jul 13 '22 at 20:46
  • Is there a way that would make it work in visual studio code? –  Jul 13 '22 at 20:48

0 Answers0