Basic Algorithm Scripting - Mutations

Tell us what’s happening:
My code pass all the tests exept for this one
// running tests

mutation(["Mary", "Aarmy"])

should return

true

. // tests completed // console output false i need to know way it did not push the index of the letter y to the letters array
Your code so far

function mutation(arr) {
  let string1 = [],
      string2 = [],
      letters = [];
  for (let i=0; i<arr[0].length; i++) {
    string1.push(arr[0][i].toLowerCase());
  }

  for (let j=0; j<arr[1].length; j++) {
    string2.push(arr[1][j].toLowerCase());
  }

  for (let k=0; k<string1.length; k++) {
    if (string1.includes(string2[k])) {
      letters.push(string1.indexOf(string2[k]));
    }
  }
  return letters.length === string2.length;
  
}

console.log(mutation(["Mary", "Aarmy"]));

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 11; Nokia 3.4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Mobile Safari/537.36

Challenge: Basic Algorithm Scripting - Mutations

Link to the challenge:

If you make one change to this line…

for (let k=0; k<string1.length; k++) {

…your code will pass.

You are checking each letter in one of the arrays to see if it appears in the other and then pushing index values to a third array so that you can ultimately check if the array lengths match. Just think about which array you should be iterating above.

Thanks i was stuck for hours thanks a lot

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