Implement the Mutations Algorithm

Tell us what’s happening:

There’s an error in the console that say’s “TypeError: “i” is read-only” but I don’t see where it’s trying to change i other than in the loop declaration.

Your code so far

function mutation (genes) {
  // Sep stands for seperated, I did this for the alphabet test.
  let genesSep = genes[1].split("");
  // Len stands for length to check if all characters and in the first string
  let correctLen = 0;
  console.log(genesSep);
  for (const i = 0; i < genesSep.length; i++) {
    console.log(i)
    if (genes[0].toLowerCase().includes(genesSep[i].toLowerCase())) {
      correctLen++;
      // To check if all characters are in the second string.
      if (correctLen.length == genesSep.length) {
        return true;
      }
    } else {
      return false;
    }
  }
  
}

console.log(mutation(["hello", "hey"]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0

Challenge Information:

Implement the Mutations Algorithm - Implement the Mutations Algorithm

But in your loop, you’ve declared i as a constant. Can you change the value of a constant?

why would a loop be a an exception?

No it can’t be changed.

The thing I can’t figure out is where I’m trying to change it, if I remember right, it makes an exception for the declaration (not sure if that’s the right word, what I mean when I say that is the part after “for“:

).

And to answer @ILM ‘s question, I don’t actually know, I just thought it said that in one of the theory documents.

here you are changing i

also you are thinking of for of and for in loops, they behave differently from for loops

So that does apply, but not to this kind of loop?

you can do for (const word of words) because each const word is in a different scope

the scopes in a for loop work differently and the i is always the same variable, so you are doing const i = 0; i++ which you can’t do

1 Like

Ok, that solved the problem, thank you.

That fixed the first test but I am having problems with the other ones.

It is reaching the full length and I’m using .toLowerCase so I don’t know why it’s not returning true.

console.log(`CL: ${genesSep.length} ?CL: ${correctLen}`);
      // To check if all characters are in the second string.
      if (correctLen.length == genesSep.length) {

What you print here is not what you are checking

1 Like

How so? They’re in a different order but it’s the same thing isn’t it?

Oh I see, it’s because in the If statement it’s checking correctLen.length instead of correctLen.

1 Like