简体   繁体   English

我的代码无法识别条件

[英]My code isn't recognizing a condition

I am trying to solve this following problem (Udacity's Intro to Javascript): 我正在尝试解决以下问题(Udacity的Java语言简介):

Directions: Write a loop that prints out the following song. 方向:编写一个循环,以打印出下一首歌曲。 Starting at 99, and ending at 1 bottle. 开始于99,结束于1瓶。

Example: 例:

99 bottles of juice on the wall! 墙上有99瓶果汁! 99 bottles of juice! 99瓶果汁! Take one down, pass it around... 98 bottles of juice on the wall! 取一下来,绕过去...墙上有98瓶果汁!

98 bottles of juice on the wall! 墙上有98瓶果汁! 98 bottles of juice! 98瓶果汁! Take one down, pass it around... 97 bottles of juice on the wall! 取一下来,绕过去...墙上有97瓶果汁!

... ...

2 bottles of juice on the wall! 2瓶果汁在墙上! 2 bottles of juice! 2瓶果汁! Take one down, pass it around... 1 bottle of juice on the wall! 取一下来,绕过去...墙上一瓶果汁!

1 bottle of juice on the wall! 1瓶果汁在墙上! 1 bottle of juice! 一瓶果汁! Take one down, pass it around... 0 bottle s of juice on the wall! 就拿一跌,各地通过它... 0墙上果汁瓶小号

and my code doesn't appropriately output the last line (it doesn't include "s" after "bottle"): 而且我的代码没有正确输出最后一行(在“瓶”之后不包含“ s”):

My code looks like this: 我的代码如下所示:

 var num = 99;

 while (num >= 1) {

 num == 1 ? ((plural = "") && (nextPlural = "s")) :
 num == 2 ? ((plural = "s") && (nextPlural = "")) :
 ((plural = "s") && (nextPlural = "s"));

 console.log (num + " bottle" + plural + " of juice on the wall! " + num + "bottle" + plural + " of juice! " + "Take one down, pass it around... " + (num - 1) + " bottle" + nextPlural + " of juice on the wall!");
 num = num - 1
 }

why is this code ignoring my condition for "num == 2?" 为什么这段代码会忽略我的“ num == 2”条件? at the last line of output? 在输出的最后一行?

FYI, I was able to solve this using the following code, but it didn't look clean so I wanted to optimize this: 仅供参考,我可以使用以下代码解决此问题,但它看起来并不干净,因此我想对其进行优化:

var num = 99;
var plural = "s";
var nextNum = num - 1;
var nextPlural = "s";

while (num >= 1) {
if (num > 1 && nextNum > 1){
plural = "s";
nextPlural = "s";
}
else if (num > 1 && nextNum == 1){
plural = "s";
nextPlural = "";
}
else if (num == 1 && nextNum <= 1){
    plural = "";
    nextPlural = "s";
}
console.log(num + " bottle" + plural + " of juice on the wall! " + num + " bottle"+ plural + " of juice! " +
"Take one down, pass it around... " + nextNum + " bottle" + nextPlural + " of juice on the wall!");
num = num - 1;
nextNum = num - 1;
}

The ternary operator stuff is just a big mess, I've tried to figure it out, but it's really worth trying to fix since it's just not a good idea to use ternary operators like that. 三元运算符的东西是一个很大的烂摊子,我试图弄清楚,但是确实值得尝试修复,因为使用像这样的三元运算符并不是一个好主意。 You've nested a ternary inside another ternary and basically have an if, else if, else condensed into 3 lines. 您已将一个三元数嵌套在另一个三元数中,并且基本上将if, else if, else精简为3行。

You can avoid doing extra logic and realize that you need only add an 's' when the number is not 1. 您可以避免执行额外的逻辑,并意识到只需要在数字不为1时添加一个's'即可。

By extracting this logic into a function that returns the string 's' or the empty string '' , you can simply plug this function into your loop and provide it with n and n-1 . 通过将该逻辑提取到返回字符串's'或空字符串''函数中,您可以简单地将此函数插入​​循环并为其提供nn-1

function plural(n) {
  return n == 1 ? '' : 's';  // note this is the appropriate usage of a ternary operator
}

You can then simply your loop to just 然后,您可以简单地将循环

while (num >= 1) {
  console.log('...');  // left as an exercise for you to fill in the required function call and string concatenations.
  num--;  // same thing as num = num - 1;
}

Alright, so the question, if I understand it right, is why the last line for the first code sample outputs "0 bottle" instead of "0 bottles". 好吧,所以,如果我理解正确的话,为什么第一个代码示例的最后一行输出“ 0瓶”而不是“ 0瓶”。

So let's translate what you attempted to achieve with your code into English and figure out what the interpreter does: 因此,让我们将您尝试通过代码实现的内容翻译成英文,并弄清楚解释器的作用:

  1. Set num to 99. num设置为99。

  2. While num greater or equal to 1, do the following: num大于或等于1时,请执行以下操作:

    2.1 If num equals 1, set plural to "" and nextPlural to "s" . 2.1如果num等于1,则将plural设置为"" ,将nextPlural"s"

    2.2 Else if num equals 2, set plural to "s" and nextPlural to "" . 2.2否则,如果num等于2,则将plural设置为"s" ,将nextPlural""

    2.3 Else set both plural and nextPlural to "s" . 2.3 Else将pluralnextPlural都设置为"s"

  3. The console output is trivial, so I won't mention it here. 控制台输出是微不足道的,因此在此不再赘述。

  4. Set num equal to num-1 . 设置num等于num-1

You might notice that substituting the ternary operator with an if statement yields the correct result: 您可能会注意到,用if语句代替三元运算符会产生正确的结果:

 var num = 1; var plural = ''; var nextPlural = ''; while (num >= 1) { if(num==1) { plural = ''; nextPlural='s'; } /*num == 1 ? ((plural = "") && (nextPlural = "s")) : (num == 2 ? ((plural = "s") && (nextPlural = "")) : ((plural = "s") && (nextPlural = "s")));*/ console.log (num + " bottle" + plural + " of juice on the wall! " + num + " bottle" + plural + " of juice! " + "Take one down, pass it around... " + (num - 1) + " bottle" + nextPlural + " of juice on the wall!"); num = num - 1 } 

What does that mean? 这意味着什么? That there is a syntax error with your use of the ternary operator. 您使用三元运算符存在语法错误。 What could it possibly be? 可能是什么? Let's declare a new variable and try setting and outputting it along with the others. 让我们声明一个新变量,然后尝试将其与其他变量一起设置并输出。

 var num = 1; var plural = ''; var nextPlural = ''; var test = ''; while (num >= 1) { //if(num==1) { plural = ''; nextPlural='s'; } num == 1 ? ((plural = "") && (nextPlural = "s") && (test = "test")) : (num == 2 ? ((plural = "s") && (nextPlural = "")) : ((plural = "s") && (nextPlural = "s"))); console.log (num + " bottle" + plural + " of juice on the wall! " + num + " bottle" + plural + " of juice! " + "Take one down, pass it around... " + (num - 1) + " bottle" + nextPlural + " of juice on the wall!"); console.log(test); num = num - 1 } 

You will notice that test remains equal to an empty string, just like nextPlural . 您会注意到test仍然等于一个空字符串,就像nextPlural一样。 That's because using && is not the correct way of instantiating variables inside ternary constructions , so this code will work as intended: 那是因为使用&&不是在三元构造中实例化变量的正确方法 ,因此此代码将按预期工作:

 var num = 99; var plural = ''; var nextPlural = ''; while (num >= 1) { //if(num==1) { plural = ''; nextPlural='s'; } num == 1 ? (plural = "", nextPlural = "s") : (num == 2 ? (plural = "s", nextPlural = "") : (plural = "s", nextPlural = "s")); console.log (num + " bottle" + plural + " of juice on the wall! " + num + " bottle" + plural + " of juice! " + "Take one down, pass it around... " + (num - 1) + " bottle" + nextPlural + " of juice on the wall!"); --num; } 

If you are interested, this is how I would probably program the solution: 如果您有兴趣,这就是我可能编写解决方案的方法:

 for(var i=99, w=' on the wall!'; i>0; i--) { console.log(returnEmpties(i)+w+' '+returnEmpties(i)+'! Take one down, pass it around... '+returnEmpties(i-1)+w); } function returnEmpties(n) { return n+' bottle'+(n==1?'':'s')+' of juice'; } 

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

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