Min Kyung Kwon
Min Kyung Kwon

Reputation: 173

Array, prompt, if, if else statement

I'm trying to create a web page as below and the code is not working. Could anyone shed some light on it?

Upvotes: 0

Views: 627

Answers (2)

Jonny
Jonny

Reputation: 83

  1. UserGuess must be userGuess. Because javascript is case sensitive.
  2. You turned the output of prompt to lower case. it's okay. but you forgot to turn the elements of the array to lower case.

Upvotes: 1

Majed Badawi
Majed Badawi

Reputation: 28424

You have a syntax error where you are using userGuess instead of UserGuess. Moreover, the purpose of using toLowerCase() is to remove the case sensitivity between the two strings when comparing, whereas what you are doing is changing the input to lower case while keeping the array elements to have some uppercase letters. Here is the solution:

var myBands = ["chromatics","acdc","michael jackson"];
var UserGuess = prompt("Who is your favourite?");
var userGuessLower = UserGuess.toLowerCase(); 
if (myBands.indexOf(userGuessLower) === -1) {
     alert("Na They're pretty lame.");
}else {
     alert("OMG I love them too!");
}

Upvotes: 1

Related Questions