Basic Algorithm Scripting - Mutations

Tell us what’s happening:
the code is not passing the tests for:
mutation([“hello”, “Hello”]) and mutation([“floor”, “for”])
i really want to use only for loops, and not the indexOf function.

Your code so far

function mutation(arr) {
  let indexer = 0;
  let first = arr[0].toLowerCase();
  let second = arr[1].toLowerCase();
  for (let i = 0; i < second.length; i++){
    for(let j = 0; j < first.length; j++){
      if(second[i] === first[j]) indexer++;
    }
  }
  return (indexer == second.length);
}

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/109.0.0.0 Safari/537.36

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

Let’s look into this case

Your indexer will increment 4 times here:
1 for letter f, 1 for letter r - that’s all good
but for letter o there will be 2 incrementations, because this letter occurs 2 times in the floor word.

4 is not equal 3(length of for word), so your function will return false

Similar story with

here also will be extra incrementations, andindexer will be 7 instead of 5

solved it!

function mutation(arr) {
  let indexer = 0;
  let first = arr[0].toLowerCase();//voodoo
  let second = arr[1].toLowerCase();//no
  for (let i = 0; i < second.length; i++){
    let last = 0;
    let isExist = false;
    for(let j = 0; j < first.length; j++){
      if((second[i] === first[j]) && (last != first[j])){
        indexer++;
        isExist = true;
      }
      last = first[j];
    }
    if(!isExist) return false;
    isExist = false;
  }
  return (indexer == second.length);
}

mutation(["hello", "hey"]);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.