Method being called without invocation

Hello, this is in reference to this challenge https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-22-javascript-syntax-part-ii/modules/wdcp-22-mysterious-organism/projects/mysterious-organism

the mutate method is affecting the instance of the object before it’s even invoked, any insight as to why?

// Returns a random DNA base const returnRandBase = () => { const dnaBases = ["A", "T", "C", "G"]; return dnaBases[Math.floor(Math.random() * 4)]; }; // Returns a random single strand of DNA containing 15 bases const mockUpStrand = () => { const newStrand = []; for (let i = 0; i < 15; i++) { newStrand.push(returnRandBase()); } return newStrand; }; const pAequorFactory = (specimenNum, DNAarr) => { return { _specimenNum: specimenNum, _dna: DNAarr, mutate() { const randomNum = Math.floor(Math.random() * this._dna.length); const newBase = returnRandBase(); if (this._dna[randomNum] === newBase) { return this.mutate(); } else { console.log(randomNum, newBase); this._dna[randomNum] = "newBase" //here i would just use the variable instead; return this._dna; } } }; }; const exOne = pAequorFactory(1, mockUpStrand()); console.log(exOne); console.log(exOne.mutate());

.mutate only affects the object when .mutate is called

1 Like