Using a regex to solve 'Basic Algorithm Scripting: Mutations'

Using a regex to solve 'Basic Algorithm Scripting: Mutations'
0

#1

This is my code:

function mutation(arr) {
let regex = /\w/i;
if (regex.test(arr)) {
return true;
} else {
return false;
}
}

mutation([“hello”, “hey”]);


I passed all of the tests except the following:

mutation([“hello”, “hey”]) should return false.
mutation([“hello”, “neo”]) should return false.
mutation([“voodoo”, “no”]) should return false.

How can I complete this challenge using a regex? Is it even possible?


#2

You can probably do it with regex but I think it’s gonna require a fair amount of time. Sounds like an advance regex to me hehe


#3

That’s not not really what regexes are for, and your code isn’t testing anything, it’s just accidentally working for some of the cases.

A regex allows you to match a pattern in a string, like /hello/.test('hello world') will return true, because the pattern hello is present in that string. It’s for testing if a specific pattern of characters in a specified order are present.

You are saying “does this array contain any single word character”, which doesn’t make sense because an array is not a string. But JS is converting the array to a string to enable it to run the regex, and so you are basically saying “does this string of word characters contain any single word character” which will always be true.

You are supposed to be testing if all the letters in the second word are present in the first; you aren’t doing that at the minute.