4

I'm not sure what I'm doing wrong here. The first instance that I use indexOf it works perfectly fine, but when I use it the second time it's not returning the result that I'm expecting.

function mutation(arr) {
    //return arr;
    res = "";
    for (var x=0; x<arr[1].split("").length; x++) {
        if (arr[0].indexOf(arr[1].split("")[x]) !== -1) {
            res += "t";
        } else {
            res += "f";
        }
    }
    // res = ttt
    if (res.indexOf("f") !== -1) {
        return true;
    } else {
        return false;
    }
}

mutation(["hello", "hey"]);
// this returns true instead of false
mutation(["floor", "loo"]); 
// returns false instead of true

mutation should return false if an element from arr[1] is not present in arr[0] else return true.

6
  • what exactly you want to do? Commented Nov 22, 2015 at 5:27
  • I just pasted your code into the JS console and I got true as well Commented Nov 22, 2015 at 5:28
  • If you are using IE8 and olders, .indexOf() doesn't be supported. Reference link: stackoverflow.com/questions/3629183/… Commented Nov 22, 2015 at 5:32
  • im using chrome and firefox. Commented Nov 22, 2015 at 5:32
  • @splucena check out my edited answer. The problems with your code are outlined there. You had an off by 1 error and a negated indexOf check. Commented Nov 22, 2015 at 5:41

4 Answers 4

3

your code isn't working because when you say:

res.indexOf("f") != -1

this means: "I found an f", but you're treating it as if it means "I did not find an f".

In your case that you want to return false if you find an 'f', but you're returning true. Flip your true and false cases:

if (res.indexOf("f") != -1) {
   return false;
 } else {
   return true;
 }

ALSO your for loop is wrong because x starts at 0, so you need to go to < length not <= length of your string.

for (var x=0; x < arr[1].split("").length; x++) {

and now your code works as you wanted it to.

Sign up to request clarification or add additional context in comments.

2 Comments

or better yet: return !res.indexOf("f")
@Macmee You're right I patterned my thinking on the first time I use indexOf and expected it to return true when I'm looking for false.
2

Just edited your code. Click on the <p> to check:

function mutation(arr) {
  //return arr;
  res = "";
  for (var x=0; x< arr[1].split("").length; x++) {
    res += arr[0].indexOf(arr[1].split("")[x]) > -1 ? 't' : 'f';
  }
 return res.indexOf('f') > -1;
}

$('p').click(function(){
  alert(mutation(["hello", "hey"]));
  alert(mutation(["floor", "loo"]));
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Click me</p>

Comments

0

If you simplify the logic a bit, that's easier to check:

function mutation(arr) {
  return arr[1].split('').reduce(function(res, x) {
    return arr[0].indexOf(x) >= 0;
  }, true);
}

Thanks Leon for the correction.

7 Comments

does using true && condition have any benefit?
@LeonAdler Compared to what?
This is working. Can you spot what's wrong with my code?
compared to just condition - return arr[0].indexOf(x) >= 0
@LeonAdler haha! Of course, thanks man. That was stupid of me.
|
0

I tried to not chance your logic, the mistake are:

  • You're trying to compare with all characters on the array[0], not only the first.
  • If you find a character equals on the first character on array[0] you should return true.

Correct code:

function mutation(arr) {
  res = "";
  for (var x=0; x<=arr[1].split("").length; x++) {
    if (arr[0].split("")[0].indexOf(arr[1].split("")[x]) !== -1) {
      return true;
    }
  }
  return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.