Mysterious organism, mutate only works on first call

I’m working on the mysterious organism project https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-challenge-project-mysterious-organism/projects/mysterious-organism
step 4.

I have created a mutate function and when I call it it works as intended but to make sure it really works i call it again . When I call it a second time the random index is always 0 and it doesn’t mutate the array. Is it some weird quirk with JS I don’t know about or is it (the way more likely option) something wrong with my code?

full code:

//test aBase = ['A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C', 'G', 'A', 'T', 'C'] // Returns a random DNA base const returnRandBase = () => { const dnaBases = ['A', 'T', 'C', 'G'] return dnaBases[Math.floor(Math.random() * 4)] } // Returns a random single stand of DNA containing 15 bases const mockUpStrand = () => { const newStrand = [] for (let i = 0; i < 15; i++) { newStrand.push(returnRandBase()) } return newStrand } const pAequorFactory = (specimenNum, dna) => { return { specimenNum, dna, mutate() { const randIndex = Math.floor(Math.random()*dna.length); console.log('random index right after calling is ' + randIndex) let base = dna[randIndex]; let randBase = returnRandBase(); console.log('randbase before while is ' + randBase) while (base === randBase) { randBase = returnRandBase(); } dna = dna.splice(randIndex, 1, randBase); /*if (randBase != base) { dna = dna.splice(randIndex, 1, randBase); } else { this.mutate(); }*/ console.log('randbase after while is ' + randBase) console.log('random index is ' + randIndex); } } } const model1 = pAequorFactory(1, aBase); console.log(model1); model1.mutate(); console.log(model1); model1.mutate(); console.log(model1);
    mutate() {
        const randIndex = Math.floor(Math.random()*dna.length);
        console.log('random index right after calling is ' + randIndex)
        let base = dna[randIndex];
        let randBase = returnRandBase();
        console.log('randbase before while is ' + randBase)
        while (base === randBase) {
            randBase = returnRandBase();
        }
        dna = dna.splice(randIndex, 1, randBase);
    }

The function in question:

        mutate() {
            const randIndex = Math.floor(Math.random()*dna.length);
            let base = dna[randIndex];
            let randBase = returnRandBase();
            while (base === randBase) {
                randBase = returnRandBase();
            }
            dna = dna.splice(randIndex, 1, randBase);
        }

If you run the codebyte you see the problem. First it prints the original dna (defined at the beginning of the code), then it prints the mutaded dna. After that I call mutate once more and print it. Thats where the problem is.

Hi,

Instead of logging the index, log dna.length: console.log('dna.length right after calling is ' + dna.length)
This logs 1 after second run.

That’s because of this line:

From the MDN docs:

Return value
An array containing the deleted elements.

You don’t need to reassign dna after applying the splice() method. .splice() mutates the array.

Thank you!
Computers are so unforgiving. This forum is very helpful with finding those mistakes I fail to figure out myself :slight_smile:

1 Like