2
votes

What I'm building is a game where the computer generates a random number (1-100) and the user must guess the correct number. The goal is for the computer to compare the current guess to the previous guess and spit out a statement: "hot", "cold", "hotter", "colder", etc.

My Code (focus on the JS): CodePen fiddle

//global variables--computer generated guess, and guess log 
var answer = Math.floor((Math.random() * 100)+1); 
var guessArray = []; 
var index = 0; 

//user clicks submit button and guess is registered by computer
$("#submit").click( function(){
    var guess = $("#guess").val(); 

    guessArray.push(guess);
    //prints out the answer and user guesses
    $("#answer").text("Answer:" + " "+ answer);
    $("#guessArrayPrint").text("You guessed: " + " " + guessArray + " ");

    if (answer === guess) {
        $("#statement").text("woo hoo right answer");
    } else {
        var currentDifference = Math.abs(answer-guess);
        var currentDiffArray = [];

        currentDiffArray.push(currentDifference); 

        if (index = 0)  {   
            //if-else statement comparing current guess range to answer
            if ( currentDifference >=1 && currentDifference <= 10){
                $("#statement").text("Ouch! You're hot!");
            } else {
                $("#statement").text("Brr! You're cold!");
            }
        } else { 
            //if-else statement comparing current guess to previous guess
            var previousDiff = answer- prevguess;
            var prevguess = guessArray [i-1];

            if( previousDiff < currentDifference){
                $("#statement").text("Ahh! Getting Warmer!");
            } else {
                $("#statement").text("Brrr...getting colder");
            }
        }
        index++
    }
});

My nested if-else statements are not working. When a user inputs a guess, no matter how close to the answer, it always returns the statement "brr.. getting colder", which is in the "else" section.

Ideally when the user inputs their first guess if (index = 0) should run then when the second guess is input, it should move to the "else" statement with the previous guess variables. I tried moving around the variables, changed orders of if/else, and thought maybe it's the placement of index++. Nothing is working. Not sure if something is wrong with my variables , arrays, or the syntax of my if/else statements.

tl;dr: when the program is run only the "else" portion of the nested if-else statement is run. Not sure how to fix… I've gone through my code a number of times. The syntax, the arrays, and variables. Uncertain what's wrong.

1
"200" !== 200. You need to parseInt(guess, 10) or var guess = parseInt($("#guess").val(), 10);.tenub
if (index = 0) { should be if(index==0){ltalhouarne

1 Answers

1
votes

You JS has if (index = 0). This should be if (index === 0).

Additionally, you need to cast the value of your input field to a number. You can do this using:

var guess = +$("#guess").val(); // + cast as a number

More syntax errors:

prevguess = guessArray[i - 1] --> prevguess = guessArray[index - 1];

Here is a partial working Fiddle. I ran through some scenarios, and the fiddle really only works if you give the application the right answer. The code has many syntax errors, bad refs and calculations. I would suggest opening the console or a debugger, identifying those issue, and fixing them.

Here is a Fully Functional Demo.