Mutations Lesson quesiton

Hey guys,
my question is Hello and hello returns true for me.
but for example the elements in the code, not returning true.
Do I have to sort those letters out? that is my question.

  **Your code so far**

function mutation(arr) {
const loweredString = arr[0].toLowerCase();
const anotherLoweredString = arr[1].toLowerCase();
if (loweredString.includes(anotherLoweredString)) {
  return true;
}
else {
  return false;
}

}

mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"]);
  **Your browser information:**

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

Challenge: Mutations

Link to the challenge:

You have to check for every single letter - as the order doesn’t matter. It only matters, that all letters are present.
.includes() however checks if the exact order is present, hence it won’t work here.

1 Like
function mutation(arr) {
  const loweredString = arr[0].toLowerCase();
  const anotherLoweredString = arr[1].toLowerCase();
  for (let i = 0; i < loweredString.length; i++) {
    if (anotherLoweredString.indexOf(loweredString[i]) !== -1) {
      return true;
    }

  }
  return false;

}

mutation(["Alien", "line"]);

hey again, might assist me why I am not passing ;p?

(Remember we try not to just give out answers) What can you observe is going wrong by adding some console.logs to debug it?

is it possible because the loop goes 1 at a time, and I need to connect them by the end of it?

Let’s go through the steps of that example above mutation(["Alien", "line"]) and plop in all the variables:

const loweredString = "Alien".toLowerCase(); // "alien"
const anotherLoweredString = "line".toLowerCase(); // "line"
for (let i = 0; i < "alien".length; i++) {
  if ("line".indexOf("a") !== -1) { // this is false so let's tick up `i`
  if ("line".indexOf("l") !== -1) { // this is true
    return true; // bye!

Do you see how that’s kinda weird? It returns true after finding only one matching letter.

Yeah thats what I thought, since it searches for only 1.

so i am thinking maybe i need to do another if, if all of them are connected or sometihng

Nah, don’t overthink it, you are pretty close already.
Think about how you’d do it by hand: you have to check every letter of the second string. When is the earliest point you can say they are “all” contained? When is the earliest point you can say they are “not all” contained?

1 Like

Thanks, I have managed to complete it last night I am at the next one now.

1 Like

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