我正在尝试解决以下问题(Udacity&#Javascript简介):
路线: 写一个打印出以下歌曲的循环。从99开始,到1瓶结束。
示例:
墙上挂着99瓶果汁! 99瓶果汁!拿下一个,把它传递下去......墙上挂着98瓶果汁! 墙上挂着98瓶果汁! 98瓶果汁!拿下一个,通过它 在墙上...... 97瓶果汁!...
墙上有2瓶果汁! 2瓶果汁!拿一个,把它递过来......墙上有一瓶果汁!
墙上挂了1瓶果汁! 1瓶果汁!拿下一个,将它传递给...墙上的0瓶 s 果汁!
我的代码没有正确输出最后一行(它不包括" s""瓶")
我的代码如下所示:
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
}
为什么这段代码忽略了我的条件" num == 2?"在最后一行输出?
仅供参考,我能够使用以下代码解决这个问题,但它看起来并不干净,所以我想优化它:
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;
}
答案 0 :(得分:0)
三元运算符的东西只是一团糟,我试图解决它,但它确实值得尝试修复,因为使用这样的三元运算符不是一个好主意。你已经在另一个三元内嵌了一个三元组,基本上有一个if, else if, else
缩成3行。
你可以避免做额外的逻辑,并意识到当数字不是1时你只需要添加's'。
通过将此逻辑提取到返回字符串's'
或空字符串''
的函数中,您只需将此函数插入循环并为其提供n
和{{ 1}}。
n-1
然后你可以简单地将你的循环改为
function plural(n) {
return n == 1 ? '' : 's'; // note this is the appropriate usage of a ternary operator
}
答案 1 :(得分:0)
好吧,问题是,如果我理解正确的话,为什么第一个代码示例的最后一行输出“0瓶”而不是“0瓶”。
因此,让我们将您的代码翻译为您想要实现的内容,并将您的代码翻译成英文并找出解释器的作用:
将num
设为99。
当num
大于或等于1时,请执行以下操作:
2.1如果num
等于1,请将plural
设为""
,将nextPlural
设为"s"
。
2.2如果num
等于2,则将plural
设为"s"
,将nextPlural
设为""
。
2.3否则将plural
和nextPlural
设置为"s"
。
控制台输出很简单,所以我不会在这里提及。
将num
设为num-1
。
您可能会注意到用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
}
这是什么意思?您使用三元运算符时出现语法错误。它可能是什么?让我们声明一个新变量并尝试设置并将其与其他变量一起输出。
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
}
您会注意到test
仍然等于空字符串,就像nextPlural
一样。这是因为using &&
is not the correct way of instantiating variables inside ternary constructions,所以这段代码将按预期工作:
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;
}
如果您有兴趣,我可能会为解决方案编程:
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'; }