+ 4
You could also use indexOf to check the content of array. Return -1 if not exist, else variable exists in the array. var hi = prompt("Please type in a way to greet someone"); var grt = ["Hi", "Hey", "Hello"]; if(grt.indexOf(hi)!=-1){ alert("Hello!"); }
28th Nov 2017, 12:39 AM
Calviղ
Calviղ - avatar
0
You can't compare like that. hi must be checked against each element in the array. var hi = prompt("Please type in a way to greet someone"); var grt = ["Hi", "Hey", "Hello"]; for (var i = 0; i < grt.length; i++) { if(hi == grt[i]){ alert("Hello!"); } }
28th Nov 2017, 12:07 AM
John Wells
John Wells - avatar