0

The purpose of the code is to allow users to play the game as many times as they want. I keep on getting stuck in infinite loop and I am not sure why. P.s. I need to stick to switch statements.

    var color1 = prompt('Pick a color');

    while (true){

    switch (color1) {
        case (color1 = 'blue'):
            document.write("nope");
            break;
       case (color1 = 'yellow'):
            document.write("nope");
            break;
        case (color1 = 'white'):
            document.write("nope");
            break;
       case (color1 = 'gray'):
            document.write("nope");
            break;
        case (color1 = 'green'):
            document.write("yes");
            break;
        case (color1 = 'pink'):
            document.write("nope");
            break;
        case(color1 = 'purple'):
            document.write("nope");
            break;
        case (color1 = 'orange'):
            document.write("nope");
            break;
       case (color1 = 'green'):
            document.write("nope");
            break;
        case (color1 = 'magenta'):
            document.write("nope");
            break;
       case (color1 = 'red'):
            document.write("nope");
            break;
    } 

    if(color1 = false)
    alert('Thanks')

    }
2
  • All the logic for the game needs to be inside the loop if you intend for them to play again. Also, giving them a way to opt out of playing again would be helpful. Commented Oct 7, 2017 at 2:51
  • Also you should use double equals == to test for equality instead of assignment =. So color1 =... should be color1 ==... Commented Oct 7, 2017 at 2:52

1 Answer 1

3

There are several problems in your code:

  • The = operator does assignment, you need === or == for comparison.

  • You are not using case correctly within your switch: you need to just put the value you want to match against color1, don't try to do a comparison as if it were an if condition. So you need something like:

    switch (color1) {
        case 'red':
            document.write("nope");
            break;
    
  • It doesn't make sense to list a whole bunch of different incorrect colours in different cases, because even aside from the fact that the user might enter values not in your list, really the logic should be "Is it green?" yes/no. An if statement would make much more sense than a switch, but since you've said you have to use switch then you should have one case for the correct answer and then use default to catch all other values:

    switch (color1) {
        case 'green':
            document.write("yes");
            break;
        default:
            document.write("nope");
            break;
    }
    

    Or in a hypothetical case where you legitimately needed to list several values but have them do the same thing you should make use of a "fall through" like this:

    switch (color1) {
        case 'blue':
        case 'yellow':
        case 'white':
            document.write("nope");
            break;
        case 'green':
            document.write("yes");
            break;
    }
    
  • The final if needs to break out of the while loop when the condition is true - currently all it does is show an alert, hence the infinite loop.

  • Testing if (color1 === false) (after fixing the operator) doesn't make sense, because color1 won't ever be false: if the user clicks the Cancel button on the prompt() then the value will be null, so test for that. You could also test for an empty string. Except you can move that logic into a case instead of having an if after the switch statement. Instead of while(true), use while(!finished) and add a finished variable that you set to true when the user clicks the Cancel button.

  • The prompt() needs to be inside the loop, otherwise the user will only be prompted once before the loop starts and the loop will keep repeating testing the same value over and over.

  • Using document.write() isn't a good plan, but I'm declaring that issue out of scope for this question. In the meantime, you should at least output <p> elements or something so that each "nope" and "yes" appears on its own line.

Putting that all together:

var finished = false;

while (!finished) {
  var color1 = prompt('Pick a color');

  switch (color1) {
    case null:
    case '':
      alert('Thanks');
      finished = true;
      break;
    case 'green':
      document.write("<p>yes</p>");
      break;
    default:
      document.write("<p>nope</p>");
      break;
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.