Mutations Help Please! :/

Mutations Help Please! :/
0

#1

My code works fine except for the case at the bottom of my code, can someone please tell me why? It will be greatly appreciated!! :smiley: function mutation(arr) {
var newArray = [];
var answer;

for (var i = 0; i < arr.length; i++) {
newArray.push(arr[i].toLowerCase());
}

for (var j = 0; j < newArray[1].length; j++) {
if (newArray[0].indexOf(newArray[1][j]) === -1) {
answer = false;
}

else if (newArray[0].indexOf(newArray[1][j]) === 0) {
  answer = true;
}

}

return answer;
}

mutation([“zyxwvutsrqponmlkjihgfedcba”, “qrstu”]);


#2

I call this kind of problem the “find a counterexample” problem (made that up just now). In these kinds of problems you start with the assumption that what you get is true. Then you proceed with examining your inputs to find just one counterexample. Once you have a counterexample, you now get false.

Typically, in code this translates to just an if-block, with no matching else block.


Check for Palindromes - 1 eye for of 1 eye help!
#3

Thanks! That explains why setting answer to true gets it to work! But why doesn’t it work without doing so? Wouldn’t it be set to true in the for loop?


#4

Using your present code, let’s try, mutation("cats", "set").

You found that "s" is found in "cats", so you set answer = true.
You found that "e" is not found in "cats", sou you set answer = false. So far so good. This is the correct answer.
Then you found that "t" is found in "cats", so you set answer = true. You’ve just replaced the correct answer.

The loop completes its work and now you have answers = true, which is definitely not true, The fate of answer lies at the last loop iteration.


#5

Ohh I see! Well setting answer to true at the top of my code. Why is that? Would you recommend adding a condition so that the loop ends when answer is set to false? Thank you so much for your responses.


#6

You can add break; after setting the variable to false, or better yet…

…instead of using a variable to store true or false, you can return false early once the condition is met. If it’s not met at the end of the loop, then you can return true.

function mutation(arr) {
  // ...
  for (...) {
    if (...) {
      return false;
    }
  }
  return true;
}

#7

I tried the latter method you gave (getting rid of the variable answer) but it did not work for the first case mutation(“hello”,“hey”). Any ideas as to why? The following is my new code: function mutation(arr) {
var newArray = [];

for (var i = 0; i < arr.length; i++) {
newArray.push(arr[i].toLowerCase());
}

for (var j = 0; j < newArray[1].length; j++) {
if (newArray[0].indexOf(newArray[1][j]) === -1) {
return false;
}

return true;

}

}


#8

You were returning true inside the for-loop. return true; should be outside, after the loop completes.


#9

Nevermind! I know why. My return true was inside of the loop.


#10

Thank you so much! :smiley: :smiley:


#11

Thank you both! :slight_smile: I’m going through it now and this post has been most enlightening! You guys rock!