简体   繁体   English

if/else 语句只记录 if,不记录 else

[英]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 !"编写一个循环记录“ Marco !” when i is even,当我是偶数时
  • " Polo !" 波罗!” when i is odd.当我是奇怪的。
  • Do not edit the existing code.不要编辑现有代码。
  • Hint 1: Use an if/else statement提示 1:使用 if/else 语句
  • Hint 2: Google the mod operator (%)提示 2:谷歌 mod 运算符 (%)

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.当我需要它来记录马球时,这会记录 Marco。 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 .它的作用是将x除以y (将11除以4 ),并去除余数 - 在这种情况下,余数将为3 ,因此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.如果你想知道一个数字是否为偶数,你可以使用% 2 ,这就是我们在if语句中所做的。 If the number is even, it will return 0 as there will be no remainder from dividing by two.如果数字是偶数,它将返回0因为除以 2 没有余数。 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:如果我们有6 (我们知道它是偶数),并且我们通过将它除以2测试它是否是偶数,它应该返回0因为没有余数:

 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.因此,如果i是偶数,则取模将返回0 ,这意味着第一个if语句将不会运行,因为i % 2将返回0其计算结果为false ,因此代码将运行console.log("Marco!")如果i是偶数,但是console.log("Polo!")如果i是奇数。

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 ) for( some setupa conditionwhat 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设置:创建一个变量来跟踪每次运行时的方向(这通常写成i但这只是一个约定 - 它用于“迭代器”或“索引”或其他任何东西。它可以是任何你想要的。设置也可以更复杂
  2. condition: as long as this i thing is lower than 10, run the loop again条件:只要这个i东西小于10,就再次运行循环
  3. after each loop: increment the i by one每次循环后:i加一

So - the loop should just run whatever code is inside it... 10?所以 - 循环应该只运行它里面的任何代码...... 10? times?次? or 11?还是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/ 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.这是我们的共同语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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