如果/ Else嵌套在Javascript Not Looping中的While循环中

时间:2015-09-18 23:21:51

标签: javascript if-statement while-loop

在编写时,该功能仅返回歌曲的一行。 “数字”是用户输入的数字。如何让它继续通过while循环直到它变为0?我已经玩过在哪里减少数量(在while条件下,在每个if语句中等),但这似乎并不重要。感谢您的任何反馈!编辑显示为数组而不是字符串。

 var theFunction = function(number) {
  var song = [];
  while(number > 0){
    if(number > 2) {
      song.push(number + "lyrics in song.");
      number--;
    } else if(number === 2) {
      song.push(number + "different lyrics in song");
      number--;
    } else if(number === 1) {
      song.push(number + " other lyrics");
      number--;
    } else {
      song.push("End of song");
    }
    return song;
  };
};

1 个答案:

答案 0 :(得分:0)

谢谢!我在错误的地方收到了退货声明。它应该是:

var theFunction = function(number) {
  var song = [];
  while(number > 0){
    if(number > 2) {
      song.push(number + "lyrics in song.");
      number--;
    } else if(number === 2) {
      song.push(number + "different lyrics in song");
      number--;
    } else if(number === 1) {
      song.push(number + " other lyrics");
      number--;
    } else {
      song.push("End of song");
    }
  } return song;
};