【问题标题】:Error when picking random array element in Javascript: Cannot read properties of undefined在 Javascript 中选择随机数组元素时出错:无法读取未定义的属性
【发布时间】:2022-01-22 13:49:21
【问题描述】:

我想从数组中随机选取一个元素而不重复,但是发生了错误:Cannot read properties of undefined (reading 'sound')

这是我的代码:

var words = [
  { word: 'major', sound: 'major.mp3'},
  { word: 'apply', sound: 'apply.mp3'},
  { word: 'blue',  sound: 'blue.mp3' },
  { word: 'cat',   sound: 'cat.mp3'  },
  { word: 'class', sound: 'class.mp3'},
  { word: 'code',  sound: 'code.mp3' },
  { word: 'cook',  sound: 'cook.mp3' },
  { word: 'daisy', sound: 'daisy.mp3'},
  { word: 'ease',  sound: 'ease.mp3' },
  { word: 'idea',  sound: 'idea.mp3' }
];

var randomWord = words[Math.floor(Math.random() * words.length - 5)]; // just added - 5 here
var audio = new Audio();
var playBtn = document.getElementById("play-button");
var guessBtn = document.getElementById("guess-button");
var nextBtn = document.getElementById("next-button");
var correct = document.getElementById("correct");
var incorrect = document.getElementById("incorrect");

playBtn.addEventListener("click", function() {
  audio.src = randomWord.sound;
  audio.play();
  var name = words.splice(randomWord,1); // just added
  words.push(name); // just added
  })

guessBtn.addEventListener("click", function() {
  var inputBox = document.getElementById("input-box").value;

  if (inputBox == randomWord.word) {
    correct.innerHTML = "Correct!"
  }
  else {
    incorrect.innerHTML = "Incorrect"
  }
})

nextBtn.addEventListener("click", function() {
  location.reload();
})

音频播放流畅,直到我试图让它不再重复。我做错了吗?

【问题讨论】:

  • 假设您尝试获取介于 0 和 words 的最后一个索引之间的随机数,那么您的操作不正确。查看relevant docs的这一部分。
  • 您正在返回一个随机数,然后从中减去 5(这可能是负数)。这是由于运算符优先级。在words.length -5 周围加上括号,当然你知道这只会随机化最多 5 个。

标签: javascript typeerror splice


【解决方案1】:

words[Math.floor(Math.random() * (words.length - 1))];

我觉得你需要这样使用。

因为 Math.floor(Math.random() * words.length - 5) 可以小于 0

【讨论】:

  • 感谢您的帮助,但效果不佳,抱歉。
【解决方案2】:

您可以考虑:1) 构建从 0 到 words.length 的索引数组,2) 选择随机索引 3) 使用随机索引获取单词(单词将是随机的)4) 删除随机索引避免重复。这样,您无需删除您的单词,您的随机索引将始终有效。

//----------New code-----------------
var indexes = []
for (let i=0; i<words.length; i++) {
    indexes.push(i)
}
//-----------------------------------




 playBtn.addEventListener("click", function() {
      //get a random index value
      let randomIndex = Math.floor((Math.random()*words.length))
       //Get your randomword here, using the random index
       var randomWord = words[randomIndex]; 
       //Delete the index so it will not repeat
       indexes.splice(randomIndex,1)
       //This is your code below
       audio.src = randomWord.sound;
       audio.play();
      //var name = words.splice(randomWord,1); // delete this!
      words.push(name); // just added >> is this required?
  })
    

【讨论】:

  • 我的音频用这个播放但仍然重复。
  • 您的代码似乎没有更新随机词。我编辑了我的答案并将添加的代码移动到 playBtn eventListener 函数中。这样,当用户单击播放按钮时,将选择并播放一个新的随机索引和单词。随机索引将从索引数组中删除,以避免重复。您的代码无法在本地运行,所以我不得不猜测解决方案。
猜你喜欢
  • 2017-09-09
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 2017-07-05
相关资源
最近更新 更多