+ 3
Hi I need help on a Javascript loop
This loop allows any anwer to be right. What do you think is wrong? Thank you so much in advance. var answer = prompt ('Who is the most famous medieval female figure in European history?'); if( answer === 'Joan of Arc' || 'Joan') {document.write("That is Right!"); } else{ prompt ('Her name starts with J and she has Arc in it'); }
3 Answers
+ 4
Try this:
var answer = prompt ('Who is the most famous medieval female figure in European history?'); if( answer === 'Joan of Arc' || answer === 'Joan') {document.write("That is Right!"); } else{ prompt ('Her name starts with J and she has Arc in it'); }
+ 5
Markie Alicia Your code will always indicate a correct answer because the second expression always evaluates as true.
Consider the difference between
if ( 'Joan' ) {... }
... and...
if ( answer === 'Joan' ) {... }
The first expression evaluates as a truthy value because it's not an empty string ( "" ), null, false, or undefined.
The second expression evaluates to true only when the variable { answer } is equal to 'Joan', as is demonstrated in the code by CalviŐ˛.
BTW... It looks like you accidentally posted the same question twice. Please delete the other question.
https://www.sololearn.com/Discuss/1578287/?ref=app
+ 2
if(answer === âJoan of Arcâ || answer === âJoanâ){}