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"]);
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.
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!