按钮不适用于随机排列的数组

Button does not work with a Shuffled Array

我在 site 上使用了一个代码来随机排列 Javascript 中的一个数组。但是,它只会在页面刷新时打乱数组,而不是在单击按钮时打乱数组。我需要 'Cars' 在方括号()中进行数组随机播放,但这是这里的主要问题。我该如何解决这个问题?

//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>

主要错误是您在其函数之外使用 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";
}

我以为你在找这个如果不是让我纠正你需要帮助的地方。

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();
<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>