skip to Main Content

I am new to coding and have been set a task using a do while and prompt. I have created the code below. If I put anything other than Gryffindor it states ‘That is not the house for you, try again!’ so when I try again it, instead of repeating that, it returns to the original prompt of asking which house they will be in. Could someone help me correct it? Thank you.

var house;

do {house = prompt("With which house will you be placed? Please enter Gryffindor, Ravenclaw, Hufflepuff, or Slytherine!");
if (prompt == 'Hufflepuff' || 'Ravenclaw' || 'Gryffindor');
    house = prompt('That is not the house for you, try again');
} while (house != 'Gryffindor'); 
house = alert ("Yes that is the house for you!")

I want it to keep repeating ‘That is not the house for you, try again’ until the user types Gryffindor rather than go back to "With which house will you be placed? Please enter Gryffindor, Ravenclaw, Hufflepuff, or Slytherine!"

This will be an easy fix for someone and I am still working on it myself with tutorials etc. but I am new to coding so any help you could give would be great.

Thanks

3

Answers


  1. You could make use of an infinite loop while(true) and break if the right answer is given. break will exit out of the nearest loop.

    do {
      const house = prompt("With which house will you be placed? Please enter Gryffindor, Ravenclaw, Hufflepuff, or Slytherine!");
      if(house == 'Gryffindor')
      {
        break;
      }
      alert('That is not the house for you, try again');
    } while (true);
    alert("Yes that is the house for you!")

    However this lacks the clarity of one of the other answers. Leaving this here as it’s a natural progression from your code to something that works – but if you want the better code go with the other answer!

    Login or Signup to reply.
  2. A simpler way would be to not use the do ... while loop. It’s a bit of a confusing control flow to use anyway, so this is a lot easier: just use a while loop itself!

    let house = prompt( "With which house will you be placed? Please enter Gryffindor, Ravenclaw, Hufflepuff, or Slytherine!");
     
    // Just repeat the question until your value is equal to Gryffindor
    while( house !== "Gryffindor" ) house = prompt( "That is not the house for you, try again" );
    
    alert( "Yes that is the house for you!" )

    If you insist on a do ... while loop (note: not recommended), the solution is to not have an if condition – your while is essentially that. As long as the code in your while condition evaluates to true, it will do another round of do:

    let house = prompt( "With which house will you be placed? Please enter Gryffindor, Ravenclaw, Hufflepuff, or Slytherine!");
     
    // It's best _not_ to go to your do..while at all if your condition is met
    // The issue, obviously, is that you have to repeat the condition twice!
    // That's clearly against the "Don't Repeat Yourself" principle.
    
    if( house !== "Gryffindor" ) do {
       house = prompt( "That is not the house for you, try again" );
    } while( house !== "Gryffindor" )
    
    alert( "Yes that is the house for you!" )

    Why is it not recommended? Because a do ... while loop is pretty confusing, your condition is only listed after a lot of code, inverse to how it is in most conditions (like if or switch). I have never seen a do ... while code in production anywhere, and it’s best to keep it that way.

    Login or Signup to reply.
  3. if (prompt == 'Hufflepuff' || 'Ravenclaw' || 'Gryffindor')
    

    If you want to compare variable to different values you will need to repeat the comparison like

    if (prompt == 'Hufflepuff' || prompt == 'Ravenclaw' || prompt == 'Gryffindor')
    

    Alternatively, you can use a switch statement like

    switch (prompt) {
        case 'Hufflepuff':
        case 'Ravenclaw':
        case 'Gryffindor':
            // prompt is either Hufflepuff | Ravenclaw | Gryffindor
            break;
        default:
            // else
            break;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search