我目前仍然坚持如何显示这个数组

时间:2017-06-22 02:47:13

标签: javascript arrays

这是我目前的代码。我在显示序数方面遇到了问题,因为我不知道接下来要循环播放什么。有人可以帮忙吗?

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
    var currentBand = bandsListArray[i];
    bandsString += "<li> My #" + [i + 1] + " band is " + currentBand + "</li>";
}

var bands = i;
if (bands == 0) {
    bands = i + "st";;
} else if (bands == 1) {
    bands = i + "nd";
} else if (bands == 2) {
    bands = i + "rd";
} else if (bands == 3) {
    bands = i + "th";
} else if (bands == 4) {
    bands = i + "th";
} else {

}

for (var i = 0; i < bandsListArray.length; i++) {
    var newBandList = bandsListArray[i];

    newBandString += "<li>My # " + [i + 1] + " band is " + currentBand + "</li>";
}


document.write(bandsString);
document.write(newBandString);

3 个答案:

答案 0 :(得分:1)

我不确定你要做什么,但这有帮助吗?我不是手动设计如何在数字上设置正确的序数后缀(&#39; st&#39;,&#39; nd&#39;&#39; rd&#39;等),而是{{{ 3}}

ordinal_suffix_of函数的工作原理如下:

第一部分是功能的开始。希望这是不言自明的。我们正在创建一个接受一个参数的函数,该函数将在函数内部命名为i

function ordinal_suffix_of(i) {

下一部分定义jk。您只需使用逗号键入var一次即可声明多个变量,如下所示:

var j = i % 10,
    k = i % 100;

这将完全相同:

var j = i % 10;
var k = i % 100;

百分号(%)被称为&#34;模数&#34;并且是一个数学运算符。它的作用是将左操作数除以右操作数并返回余数。因此,在j的情况下,它被分配i10之外的剩余部分。例如,如果i14j将为4。如果i179j将为9。同样的事情发生在k上,除了它除以100而不是10

它实际做的是提取最后1位数(j)和最后2位数(k),然后只检查(if声明)他们应该有哪些序数后缀。

k的原因是您始终希望以123结尾的数字具有后缀st,{{1当数字以ndrd11结尾时,}和12 除了 它应该得到后缀13。例如,th应为112112th应为313513313513th通过设置来处理此问题,以便如果数字以k1112结尾,我们只会添加13。请注意,th会产生11 % 100,因此这适用于111112

&#13;
&#13;
13
&#13;
&#13;
&#13;

答案 1 :(得分:0)

试试这个,只需使代码更简单

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
   var currentBand = bandsListArray[i];
   bandsString += "<li> My #" + [i + 1] + " band is " + currentBand + "</li>";        
   newBandString += "<li> My #" + numeric(i+1) + " band is " + currentBand + "</li>";
}

function numeric(i){
   var bands = i+"th";
  if (i == 1) {
    bands = i + "st";;
  } else if (i == 2) {
    bands = i + "nd";
  } else if (i == 3) {
    bands = i + "rd";
  } 
  return bands;
}
document.write(bandsString);
document.write(newBandString);

答案 2 :(得分:0)

如果这是你想要达到的目的:

  

我的第二支乐队是Atmosphere

然后你可以如何实现它

var bandsString = "";
var newBandString = "";
var bandsListArray = ["Linkin Park", "Atmosphere", "Rage Against The Machine", "Immortal  Technique", "System of a Down"];

for (var i = 0; i < bandsListArray.length; i++) {
    var currentBand = bandsListArray[i];
    bandsString += "<li> My #" + (i + 1) + " band is " + currentBand + "</li>";
    newBandString += "<li>My " + numberSuffix(i + 1) + " band is " + currentBand + "</li>";
}

function numberSuffix(number){
        if(number % 10==1) return number+'st';
        if(number % 10==2) return number+'nd';
        if(number % 10==3) return number+'rd';
        return number+'th';
}

document.write(bandsString);
document.write(newBandString);