I can’t reply to the hint post so I think I will ask here.
My code works for most of the tests in this challenge.
Try pasting my code and look through the tests and it’s really weird.
It passed :
mutation([“Mary”, “Aarmy”])
mutation([“hello”, “hey”])
But it won’t pass :
mutation([“Alien”, “line”])
You know what’s funny?
Change the “neo” in the last line to “leo” and you would get
TypeError: Cannot read property ‘toLowerCase’ of undefined
…
What?.. I’m banging my head over the wall for more than 2 hours now. Don’t know why it doesn’t work.
I have moved your post to it’s own topic.
Whenever you have questions about a challenge it is always best to create a new post and people can assist you there.
Hi @jwilkins.oboe !
Thanks for the reply.
I actually can get it to work after looking at the solution.
Javascript was so weird. Sometimes string can act like array.
Anyway, I’m really curious about the “TypeError: Cannot read property ‘toLowerCase’ of undefined” part.
Could you please explain that too?
function mutation(arr) {
// This condition will convert your second string to an array EVERY LOOP ITERATION
// Why not just convert to a lowercase array once?
for (let i = 0; i < arr[1].toLowerCase().split("").length; i++) {
// This condition will convert your first string to an array EVERY LOOP ITERATION
// Why not just convert to a lowercase array once?
for (let j = 0; j < arr[0].toLowerCase().split("").length; j++) {
// j is over the length of arr[0].... so you shouldn't index into arr[1] based on j
// Also, you shouldn't keep repeating the conversion to lower case
if (arr[0].toLowerCase().indexOf(arr[1][j].toLowerCase()) == -1)
return false;
}
}
return true;
}
mutation(["hello", "neo"])
My answer for this challange look like almost exactly like 1st solution given by FCC, depend on the problem i think before jump to main code, sometimes better to make abstraction first, made your code more clear and easier to read.
When I was doing mine, I converted the two input words to arrays, then defined the empty “target array”. It worked, overall… Want to make it a little bit more compact, at some point…
for (let i = 0; i < sndWrdLowC.length; i++) {
if (fstWrdLowC.includes(sndWrdLowC[i])) {
trueArr.push(sndWrdLowC[i]);
} else {
if (sndWrdLowC !== trueArr)
return false;
}
Please look at your code again and ask yourself - what are you doing and what is the actual goal?
Here is a hint: You only need one for-loop and no nesting
Did you read my comments in your code? The comment about j vs i is exactly the cause of the type error. Bad indexing leads to undefined values, which leads to that specific type error.
Yes, I did read it.
The “neo” and “leo” is same in length that’s why I asked.
With the “neo” you would not get the Type error.
Sorry, may be I was just too much skeptical.
With "neo", the very first character is not matching, so the indexing problem is not apparent. All of the characters in "leo" match but "hello" is longer, which is why you get the indexing problem. Your indexing must be using the actual length of the string you are indexing into.