Console.log does not mach to array

Viewed 37

Pressing instrument sound button a sound is played. I've tried to match each array element on console.log but is not working.

Why does "console.log" match to array and sometimes not? I mean, console.log sometimes shows the correspondent instrument name to the sound heard, sometimes the wrong name is shown. The first "console.log" is the only one which matches to the sound played.

   
    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>

1 Answers

I think i finally got it !!!

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

Tell me please if this isn't what you expected.

The line $("#element").html("<audio autoplay><source src=\"" + thisSound + "\" type=\"audio/mpeg\"><embed src=\"" + thisSound + "\" hidden=\"true\" autostart=\"true\" /></audio>"); I'd to remove it because it was giving me an error. Just paste it back and that's it (I hope xd)

(BTW the code only runs when the instrument sound button is pressed (I've understood it that way (if not, tell me as well and I'll try to fix it)))

Related