Variables | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Variables

var hi = prompt("Please type in a way to greet someone"); var grt = ["Hi", "Hey", "Hello"]; if(hi == grt){ alert("Hello!"); } Above is my code. But it doesn't seem to work. What's wrong with it?

27th Nov 2017, 10:18 PM
Zakariya
Zakariya - avatar
3 Antworten
+ 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
+ 2
ok, thanks
28th Nov 2017, 6:21 AM
Zakariya
Zakariya - 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