Mutations: Basic Algorithm Scripting

Hey guys! I hope you are all doing great. can anyone help me with this? it works on all tests but not the first test! why?

function mutation(arr) {
  let target = arr[0];
  let test = arr[1];
  for (let i = 0; i < test.length; i++) {
    let elem = new RegExp(test[i], "i")
    let result = elem.test(target);
   // console.log(result)
    if (result === true) {
      return result
    } else {
      return false;
    }
  }
}

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

please post a link to the challenge

when I copied your code into the challenge editor, I immediate was presented with this syntax error

SyntaxError: unknown: Unexpected character ‘“’. (5:31)

you should fix this first before trying to debug

edit: nevermind. I think it is happening because your code above was not posted in a code block.

I’ve edited your code 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 (').

when you add a console.log statement to see the test[i] before you compare it, (such as in the code below), you can see that you are comparing h and then returning true immediately after.

Do you think this logic makes sense? (to compare only the first character and if it is found quit immediately?)

function mutation(arr) {
  let target = arr[0];
  let test = arr[1];
  for (let i = 0; i < test.length; i++) {
    console.log(test[i]);
    let elem = new RegExp(test[i], "i")
    let result = elem.test(target);
    console.log(result)
    if (result === true) {
      return result
    } else {
      return false;
    }
  }
}

Thanks for helping. Actually I had forgotten to remove it (I just wanted to check the character before if statement!).
And my problem is when I tested this code, All the tests got passed except the first one!

Thank you very much for spending your time to help me!

Please re-read my comment. I think you have missed the point.

I did read it! I got what your first paragraph means…
About your question my Answer is no! it doesn’t make sense!

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