Mutations-want to solve using if-else condition

Mutations-want to solve using if-else condition
0.0 0

#1

Tell us what’s happening:
For solving this problem can I use if else condition and satisfy all the cases? Please tell me I need help.

Your code so far

function mutation(arr) {
  var newArray=[];
   for(var i=0;i<arr.length;i++)
     {
       newArray.push(arr[i].toLowerCase());
     }
  
  
      if(newArray[0].indexOf(newArray[1]))
      {
        return true;
      }


    else if(newArray[0]!==newArray[1])
      {
        return false;
      }

  
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36.

Link to the challenge:


#2

I am not sure what all of the test cases are and I am seeing that your mutation() function is lowercasing both string values and then comparing them, once with an interesting way of using indexOf(). I think that condition will always return a false, being that 0 or -1 are the two possible returned values and which both return “falsey” in an if() condition.
For info on indexOf() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
For info on conditionals https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else


#3

You can use if statements, but you need to think about what your are comparing. Your code has two main problems.

#1) You need to check if each letter in newArray’s 2nd element is in newArray’s 1st element. You can use indexOf, but not the way you are using it. indexOf returns an integer -1 through the length of the array minus 1. The number return indicates the first index (left to right) where the supplied argument is found. If it can not find it, it will return -1.

Your current logic is not checking each letter of newArray’s second element. Using the test case shown in your post, The following if statement:

  if(newArray[0].indexOf(newArray[1])) {

asks if (“hello”.indexOf(“hello”) ? “hello”.indexOf(“hello”) evaluates to 0 because “hello” is found at “hello” 0 index. So you are asking if (0), so this evaluates to false, so the following else if is evaluated:

else if(newArray[0]!==newArray[1]) {

The above asks if “hello” is Not equal to “hello”, which evaluates to false.

Neither of the return statements return anything, so the function ends and returns undefined.

You need to review how to properly use indexOf and all make sure you are looping over each letter in the array’s second element.


#4

A post was split to a new topic: Mutations - can not test one test