0

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')
    }
  }
}
SaganRitual
  • 3,143
  • 2
  • 24
  • 40
brenna282
  • 13
  • 3
  • Related: [How to break nested loops in JavaScript?](https://stackoverflow.com/questions/1564818/how-to-break-nested-loops-in-javascript) – Jonathan Lonowski Mar 23 '18 at 06:10

3 Answers3

2

break just breaks the while loop. But after it for loop will again work it's next iteration.

You can add more condition in the for loop - i < 10 && answer !== 'quit'

let numSpelling = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"];
let num, answer;

alert('If you want to exit the game, just enter the word quit');

for(let i = 0; i < 10 && answer !== 'quit'; 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')
       }
    }
}
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
  • I have to have a for loop, it's for a project and it specifically needs the game to run 10 times, unless the user enters 'quit', in which the entire game will end. – brenna282 Mar 23 '18 at 06:11
  • Thank you! This makes more sense. I am not too familiar with coding yet, I've only been doing it for a few weeks, so thanks for the help! – brenna282 Mar 23 '18 at 06:22
2

May tag your outer for loop:

  main: for(i = 0; i < 10; i++){

And then you can exit it with

  break main;

Docs

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
1

You are only breaking out of your while loop and not from your for loop

let quit = false;
while(true) {
   if(answer == 'quit'){
    alert('Goodbye');
    quit = true;
    break;
   }else if(answer == numSpelling[num]){
    alert('Correct');
    break;
   } else {
    alert('Incorrect');
    answer = prompt('Please try to spell ' + num + ' again')
   }
} 
if (quit) { break; }
Prasanna
  • 4,125
  • 18
  • 41