Why does this code not return the false

*/
function mutation(arr) {

  var a = arr[0].toLowerCase();

  var b = arr[1].toLowerCase().split("");

  var c = 0;

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

   if(a.includes(b[i])){

     c++;

     return true;

   }

 } else return false;

}
*/
1 Like

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.


I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

When your code is properly indented, you should be able to see the else statement is out of place:

function mutation(arr) {
  var a = arr[0].toLowerCase();
  var b = arr[1].toLowerCase().split("");
  var c = 0;
  for (var i = 0; i <= b.length; i++) {
    if (a.includes(b[i])) {
      c++;
      return true;
    }
  } else return false;
}

Moving the else statement code block will stop the error message you are seeing in the FCC console, but will not solve your logic issue concerning the problem.

I notice you initialize a c variable and increment it within the for loop, but what is its purpose?

2 Likes

Hi randall,

Thank you for the feedback, i have altered the issue with the ‘else’ statement and moved it one line up. Also I have deleted the variable c as i can see that it has no purpose. The code now passes all of the tests except from the first one and i am perplexed as to why if all the others are working fine.

The new code reads:

function mutation(arr) {
  var a = arr[0].toLowerCase();
  var b = arr[1].toLowerCase().split("");

 for(var i = 0; i <= b.length; i++) {
   if(a.includes(b[i])){
     return true;
   } else return false;
 } 
}
  
mutation(["hello", "hey"]);
1 Like

Try logging a[i] and b[i] inside the loop at the top.

Summary
function mutation(arr) {
  var a = arr[0].toLowerCase();
  var b = arr[1].toLowerCase().split("");

 for(var i = 0; i <= b.length; i++) {
   console.log('a[i] ===', a[i])
   console.log('b[i] ===', b[i])
   if(a.includes(b[i])){
     return true;
   } else return false;
 } 
}

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

Edit: Also, please include the link to the challenge when asking for help:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.