0

I am trying to make this Guessing Game. Below is the repo on Github. https://github.com/christsapphire/Guessing-Game

I wrote the logic in guessing-game.js and it worked fine. Which I am using userGuess = prompt("Guess a number"). However, if I click cancel on this prompt, it will keep asking (maybe thats why??).

I tried to translate this to jQuery, using userGuess = $('#input').val(); and it encountered a bug. The webpage crashed after I click the "Submit" button.

I want when a user click "Submit" button on the webpage, it runs this function below.

function checkUserInput() {

    for (var valid = false; !valid;) {

        //I suspect these two  below
        // userGuess = parseInt($('#submit').val(), 10)
        //userGuess = parseInt(prompt("Guess a number"), 10);
       //-----------

        if ((userGuess >= 1) && (userGuess <= 100)) {
            if (repeatAnswer(userGuess, userGuessHistory)) {
                $('#status').text("You chose that number before! Choose another number!");
            } else {
                valid = true;

                userGuessHistory.push(userGuess);
                return true;
            }
        } else {
            $('#status').text("That number is invalid! Please enter a number between 1-100");
        }
    }

}

I think when I enable userGuess = $('#submit').val() it is repeatedly trying to take an input value from the Input html element, so it crashed.

Please help! Thanks before

3
  • 1
    Describe what exactly you mean by "crash", and provide the full text of any error messages that occur. Commented May 18, 2014 at 1:34
  • 1
    I want when a user click "Submit" button on the webpage, it runs this function below. Have you tried $("#sumbit").click(checkUserInput); ? Commented May 18, 2014 at 1:37
  • There was no error messages, just crashed like that. Yes, I tried that, it still didn't work. Maybe it has something to do with my FOR loop Commented May 18, 2014 at 1:40

1 Answer 1

1

It looks like you are really wanting to get the value of the input, not the submit button:

$("#input").val();

Are you trying to attach your function to the submit button? That would be:

$("#submit").click(function(){
     CheckUserInput();
});
Sign up to request clarification or add additional context in comments.

1 Comment

No, thats not what I am trying to do. Yes I already tried that, I want to attach that function to the submit button just like the way you suggested.

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.