简体   繁体   中英

if/else statements only logs if, not else

I am stuck on a problem for my coding homework. Here it is:

  • Write a loop that logs " Marco !" when i is even,
  • " Polo !" when i is odd.
  • Do not edit the existing code.
  • Hint 1: Use an if/else statement
  • Hint 2: Google the mod operator (%)

My attempt

let x=11;
let y=4;
let i=x%y;

  if (i) {
 console.log("Marco!")
 }

  else  {
 console.log("Polo")
 }

This logs Marco when I need it to log polo. So while I continue to try and solve this I wanted to see how experts would do it.

If you want to check if a number is even or odd, use the modulo operator ( % ), which returns the remainder of dividing one number by the other. You should reverse your logic:

 let x = 11; let y = 4; let i = x % y; if (i % 2) { console.log("Polo!"); } else { console.log("Marco!"); } console.log(i); //So you can see if the above works or not

Here's how this works:

let i = x % y;

What this does is it divides x by y (divides 11 by 4 ), and takes away the remainder - in this case the remainder would be 3 , so i = 3 .

Now, here comes the tricky bit. If you want to find out if a number is even, you can use % 2 , which is what we're doing in the if statement. If the number is even, it will return 0 as there will be no remainder from dividing by two. It's tricky, but I'll show you as best I can:

If we have 6 (which we know is even), and we test if it is even by dividing it by 2 , it should return 0 as there is no remainder:

 console.log(6 % 2);

And this is how our logic in the first snippet works, only it uses Boolean truthy and falsy values. Falsy values are:

false
0
''
""
``
null
undefined
NaN

So if i is even, the modulo will return 0 , meaning that the first if statement will not run because i % 2 will return 0 which evaluates to false , therefore the code will run console.log("Marco!") if i is even, but console.log("Polo!") if i is odd.

Further reading:

If you want to "run a loop" ... you'll need to run a loop! : )

for (var i = 0; i < 10; i++) {
  // do things!
}

These loops are ugly and scary!

But - it's really just a start { and an end } to some instructions.

Then there a few other things.

for ( some setup , a condition , what to do after each loop )

  1. setup: make a variable to keep track of each time it runs through the directions (This is usually written i but it's just a convention - and it's for "iterator" or "index" or whatever. It can be anything you want. The setup can also be more complex
  2. condition: as long as this i thing is lower than 10, run the loop again
  3. after each loop: increment the i by one

So - the loop should just run whatever code is inside it... 10? times? or 11?

for (var counter = 0; counter < 10; counter++) {
  console.log('curerntly...', counter);
}

Now... what goes inside?

That's where the % bit comes in. This is the classic "fizz buzz" type of test.

for (var i = 0; i < 10; i++) {
  var message = 'Marco';
  
  var isOdd = (i % 2) != 0; // if it can't be evenly divided by 2...
  if (isOdd) {
    message = 'Polo';
  }

  console.log(message);
}

https://jsfiddle.net/sheriffderek/dtkj0aby/

and you can write it in many different ways too.

for (var i = 0; i < 10; i++) {
  console.log( (i % 2) != 0 ? "Marco" : "Polo" );
}

but that's pretty ugly! (yes, you can even make it shorter...)

So, stick to what is helping you and your team read it instead of trying to get too crazy and unreadable!!! The code is just as much for the humans as it is for the computer. It's our shared language.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM