Beat mix project - struggle with toggle drum function

Hi everyone,

I’m starting with the beat mix project.
I am struggling with the toggle drum function which seem to work when I log the results but do not pass the test.
Below is my code:

// Drum Arrays

let kicks =[false, false, false, false,false, false, false, false,false, false, false, false,false, false, false, false];

let snares =[false, false, false, false,false, false, false, false,false, false, false, false,false, false, false, false];

let hiHats =[false, false, false, false,false, false, false, false,false, false, false, false,false, false, false, false];

let rideCymbals =[false, false, false, false,false, false, false, false,false, false, false, false,false, false, false, false];

// check validity of array name

function isArrayValid(drumArray)

{

    if (drumArray === kicks ||drumArray === snares ||drumArray === hiHats ||drumArray === rideCymbals)

    {return true} else {return false}

}

// change value of the drum array

function toggleDrum (drumArray, index) 

{

    if (isArrayValid(drumArray) && index>=0) 

    {

        drumArray[index] = !drumArray[index]

    }

}

Thanks a lot for your help!

Hi ehytte, I have the same problem. Code works in Visual Code (ran it with debugger function), but test only passes partly and in the browser it doesn’t work at all.
Here is my code, I have a different approach:

// Drum Arrays
let kicks = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
let snares = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
let hiHats = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]
let rideCymbals = [false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false]


function toggleDrum(arrayName, indexNumber){
  arrayName[indexNumber] = !arrayName[indexNumber]
};

function clear(arrayName){
arrayName.forEach(function clearEach(part, index, array){
        array[index] = false;
        },arrayName)};

function invert(arrayName){
arrayName.forEach(function invertall(theArray, index, array){
        array[index] = !array[index];
    }, arrayName)};

For the presetHandler, I have similar problems. Code works in Visual Studio, passes some tests and doesn’t work at all in the Browser.

// Use this presets array inside your presetHandler
const presets = require('./presets');

// Complete this function:
const presetHandler = (requestType, arrayIndex, newPresetArray) => {
    let correct = [200]
    let incorrect = [400]
    
    if (arrayIndex < 17 && arrayIndex >= 0 && Number.isInteger(arrayIndex)){
        if (requestType === 'GET'){
            correct.push(arrayIndex)
            return correct}
        else if (requestType === 'PUT') {
            correct.push(newPresetArray)
            return correct
        } 
    else {return incorrect} 
}};


// Leave this line so that your presetHandler function can be used elsewhere:
module.exports = presetHandler;

Anyone feedback regarding the Code?

Thanks & best regards, really enjoying this course

Basically, if you look in the test code, they identify each array with strings so that when called up by a string like ‘kicks’, it would call the kicks array anyway.
I don’t understand why they implemented this in their code, it makes no sense to me (as far as I can understand it now)

I don’t want to spoil the solution if you’re not there yet.
I was lost, so I peeked.

const getDrumArrayByName = (name) => {
switch (name) {
case ‘kicks’:
return kicks;
case ‘snares’:
return snares;
case ‘hiHats’:
return hiHats;
case ‘rideCymbals’:
return rideCymbals;
default:
return;
}
};

And then use the getDrumArrayByName(name) function inside the other functions you create, like the toggleDrum, clear etc

1 Like

I also peaked at the solution at some point, it would be interesting, why my code worked in Visual Studio Code, but not in the browser. I guess they had this very specific design in mind, and the other code relates to it.