Implement the Mutations Algorithm - Implement the Mutations Algorithm

Tell us what’s happening:

I am trying to understand what to do next. I can do a inner loop to check if the words are equal but before that do I need to sort out letters in words

Your code so far

function mutation(pantary){
  //Does pantary have listOfItems
  for(let i = 0; i <= pantary[1].length-1;i++){
    console.log(pantary[1][i])
  }
}
mutation(['ab','ammar']);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36

Challenge Information:

Implement the Mutations Algorithm - Implement the Mutations Algorithm

Ok, so you have a loop that iterates over each letter of the 2nd string :white_check_mark:

Once you have the first letter a what do you want to check?

You can use an inner loop, what will it iterate over?

If the words are equal? I don’t think that’s what you want to check

Sort them as in alphabetical order? Why?

When I have the first letter matching same letter in 1st string. I will want to move to the next word in second string.
My thought process was sorting words like 'Alien to check with ‘line’ If I do

Aline → Aeiln (now sorted)
Line-> eiln (now sorted)

It will easier to check, but now I think one outer loop is enough e.g. I will take the first letter of 2nd string and look for it in 1st string, if found, move onto next one and so forth but if I don’t find any or one of them, break the loop.

For my breaking down problem is a problem and how to solve is hard like not the programming or what to use in programming but in general solving a problem.

1 Like

That’s really what programming is. Breaking a problem down so that you can solve it systematically / algorithmically. Then, you just implement that system in a language the computer understands.

there will be word pairs where you don’t have such neat arrangment of the letters in the alphabet, so you can’t do that to check

Not sure what you mean. Can you explain by what you mean

here will be word pairs where you don’t have such neat arrangment of the letters in the alphabet, so you can’t do that to check

I came to this after a while and I thought to do this

function mutation(arr1){
  for(let i = 0; i <= arr1[0].length - 1;i++){
    if('t' === arr1[0][i]){
      console.log(true)
    }else{
      console.log(false)
    }
  }
}
mutation(['localhost','tsohlacol']);

I thought to test it with hardcode letter, now the last one is true. Am I going in right direction?

don’t test it with hardcoded letters, it does not help to write the logic

try mutation(['localhost','c']);
try mutation(['localhost','v']);
and then mutation(['localhost','tc']);
and then mutation(['localhost','tcg']);
and then mutation(['localhost','tch']);
and then mutation(['localhost','tcih']);

the first should test true, the second should not, and so on

So I updated my code to take value from parameter, so right now it is like

function mutation(arr){
  for(let i = 0; i <= arr[0].length - 1;i++){
    if(arr[1] === arr[0][i]){
      console.log(true)
    }else{
      console.log(false)

    }
  }
}
mutation(['localhost','c']);

I tried using arr[1][i] but it will compare each letter with another and move on like
e.g. ‘localhost’, ‘tsohlacol’
l will check if it is equal to h
o will check if it is equal to s

but my thinking was that each word in the second array loop over each letter on first array to check, or am I thinking too much? and if so what can I do to help myself.

There is only 1 array. Do you mean each letter in the second word ? Then yes, that sounds like a good plan

1 Like