var n = prompt("Digite um numero");
var total = n/2;
if(n / 2 = 0) {
alert("Par");
}
if(n / 2 = 1){
alert("Impar");
}
alert(total);
Friend when you use:
var 'total = n/2'
you’re just dividing this value, there’s no way to know if it’s even or odd this way.
You need to use the operator %
that will give the result of the surplus of the division, and by means of this surplus it is known whether it is even or odd. Very simple neh ?
just change:
var total = n / 2;
for:
var tola = n % 2;
something else is not:
if(n/2 = 1){
}
but yes :
if(total == 0){
alert("par");
}
in short, the code can look like this:
var n = prompt("digite numero");
var resto = n % 2;
if (resto == 0) {
console.log(n + " par");
} else {
console.log(n + " impar");
}
Remember that the operator
==
compares whether the value of two objects is equal and the operator===
compares whether the value and object types are equal. For example'true' == true
results intrue
;'true' === true
returnsfalse
.– Marcus Vinicius