toni
toni

Reputation: 17

My javascript loop/ while statement code doesn't work, please help me identify my problem

So apparently according to my teacher I need to refine my syntax and logic. It doesn't make any sense (my code and my thoughts about this whole javascript thing)

var die = Math.random() * 6;        // generate a random number

die = Math.ceil( die );     // round it to between 1 and 6

var counter = 1;

var x = prompt( "What is your first guess?"); // fill x variable with the guess.  Then compare the value in die to the value in x.  What would that code be?


        while (counter < 3)
        if (x==die)
        {alert "Well done you win!"}
        break;
        }
        else {
        prompt ("Sorry.  Not the right answer.  Try again");
        counter == counter+1;
        }

var counter = counter+1;

var y = prompt("What is your second guess?"); // second x value

        while (counter ==2)
        if (y==die)
        {alert "Well done"}
        break;
        }
        else{
        prompt ("Sorry, you were wrong. Last try.");
        counter == counter+1;
        }

var counter = counter+1;


        var z = prompt("What is your third guess?"); // third x value

        while (counter == 3)
        if (z==die)
        {alert "Well done"}
        break;
        }
        else{
        prompt ("Sorry, you lost.");
        break;
        }

Upvotes: 0

Views: 67

Answers (1)

Maheer Ali
Maheer Ali

Reputation: 36574

The mistake is probably in the line

counter == counter+1;

It should an assignment expression

counter = counter + 1;

Using while loop

A good way to do that is store the strings in a array and then use them in single while loop.

let arr = ["first","second","third"];
let die = Math.ceil(Math.random() * 6);
console.log(die);
let counter = 0;
while(counter < 3){
  let input = prompt("What is your " + arr[counter] + " guess?");
  if(+input === die){ // +input converts input into a number here
    alert("You win");
    break;
  }
  else{
    alert("Sorry try again");
  }
  counter++;
}

Using for loop

I would do it using for loop. Because the counter variable is increasing one by one each time.

let arr = ["first","second","third"];
let die = Math.ceil(Math.random() * 6);
console.log(die);

for(let i = 0;i<3;i++){
  let input = prompt("What is your " + arr[i] + " guess?");
  if(+input === die){ // +input converts input into a number here
    alert("You win");
    break;
  }
  alert("Sorry try again");
}

Using for..of

We already have an array of length 3 ["first","second","third"]. So we don't need to create a variable for loop and then use it. We can clean up the code the using for..of. I also used template string for a perfect solution

let arr = ["first","second","third"];
let die = Math.ceil(Math.random() * 6);
console.log(die);

for(let x of arr){
  let input = prompt(`What is your ${x} guess?`);
  if(+input === die){ // +input converts input into a number here
    alert("You win");
    break;
  }
  alert("Sorry try again");
}

Upvotes: 1

Related Questions