Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的JS代码是否适用于';墙上挂着99瓶啤酒';歌曲_Javascript_If Statement_While Loop_Logic_Conditional - Fatal编程技术网

Javascript 我的JS代码是否适用于';墙上挂着99瓶啤酒';歌曲

Javascript 我的JS代码是否适用于';墙上挂着99瓶啤酒';歌曲,javascript,if-statement,while-loop,logic,conditional,Javascript,If Statement,While Loop,Logic,Conditional,JS新手在这里寻找一些建议。我一直在阅读Head First JavaScript编程书籍,它向我展示了如何将“99瓶啤酒在墙上”这首歌记录到控制台上。以下是原始代码: var word=“瓶子”; var计数=99; 而(计数>0){ console.log(计数+“”+word+”墙上的啤酒); console.log(啤酒的计数+“”+word+”); log(“拿一个下来,传一圈,”); 计数=计数-1; 如果(计数>0){ console.log(计数+“”+word+”墙上的啤酒)

JS新手在这里寻找一些建议。我一直在阅读Head First JavaScript编程书籍,它向我展示了如何将“99瓶啤酒在墙上”这首歌记录到控制台上。以下是原始代码:

var word=“瓶子”;
var计数=99;
而(计数>0){
console.log(计数+“”+word+”墙上的啤酒);
console.log(啤酒的计数+“”+word+”);
log(“拿一个下来,传一圈,”);
计数=计数-1;
如果(计数>0){
console.log(计数+“”+word+”墙上的啤酒);
}否则{
log(“墙上不再有啤酒”+单词+”);
}

}
用简洁的三元表达式替换那些笨重的if-else语句,可以稍微整理一下逻辑:

while (count > 0) {
    var bottle = count == 1 ? "bottle" : "bottles";
    console.log(count + " " + word + " of beer on the wall");
    console.log(count + " " + word + " of beer,");
    console.log("Take one down, pass it around,");
    --count;
    var bottle = count == 1 ? "bottle" : "bottles";
    console.log(count + " " + word + " of beer on the wall.");
}

这使得语法假设墙上的短语
0瓶啤酒
是正确的,并且是我们想要使用的。我觉得这听起来不错,但我想从技术上讲,0瓶或1瓶都应该是单数。

既然你想接近这个例子,我只指出你可以跳过

if (count < 1){
    var word = "bottles"
  }
进入

并将var声明放在此处:

while (count > 0) {
    var word = "bottles";

还有很多其他内容可以缩短,但这可能会让它变得更困难。

一种可能是将整个瓶子单词内容提取到一个单独的函数中。这样,您只需要更改原始代码中的任何内容

var计数=99;
而(计数>0){
console.log(count+“”+getballword(count)+“墙上的啤酒”);
console.log(count+“”+getBottleWord(count)+啤酒);
log(“拿一个下来,传一圈,”);
计数=计数-1;
如果(计数>0){
console.log(count+“”+getballword(count)+”墙上的啤酒);
}否则{
log(“不再”+getballword(count)+“墙上的啤酒”);
}
}
函数getword(count){
退货数量===1“瓶”:“瓶”;

}
好吧,您可以将if-else语句改写成更简洁的语句,例如:

var word = "bottles";
var count = 99;
while (count > 0) {
    console.log(count + " " + word + " of beer on the wall");
    console.log(count + " " + word + " of beer,");
    console.log("Take one down, pass it around,");
    count = count - 1;
    if (count > 1) {
        console.log(count + " " + count + " of beer on the wall.");
    } else if(count == 1) {
        console.log(count + " bottle of beer on the wall."); 
    } else {
        console.log("No more " + word + " of beer on the wall.");
    }
}

但在你的问题中值得指出的部分是:注意多个
var word=“blanks”
句子,以及通常使用
var
关键字及其注意事项,特别是在其范围方面。例如,考虑它们和它们各自的区别。

< P>可以使用递归来调用相同的函数,直到0个计数

时,用更少的瓶子调用相同的函数。
const pluralize=(i,word)=>word+(i>1?'s':“”)
常数瓶=计数=>{
log(`${count}${pluralize(count,'瓶')}墙上的啤酒。`)
log(`${count}${pluralize(count,'瓶')}啤酒。`)
log(`Take one down,pass out`)
如果(计数>1){
log(`count-1}${pluralize(count-1,'瓶')}墙上的啤酒。`)
瓶子(count-1)//递归调用瓶子函数,只需少一个瓶子
}
否则{
console.log(`墙上不再有啤酒瓶了。`)
}
}
瓶子(12)
我是这样做的:

var word="bottles";
var count=99;
while (count>1) {
    console.log(count + " " + word + " of beer on the wall");
    console.log(count + " " + word + " of beer,");
    console.log("Take one down, pass it around,");
    count=count-1;
    
    if (count>1) {
        console.log(count + " " + word + " of beer on the wall.");
    }
    if (count==1) {
        console.log(count + " bottle" + " of beer on the wall.");
        console.log(count + " bottle" + " of beer on the wall,");
        console.log(count + " bottle" + " of beer");
        console.log("Take one down, pass it around,");
        count=count-1;
    }
    if (count==0) {
        console.log("No more " + word + " of beer on the wall.");
        
    }
}

你可能想看看。一定要先通过他们的帮助中心,然后再发布银行,苏拉杰-我会检查的。我看到你使用的是三元运算符。我知道这一点,但我还没有完全了解。我会记下来的-非常感谢你花时间回答我的问题。太棒了,谢谢你提醒我关于let变量和slice方法。谢谢。我只是考虑写一瓶“啤酒在墙上”猜我只是想看看我是否可以修改变量。正如你所说,我认为let变量在这里更合适。太好了,谢谢。我还是个新手,对函数还不太熟悉,但我会在更熟悉的时候回顾一下这个建议。递归可能会让人觉得费解,但它只是用不同的参数一次又一次地调用同一个函数,直到不满足某些条件为止。如果您刚刚开始,可能需要一两年的时间才能实现递归。享受编程的冒险。我现在可以看到这个函数如何使它变得更简单。谢谢你!这只是我的第二节JavaScript课程
var word = "bottles";
var count = 99;
while (count > 0) {
    console.log(count + " " + word + " of beer on the wall");
    console.log(count + " " + word + " of beer,");
    console.log("Take one down, pass it around,");
    count = count - 1;
    if (count > 1) {
        console.log(count + " " + count + " of beer on the wall.");
    } else if(count == 1) {
        console.log(count + " bottle of beer on the wall."); 
    } else {
        console.log("No more " + word + " of beer on the wall.");
    }
}
// Instead of var you can start using 'const' for variables that will not be changed
// and 'let' for variables that will be changed.
let word = "bottles";
let count = 99;
while (count > 0) {
    if (count == 1){
        // No need to recall 'var' here, you are reassigning an existing var not creating a new one.
        // Can just do:
        // word = "bottle"
        word = "bottle"
    }
    /* As of ES6 You can use string templets here:
     *
     * console.log(`${count} ${word} of beer on the wall`);
     * console.log(`${count} ${word} of beer,`);
     * console.log("Take one down, pass it around,");
     */
    console.log(count + " " + word + " of beer on the wall");
    console.log(count + " " + word + " of beer,");
    console.log("Take one down, pass it around,");

    count = count - 1;
    /* For the word changing, you can lose some code by not actually changing the string,
     * That way you will not need to reassign it back to plural again when the count is 0
     * To take the 'bottles' string without the final plural 's' character use:
     * word.slice(0,-1);
     */
    if (count > 0) {
      if (count == 1){
        console.log(count + " " + word.slice(0, -1) + " of beer on the wall.");
      }
    } else {
        // now you can lost this if statment.
        console.log("No more " + word + " of beer on the wall.");
    }
}
var word="bottles";
var count=99;
while (count>1) {
    console.log(count + " " + word + " of beer on the wall");
    console.log(count + " " + word + " of beer,");
    console.log("Take one down, pass it around,");
    count=count-1;
    
    if (count>1) {
        console.log(count + " " + word + " of beer on the wall.");
    }
    if (count==1) {
        console.log(count + " bottle" + " of beer on the wall.");
        console.log(count + " bottle" + " of beer on the wall,");
        console.log(count + " bottle" + " of beer");
        console.log("Take one down, pass it around,");
        count=count-1;
    }
    if (count==0) {
        console.log("No more " + word + " of beer on the wall.");
        
    }
}