0

I am trying to create a simple guessing game using prompt. I got it working, but I have one bug I have not been able to fix

In the game you are supposed to enter a maximum number that is used to produce a random number within that maximum limit, then after the random number is created the logic prompts you to enter numbers until you guess it and at the end it shows you how many attemps you had to do.

I created a function that validates if the max number is indeed a number and if not then the function calls itself asking for the max number again. If I enter a number it works fine, but if in your first try you enter a string and then when it asks again you enter the number right, it all goes bad because the value of 'max' becomes NaN and I do not why! (Be careful because it enters into a infinite loop)

I added a couple console.log so you can see value of 'max' result in the console in that scenario. (See image)

Image of max value in the console

I was reviewing this question in the link below and I think it is maybe because of that function calling itself within that function, or maybe it has to do with the let variable scope. I do not know.

JavaScript closure inside loops – simple practical example

Here is my code:

let maximun = required();
const ram = Math.floor(Math.random()*maximun+1);
// console.log(ram);
let timesGuess = 1;
let num = parseInt(prompt('Guess the number'));

while (true) {
    console.log(`i am ram${ram}`);

    if (!isNaN(num)) {
        if (num < ram) {
            num = parseInt(prompt('Too low! Guess Again'));
            timesGuess++;
        } else if (num > ram) {
            num = parseInt(prompt('Too high! Guess Again'));
            timesGuess++;
        }
    } else {
        num = parseInt(prompt('Must be a number'));
    }
    
    if (num === ram) {
        break;
    }
}

alert(`it took you ${timesGuess} guesses`);



function required(){
    let max = parseInt(prompt("Enter your maximum number"));
    if (max === " " || isNaN(max)) {
        alert("Enter a numeric value");
        required();
    } 
    console.log(`i am max${max}`);
    return max;
}

Your support is much appreciated

Thinking about the answer to this question I believe this is the logic, please plese confirm if I am warm hahaha

With the original code if I enter 4 consecutive strings and in the fifth attempt I enter the number 10, as I am not returning the function the fifth value 10 is lost withing that fifth function call and then within the first call I am left with the original value of "max" before even the second call of the function is made. Now, with your fix, since the function is always returned 10 passed from the fifth call of the function, then to the fourth, an so on until it reaches the first call, and now the value of "max" is the 10, which is returned to be stored in "maximun"

1 Answers1

1

The infinite loop is happening because you are not returning the value of the function call. You are simply calling the function. What this does is let you receive a Number on the next try but not actually set it to the variable max in the first call. That second input(the max number you enter) gets discarded when your function returns(the inner call).

Simple fix: put a return statement before the inner function call.

let maximun = required();
const ram = Math.floor(Math.random()*maximun+1);
// console.log(ram);
let timesGuess = 1;
let num = parseInt(prompt('Guess the number'));

while (true) {
    console.log(`i am ram${ram}`);

    if (!isNaN(num)) {
        if (num < ram) {
            num = parseInt(prompt('Too low! Guess Again'));
            timesGuess++;
        } else if (num > ram) {
            num = parseInt(prompt('Too high! Guess Again'));
            timesGuess++;
        }
    } else {
        num = parseInt(prompt('Must be a number'));
    }
    
    if (num === ram) {
        break;
    }
}

alert(`it took you ${timesGuess} guesses`);



function required(){
    let max = parseInt(prompt("Enter your maximum number"));
    if (max === " " || isNaN(max)) {
        alert("Enter a numeric value");
        return required();
    } 
    console.log(`i am max${max}`);
    return max;
}