【问题标题】:Is my JS code right for '99 Bottles of Beer on the Wall' song? [closed]我的 JS 代码是否适合“墙上的 99 瓶啤酒”歌曲? [关闭]
【发布时间】:2018-07-17 16:16:23
【问题描述】:

JS新手在这里寻求一些建议。我一直在研究 Head First JavaScript Programming 这本书,它向我展示了如何将“墙上的 99 瓶啤酒”歌曲记录到控制台。这是原始代码:

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 > 0) {
        console.log(count + " " + word + " of beer on the wall.");
    } else {
        console.log("No more " + word + " of beer on the wall.");
    }
}

没有给出答案,它暗示代码没问题但不是 100% 正确,并询问您是否可以找到缺陷并​​修复它。在查看控制台后,我发现 while 循环没有考虑到一旦你在歌曲中找到一瓶啤酒,“bottles”这个词就需要变为“bottle”。我在循环中添加了一些 if 语句,以便在需要时将 var 单词更改为“bottle”。该代码现在似乎可以正常工作:

var word = "bottles";
var count = 99;
while (count > 0) {
  if (count == 1){
    var word = "bottle"
  }
    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 > 0) {
      if (count == 1){
        var word = "bottle"
      }
        console.log(count + " " + word + " of beer on the wall.");
    } else {
      if (count < 1){
        var word = "bottles"
      }
        console.log("No more " + word + " of beer on the wall.");
    }
}

我的问题是 - 有没有比我的尝试更好或更简洁的方法来做到这一点?我觉得我的尝试有点混乱。我知道您可以为此使用 For 循环,但该示例使用了 While 循环,所以如果可能的话,我想坚持下去。

非常感谢!

【问题讨论】:

标签: javascript if-statement while-loop logic conditional


【解决方案1】:

你可以通过用简洁的三元表达式替换那些笨重的 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 bottles of beer on the wall 是正确的并且是我们想要使用的。这听起来对我来说是正确的,但我想从技术上讲,0 瓶或 1 瓶都应该是单数的。

【讨论】:

  • 我看到您使用的是三元运算符。我知道(有点),但我还没有完全了解它。我会记下这一点 - 非常感谢您花时间回答我的问题。
【解决方案2】:

由于您想紧贴示例,我只指出您可以跳过

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

如果你改变了

 if (count == 1){
    var word = "bottle"
  }

进入

if (count == 1){
    word = "bottle"
  }

并在此处放置 var 声明:

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

还有很多其他的东西你也可以缩短,但这可能会使它变得更加困难。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 我现在可以看到这个功能如何使它更简单。谢谢你!
    【解决方案4】:
    // 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.");
        }
    }
    

    【讨论】:

    • 太棒了,谢谢你提醒我 let 变量和 slice 方法。
    【解决方案5】:

    您可以使用递归调用相同的函数,但瓶数更少,直到计数为 0

    const pluralize = (i, word) => word + (i > 1 ? 's': '')
      
    const bottles = count => {
      console.log(`${count} ${pluralize(count, 'bottle')} of beer on the wall.`)
      console.log(`${count} ${pluralize(count, 'bottle')} of beer.`)
      console.log(`Take one down, pass it around`)
      if (count > 1) {
        console.log(`${count - 1} ${pluralize(count - 1, 'bottle')} of beer on the wall.`)
        bottles(count - 1) // recursively call the bottles function with one less bottle
      }
      else {
        console.log(`No more bottles of beer on the wall.`)
      }
    }
    
    bottles(12)

    【讨论】:

    • 太好了,谢谢。我还是新手,还没有完全掌握功能,但是一旦我更熟悉了,我一定会回头看看这个建议。
    • 递归似乎让人费解,但它只是用不同的参数一次又一次地调用同一个函数,直到不满足某些条件。如果您刚刚开始,可能需要一两年的时间才能进行递归。享受编程的冒险。
    【解决方案6】:

    好吧,您可以将 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 = "bottles" 句子,并且通常使用 var 关键字及其注意事项,尤其是关于它的范围。例如,考虑varlet 与它们各自的scopes 之间的区别。

    【讨论】:

    • 谢谢。我确实考虑过只写“墙上的一瓶啤酒”。猜猜我只是想看看我是否也可以修改变量。也像你说的,我认为 let 变量的使用在这里会更合适。
    【解决方案7】:

    我是这样做的:

    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.");
            
        }
    }
    

    【讨论】:

    • 这只是我的第二节 JavaScript 课
    猜你喜欢
    • 2016-10-06
    • 2020-04-03
    • 2021-04-27
    • 2014-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多