【发布时间】:2017-11-06 22:04:44
【问题描述】:
为什么下面的代码大部分时间都显示undefined?
var fragOne = ["From morning until night", "Most pastors in church", "Hollywood movies in the 21st century", "Never disrespect anyone unless"];
var fragTwo = ["start after the weekend", "the Christians nowadays", "my friend Jacob in Class", "because of snowfall during winter"];
var fragThree = ["some girls in the class.", "intelligent Pilots in the UK.", "Several girls in my complex.", "If I believe in God."];
var button = document.getElementById("btn");
button.addEventListener("click", function(){
var displaying = document.getElementById("display");
var randomOne = Math.floor(Math.random()*fragOne.length);
var randomTwo = Math.floor(Math.random()*fragTwo.length);
var randomThree = Math.floor(Math.random()*fragThree.length);
var combineRandom = randomOne+randomTwo+randomThree;
display.innerHTML = fragOne[combineRandom]+" "+fragTwo[combineRandom]+" "+fragThree[combineRandom];
console.log(fragOne[combineRandom]+" "+fragTwo[combineRandom]+" "+fragThree[combineRandom]);
});
【问题讨论】:
-
调试你的代码,你就会明白为什么。提示:您不想通过 combineRandom 进行索引,而是要组合字符串
combineRandom = fragOne[randomOne] + fragTwo[randomTwo] + fragThree[randomThree]
标签: javascript quotes