nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.48k stars 281 forks source link

Conditional Statement: Using AND and OR in node js if statement #3300

Closed chuksgpfr closed 3 years ago

chuksgpfr commented 3 years ago

I need to understand why this doesn't work

if(x%2 === 0 && y%2 === 0){
 ...
}

But this works.

if((x%2 === 0) && (y%2 === 0)){
 ...
}

The first works in languages like java and c#. If this needs to be worked on, I'd LOVE to CONTRIBUTE to it.

Ayase-252 commented 3 years ago

Hello,

I see the first example works

let x = 2
let y = 2
if (x % 2 === 0 && y % 2 === 0) {
  console.log("both are even")
}
node index.js 
both are even

What do you want to do? Could you please provide your goal?

chuksgpfr commented 3 years ago

I was solving an algorithm question where i was suppose to

  1. print fizz if the number passed into is a multiple of 3 and not 5
  2. prints fizzbuzz if the number is a multiple of 3 and 5
  3. prints buzz if it's a multiple of 5 and not 3
Ayase-252 commented 3 years ago

Just solve your first question

function printFizz(num) {
  if(num % 3 === 0 && num % 5 !== 0) {
    console.log('fizz')
  }
}

printFizz(3) // fizz
printFizz(4) // nothing to print
printFizz(15) // nothing to print

You can take this as a propotype to explore another questions