var g= prompt ("Is M1 greater than M2?"); if g= ("Yes") { alert("M1 wins!"); } else { alert ("M2 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

var g= prompt ("Is M1 greater than M2?"); if g= ("Yes") { alert("M1 wins!"); } else { alert ("M2

This code isn't working. Can anyone tell me why? I can rework it to get the same result another way but I would like to know why this particular method isn't working. Thanks.

6th Sep 2019, 6:50 PM
Inventorian
2 Answers
+ 1
Two things are wrong. Firstly, if statements have to be in parenthesis. Secondly, you have a single equal sign. That is the assignment operator, to give something a value. You have to compare the values using a double equal sign. The if statement becomes: if (g == "Yes") { alert("M1 wins!); } Edit: Typically in JavaScript it is better to use a triple equal sign to avoid conversions. So it would be: if (g === " Yes")
6th Sep 2019, 6:57 PM
Daniel C
Daniel C - avatar
0
Thanks, it worked.
6th Sep 2019, 7:06 PM
Inventorian