微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Console.log不能阵列

如何解决Console.log不能阵列

按下乐器声音按钮将播放声音。我试图匹配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)

(顺便说一句,该代码仅在按下乐器声音按钮时运行(我已经这样理解(否则,请告诉我,我会尝试修复它)))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。