Reputation: 979
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
Upvotes: 0
Views: 54
Reputation: 11
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();
});
Upvotes: 1