gpt4 book ai didi

arrays - Console.log与数组不匹配

转载 作者:行者123 更新时间:2023-12-02 22:35:56 26 4
gpt4 key购买 nike

按下乐器声音按钮会播放声音。我试图匹配console.log上的每个数组元素,但是不起作用。
为什么“console.log”与数组匹配,有时不匹配?我的意思是,console.log有时会显示与所听到声音对应的乐器名称,有时会显示错误的名称。第一个“console.log”是与播放的声音匹配的唯一一个。

   
let index;

let sounds = ["DB.WAV","CLA.WAV","TBN.WAV","REC.WAV","TRI.WAV"];
oldSounds = [];

let playSounds = function () {
index = Math.floor(Math.random() * sounds.length);

thisSound = sounds[index];

oldSounds.push(thisSound);
sounds.splice(index, 1);

if (sounds.length < 1) {
sounds = oldSounds.splice(0, oldSounds.length);
}

$("#element").html("<audio autoplay><source src=\"" + thisSound + "\" type=\"audio/mpeg\"><embed src=\"" + thisSound + "\" hidden=\"true\" autostart=\"true\" /></audio>");



if(index === 0) {
console.log(`Double Bass`);
} else if(index === 1) {
console.log(`Clarinet`);
}else if(index === 2) {
console.log(`Trombone`);
}else if(index === 3) {
console.log(`Recorder`);
}else if(index === 4) {
console.log(`Triangle`);}
}
</script>
<div class="wrapper clearfix">
<div class="player-panel">

<button class="btn-answer"><i class="ion-ios-download-outline"></i>Answer</button>
<div onclick="playSounds()">
<button class="btn-play"><i class="far fa-play-circle"></i>Instrument Sound</button>
</div>
<input type="text" placeholder="Instrument name" class="instrument">
<div id="element"></div>
</div>
</div>

最佳答案

我想我终于明白了!

<html>
<head>
<title>lkl</title>
</head>
<body>

<div class="wrapper clearfix">
<div class="player-panel">

<button class="btn-answer"><i class="ion-ios-download-outline"></i>Answer</button>

<div onclick="playSounds()">
<button class="btn-play"><i class="far fa-play-circle"></i>Instrument Sound</button>
</div>

<input type="text" placeholder="Instrument name" class="instrument">
<div id="element"></div>
</div>
</div>

<script>
let index;
let sounds = ["DB.WAV","CLA.WAV","TBN.WAV","REC.WAV","TRI.WAV"];
oldSounds = [];

let playSounds = function () {
index = Math.floor(Math.random() * sounds.length);

console.log(sounds[index]);

if(index == sounds.indexOf('DB.WAV')) {
console.log(`Double Bass`);
} else if(index == sounds.indexOf('CLA.WAV')) {
console.log(`Clarinet`);
}else if(index == sounds.indexOf('TBN.WAV')) {
console.log(`Trombone`);
}else if(index == sounds.indexOf('REC.WAV')) {
console.log(`Recorder`);
}else if(index == sounds.indexOf('TRI.WAV')) {
console.log(`Triangle`);
}


thisSound = sounds[index];

oldSounds.push(thisSound);
sounds.splice(index, 1);

//checking if the code of avobe is working
console.log(oldSounds);
console.log(sounds);

if (sounds.length < 1) {
sounds = oldSounds.splice(0, oldSounds.length);
}
}



</script>
</body>
</html>

如果这不是您期望的,请告诉我。
我会删除 $("#element").html("<audio autoplay><source src=\"" + thisSound + "\" type=\"audio/mpeg\"><embed src=\"" + thisSound + "\" hidden=\"true\" autostart=\"true\" /></audio>"); 行,因为它给了我一个错误。只需将其粘贴回原样即可(我希望xd)
(顺便说一句,该代码仅在按下乐器声音按钮时运行(我已经这样理解(否则,请告诉我,我会尝试修复它)))

关于arrays - Console.log与数组不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64587986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com