1

I am trying to make a simple JavaScript guessing game, and my for loop keeps getting skipped! Here is the part of my code that is getting skipped:

for (i = 0; i === tries; i += 1) {
    isSkipped = false;
    var guessedNumber = prompt("Guess your number now.");

    console.log("User guessed number " + guessedNumber);

    //check if number is correct
    if (guessedNumber === numberToGuess) {
        confirm("Hooray, you have guessed the number!");
        break;
    } else if (guessedNumber > numberToGuess) {
        confirm("A little too high...");
    } else {
        confirm("A little too low...");
    }
}

and here is the full code:

//declaring variables
var numberToGuess;
var tries;
var i;
var isSkipped = true;

var confirmPlay = confirm("Are you ready to play lobuo's guessing game? The number for you to guess will be a number ranging from 1 to 25."); //does the user want to play?

if (confirmPlay === true) {
    console.log("User wants to play");
} else {
    window.location = "http://lobuo.github.io/pages/experiments.html";
} //if user wants to play, let them play, else go to website homepage

numberToGuess = Math.floor((Math.random() * 25) + 1); //sets computer-generated number

tries = prompt("How many tries would you like?"); //gets amount of tries
tries = Math.floor(tries); //converts amount of tries to integer from string

for (i = 0; i === tries; i += 1) {
    isSkipped = false;
    var guessedNumber = prompt("Guess your number now.");

    console.log("User guessed number " + guessedNumber);

    //check if number is correct
    if (guessedNumber === numberToGuess) {
        confirm("Hooray, you have guessed the number!");
        break;
    } else if (guessedNumber > numberToGuess) {
        confirm("A little too high...");
    } else {
        confirm("A little too low...");
    }
}

if (isSkipped === true) {
    console.log("Oh no! The for loop has been skipped!");
}

If you need any further details, just ask.

3
  • 4
    What is this for i += 0??? Commented Feb 23, 2014 at 6:52
  • oops... I fixed that, but still nothing... It was supposed to be i += 1 Commented Feb 23, 2014 at 6:54
  • Simply use i++; besides i === tries is not well iterated! Commented Feb 23, 2014 at 6:55

3 Answers 3

2

Shouldn't the for be like this?:

for (i = 0; i < tries; i += 1) {
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to explain what's the difference between the two, and provide the edited code.
oops... didnt notice you did i < tries. That fixed it. Thanks :D
0

When you write:

for (i = 0; i === tries; i += 0) {

the loop repeats as long as the condition i === tries is true. If tries is 3, for instance, this condition is not true on the first iteration, and the loop ends immediately.

You should write:

for (i = 0; i < tries; i++) {

Comments

0

Also you need to use parseInt() function on user's input.

 var guessedNumber = parseInt(prompt("Guess your number now."), 10);

instead of

var guessedNumber = prompt("Guess your number now.");

5 Comments

why downvote as he checked for types also guessedNumber === numberToGuess?
@SumanBogati Because the type does not matter since it would be a Number no matter what after Math.floor.
@Derek朕會功夫: the OP doesn't do Math.floor on guessedNumber, but he does so on tries. Because guessedNumber is a string while numberToGuess is a number, it will never successfully complete.
@Qantas94Heavy - Oh I see it now. One more reason to use == it seems!
yeah how can be succeed 'string' === number.

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.