I have been working on a small game where you ask people to spell a random number from 1-10.
I've done this by using an if/else
statement. I've tried to add a feature where if the user enters the word 'quit', the game will display an alert saying 'Goodbye' and the game will break. But when I test it, and enter the word 'quit', it displays an alert saying 'Goodbye', but it will not break.
I've tried to figure out the solution, but I just can't figure it out. Any helpful hints?
P.S I'm new to coding, please do not judge my lack of knowledge.
var numSpelling = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
var num;
var answer;
var i;
alert('If you want to exit the game, just enter the word quit');
for (i = 0; i < 10; i++) {
num = Math.floor(Math.random() * 10) + 1;
answer = prompt('What is the spelling for the number ' + num);
while (true) {
if (answer == 'quit') {
alert('Goodbye');
break;
} else if (answer == numSpelling[num]) {
alert('Correct');
break;
} else {
alert('Incorrect');
answer = prompt('Please try to spell ' + num + ' again')
}
}
}