Mutations - can not test one test

Mutations - can not test one test
0

#1

Hello,

I post here but I don’t know if I’m supposed to open a new channel for my problem or jump in the post on the same challenge from another member.

I’ve tried many different approach but there is always something that doesn’t work …
The latest below has just one test that doesn’t pass, I don’t know why


function mutation(arr) {
  
  if (arr[0] > arr[1]) {
    for (i = 0; i < arr[1].length; i++) {
      if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) !== - 1) {
        return true;
      }      
      return false;
    }
  }
  
  if (arr[0] < arr[1]) {
    for (i = 0; i < arr[1].length; i++) {
      if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) !== - 1) {
        return true;
      }      
      return false;
    }
  } 
}

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

image


Mutations-want to solve using if-else condition
#2

Only post on someone’s thread if you are answering a question they had or feel some extra comments would help clarify something. When you have your own issues with a challenge, start a new topic.

In the following test case:

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

the first if statement evaluates to false, so we move to the second if block:

  if (arr[0] < arr[1]) {
    for (i = 0; i < arr[1].length; i++) {
      if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) !== - 1) {
        return true;
      }      
      return false;
    }
  } 

The if statement evaluates to true, so we enter the first iteration of the for loop and arrive at the following if statement:

 if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) !== - 1) {

The above evaluates to true and so your code returns true. Why? Because arr[1][i].toLowerCase() is “h”, so since arr[0].toLowerCase() is “hello”, the following:

arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase())

evaluates to 0, because “h” is located at index 0 of arr[0].toLowerCase() and 0 is not equal to -1.


#3

Hey sorry for late reply, I wasn’t home last day

If i understood your point, i’m comparing a string with a letter.
I’ve try to access arr[0][0] but it doesn’t work
I’ve tested with arr[0][i] but it doesn’t work too as I supposed

I’m also curious, when i return true in

 for (i = 0; i < arr[1].length; i++) {
      if (arr[0].toLowerCase().indexOf(arr[1][i].toLowerCase()) !== - 1) {
        return true;
}

Does the function goes though every value of i or does it just take i = 0 and return true or false ?
A friend told me that return end the function