So im trying to make a calculator to get into an IT school. But I cant seem to figure out why I get NaN as my answer.
What im supposed to make is a calculator with a feat, the feat being that you can also guess what the answer’ll be. Im stuck trying to figure out why I keep getting NaN as an answer to all my multiplications.
The reason why there is 2 number3’s in my var: product. that is because I found out I needed to have it in the multiplication to do the if statement. number3 is the one you guess withthis is how it looks, i translated the image if anyone was wondering
This is really bugging me since I like doing this but I cant seem to figure it out.
function CalculateProduct() {
var product = parseInt(number1) * parseInt(number2) * parseInt(number3) / parseInt(number3)
var number1 = document.getElementById("number1").value;
var nummer2 = document.getElementById("number2").value;
var number3 = document.getElementById("number3").value
var owncalculation= document.getElementById("owncalculation");
var uitkomst = document.getElementById("uitkomst");
if (number3 == product) {
document.getElementById("owncalculation").innerHTML="The answer you put in was correct"
} else {
document.getElementById("owncalculation").innerHTML="The answer you put in was false"
}
if (Getal3 == product) {
owncalculation.style.color = "green"
} else {
owncalculation.style.color = "red"
}
result.innerHTML = "The answer to " + Getal1 + " * " + Getal2 + " = " + product;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap demo</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<pre>
<h1>Js Rekenmachine</h1>
Voer een getal in: <input type="number" id="Getal1">
Voer een tweede getal in: <input type="number" id="Getal2">
Zelf berekende uitkomst: <input type="number" id="Getal3">
<p id="uitkomst">De uitkomst is:</p>
<span id="eigenberekening">Het door jou berekende antwoord
is:</span>
<button type="button"
class="btn btn-success btn-lg"
onclick="BerekenSom()">Bereken som</button>
<button type="button"
class="btn btn-success btn-lg"
onclick="BerekenProduct()">Bereken Product</button>
</div>
</pre>
<script src="./app.js"></script>
</body>
</html>
function BerekenProduct() {
var Getal1 = document.getElementById("Getal1").value;
var Getal2 = document.getElementById("Getal2").value;
var Getal3 = document.getElementById("Getal3").value
var product = parseInt(Getal1) * parseInt(Getal2)
document.getElementById("product").innerHTML = product
uitkomst.innerHTML = "Het product van " + Getal1 + " * " + Getal2 + " = " + product;
if (Getal3 == product) {
document.getElementById("eigenberekening").innerHTML="Het door jou berekende antwoord is goed"
} else {
document.getElementById("eigenberekening").innerHTML="Het door jou berekende antwoord is fout"
}
if (Getal3 == product) {
eigenberekening.style.color = "green"
} else {
eigenberekening.style.color = "red"
}
}
2
Answers
Make sure to create the variables before you calculate the product
There was also a typo
var number2
misspelled asvar nummer2
.Using your code
I have fixed the two errors, and marked them in the code below:
EDIT WITH A SECOND SNIPPET!
Here is your homework, I had time to loose 😉
I don’t understand dutch, so I tried my best.
If you have question, feel free to comment.
Have a nice day.
Same code with – and / operators :