I would just like you to know that I am a complete beginner at JavaScript.
For my program, I need to check if the user inputted the "correct" answer for the given equation (created by function newEquation()
). However, when the user inputs an actual correct answer for the equation, the if-else statment in function checkAnswer()
goes straight to the else (causing the player to lose a heart). I feel like it has to do something with me assigning the two variables, but I'm not entirely sure, it could be a different problem that I can't see. Please comment if you don't understand something in my code below. Thank you in advance.
//These are the two variables I am concerned about
var randNum1 = randomNumber(1,6);
var randNum2 = randomNumber(1,6);
//This is the function to create an equation with the two variables which is called somewhere else in my program
function newEquation() {
randNum1 = randomNumber(1,6);
randNum2 = randomNumber(1,6);
setText("equation_text", randNum1 + " + " + randNum2);
}
//This is the function where I think the problem is coming from
function checkAnswer() {
var user_answer = getNumber("answer_input");
randNum1 = randomNumber(1,6);
randNum2 = randomNumber(1,6);
if (user_answer == randNum1 + randNum2) {
showElement("coinToCollect");
hideElement("randMonster");
} else {
hideElement("heart_one");
livesValue--;
}
}
checkAnswer();
Aucun commentaire:
Enregistrer un commentaire