Javascript condition

Asked

Viewed 79 times

0

I started studying javascript recently and the following code is only giving me the wrong answer, where I went wrong?

When I play at the prompt to see if it’s right, it gives me "n1 is odd"

// Verifique se um número é par ou ímpar
var n1 = 4;
var sobra = n1%2;

if(sobra = 0){
    console.log("n1 é par")
}else{
    console.log("n1 é ímpar")
};

  • good morning, avoid is posting photo, it is better to post the same code, copy the paste code for a better view, so someone denied your question.

  • and the error is quite simple, you are using the assignment operator and not comparison, the correct serious == and not =

  • ai I did not know apokdaposkdapo, but brigade by tip

1 answer

2


Hello, the error is quite simple, you are using the assignment operator =, the correct serious the comparison operator ==.


Documentation for more information on operators.

var n1 = 4;

var  sobra = n1%2;

if (sobra == 0) {
    console.log('n1 é par');
} else {
    console.log('n1 é impar');
}

Another way to get serious like this:

var n1 = 4;

if ((n1 % 2) == 0) { 
    console.log('n1 é par');
} else {
  console.log('n1 é impar');
}

With ternary operator, more information in the documentation:

var n1 = 4;
(((n1 % 2) == 0) ? console.log('n1 é par') : console.log('n1 é impar'));
  • 1

    Our, young brigade <3

  • @Miyukifukagawa if you mark it as the correct answer/ ^^

  • 1

    wow, I just joined this site and I don’t know how to touch anything here, but I think I marked it as the right answer, thanks again <3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.