+ 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'); }

8th Nov 2018, 1:03 AM
Markie Alicia
Markie Alicia - avatar
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'); }
8th Nov 2018, 1:16 AM
CalviŐ˛
CalviŐ˛ - avatar
+ 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
8th Nov 2018, 1:53 AM
David Carroll
David Carroll - avatar
+ 2
if(answer === “Joan of Arc” || answer === “Joan”){}
8th Nov 2018, 2:57 AM
AquA217
AquA217 - avatar