Basic Algorithm Scripting - Mutations

Tell us what’s happening:

Hello, I’m currently trying to get it to work, I’m changing it into lowercase to make it easier to check, but it’s still not working.

Your code so far

function mutation(arr) {
  let lowerArrOne = arr[0].toLowerCase();
  let lowerArrTwo = arr[1].toLowerCase();
  if(lowerArrOne[0].indexOf(lowerArrTwo) == -1) {
    return false
  }
  return true;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Challenge Information:

Basic Algorithm Scripting - Mutations

Hi @chetanzeogu410

What does the following code return from your function?

console.log(mutation(['elloh', 'hey']));

Happy coding

Logged it, it returns false

Changing it to lowercase is a good start. but there are other flaws in your current logic.

Here is another way to look at it.

Currently you have this

So if we take the example console.log provided to you, you are basically trying to do this

"e".indexOf("hey")

That says find the position where the literal string "hey" can be found in the string "e"

Well, "hey" is not present in the string "e" so it will return false.

The core issue with your approach is that you are not writing correct logic for this part of the problem statement

Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array.

Right now, you are not checking if all letters in string two are in string one. You are only checking once and then returning true or false.

My advice to you would be to use pen and paper and use pseudo code to design an algorithm to solve the problem.

Don’t worry about JavaScript syntax or methods at this stage. The goal of this stage is to understand the problem first with plain english step by step algorithm in pseudocode.

Once you have designed your algorithm, then test your algorithm (still on pen and paper) against the test cases they gave you. Do your algorithm work correctly? Or are there flaws?

Once you know your algorithm is working, THEN you code it out.

Once you truly understand, how to solve the problem, then you will find that writing the code will be easier to grasp.

That is how you should approach each of these problems. Because the goal is to strengthen your problem solving skills, THEN worry about implementation details.

Hope that helps.

2 Likes

Got it, thank you for the advice!