我在这个site上使用了一个代码来在Javascript中对数组进行洗牌。但是,它只在页面刷新时洗牌,而不是在单击按钮时洗牌。我需要括号()中的'汽车'来使数组洗牌,但这是主要的问题。我该如何解决这个问题?
//var clickMe = ["Lamborgihni", " Ferrari", " Porsche"," Buggatti", " Toyota", " Mercedes", " Audi"," BMW"];
document.getElementById("finished").onclick = function() {
randomCars(Cars)
};
function randomCars(Cars) {
var i = Cars.length,
t, j;
// While there remain elements to shuffle...
while (0 !== i) {
// Pick a remaining element...
j = Math.floor(Math.random() * i);
i -= 1;
// And swap it with the current element.
t = Cars[i];
Cars[i] = Cars[j];
Cars[j] = t;
}
return Cars;
}
var answer1 = ["Lamborgihni", " Ferrari", " Porsche", " Buggatti", " Toyota", " Mercedes", " Audi", " BMW"];
answer1 = randomCars(answer1);
//console.log(answer1);
document.getElementById("finished").innerHTML = answer1;
var firstPlace = document.querySelector(".firstPlace");
var secondPlace = document.querySelector(".secondPlace");
var thirdPlace = document.querySelector(".thirdPlace");
if (answer1 = answer1.length) {
firstPlace.textContent = answer1[0] + " Came 1st Place";
}
<h1>Car Racing Game with a Table</h1>
<p>We have 8 cars participating in a race. The aim of this exercise is to randomize the order in which the cars finish the race. Pressing a button should trigger this event.</p>
<div class="carRace">
<p id="finished"></p>
</div>
<button onclick="randomCars(Cars)" type="button" id="clickMe">Click Me</button>
<p class="firstPlace"></p>
<p class="secondPlace"></p>
<p class="thirdPlace"></p>
答案 0 :(得分:0)
主要的错误是你在其功能之外使用Cars(这是一个函数参数)。你应该使用answer1:
document.getElementById("finished").onclick = function() {
randomCars(answer1)
};
但除非你把它移到randomCars函数,否则不会执行这部分代码:
document.getElementById("finished").innerHTML = answer1;
var firstPlace = document.querySelector(".firstPlace");
var secondPlace = document.querySelector(".secondPlace");
var thirdPlace = document.querySelector(".thirdPlace");
if (answer1 == answer1.length) {
firstPlace.textContent = answer1[0] + " Came 1st Place";
}
答案 1 :(得分:0)
我以为你在寻找这个如果不让我纠正你需要帮助的地方。
var randomCarArray = ["Lamborgihni", " Ferrari", " Porsche", " Buggatti", " Toyota", " Mercedes", " Audi", " BMW"];
function randomCars() {
var temp = randomCarArray;
var i = temp.length,
t, j;
// While there remain elements to shuffle...
while (0 !== i) {
// Pick a remaining element...
j = Math.floor(Math.random() * i);
i -= 1;
// And swap it with the current element.
t = temp[i];
temp[i] = temp[j];
temp[j] = t;
}
randomCarArray = temp;
document.getElementById("finished").innerHTML = randomCarArray;
var firstPlace = document.querySelector(".firstPlace");
firstPlace.textContent = randomCarArray[0] + " Came 1st Place";
return randomCarArray;
}
document.getElementById("finished").onclick = function() {
randomCars()
};
randomCars();
&#13;
<h1>Car Racing Game with a Table</h1>
<p>We have 8 cars participating in a race. The aim of this exercise is to randomize the order in which the cars finish the race. Pressing a button should trigger this event.</p>
<div class="carRace">
<p id="finished"></p>
</div>
<button onclick="randomCars('Cars')" type="button" id="clickMe">Click Me</button>
<p class="firstPlace"></p>
<p class="secondPlace"></p>
<p class="thirdPlace"></p>
&#13;