.indexOf refuses to stop confusing me

Ternary operator isn’t necessary in this case since it always return true or false, for example you can do :

function isEquals4(num) {
    return num == 4;
}

That’s what @codefu-chivy said.

1 Like

Was just providing an alternative :slight_smile:

1 Like
 var first = arr[0].toLowerCase();
 var second = arr[1].toLowerCase();
 var c = "false";
  for ( var i = 0; i < second[1].length; i++ ) {
   
   c = first.indexOf(second[i],0) !== -1;
   
    if ( c == false ) {
     break;
  
    }else{
      c = true;
    }
    }
     return c;
    }

mutation(["hello", "hey"]);```
1 Like

I seem to have a serious misunderstanding of this problem, still get all but one test to pass. Also, the backtick thing doesn’t seem to work…

1 Like

The triple backticks should be in their own lines. Like:

```
  code goes here
```
2 Likes
function mutation(arr) {
 var first = arr[0].toLowerCase();
 var second = arr[1].toLowerCase();
 var c = "false";
  for ( var i = 0; i < second[1].length; i++ ) {
   
   c = first.indexOf(second[i],0) !== -1;
   
    if ( c == false ) {
     break;
  
    }else{
      c = true;
    }
    }
     return c;
    }

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

Hi

var c = "false";

you probably mean

var c = false;

for ( var i = 0; i < second[1].length; i++ ) {

you probably mean

for ( var i = 0; i < second.length; i++ ) {

You want to loop over the characters in the first string and for each one, you want to check it against every character in the second string - is that right? In that case, you will probably need two loops. Hope that helps a bit.

1 Like

I didn’t catch the “false” mistake, thank you. Once i posted the code, I saw the [1] mistake, after removing it, the tests all passed. Yea, I have to make sure all the letters in “second” are present in “first” and return true or false.

1 Like