I understand why it’s happening but can’t figure out a better way to fix this.
Can you please give me some suggestion to improve my way of thinking?
You get wrong results because you count the same letter every time it appears.
In the voodoo example the count is going to be 4 (4 x “o”), so it is larger than length of “no”.
I suggest you keep track only of one variable for example “result” that is initialized to true. If you find a letter that doesn’t exist then you change it to false.
First of all, try not to mix var and let, if you haven’t got really good reason to use var just use let and const
Back to the logic of your function. Take a look at this example:
const animals = ['dog', 'horse', 'cat', 'cat', 'zebra'];
function isThereCat(arr) {
// Initially the result is false because we assume that there
// will be no cat
let result = false;
for (let i = 0; i < arr.length; i++) {
if (arr[i] === 'cat') {
// If we find the cat, then we set the result to false
result = true;
}
}
return result;
}
What I’m trying to say is that you don’t need the “count”. In the example function the fact that there are two cats doesn’t change the final result.
The same applies in this challenge. We only care if all the letters from one word can be found in another. So as soon as we find one letter that doesn’t exist in another, we know that the function should return false.
function mutation(arr) {
function helper(a,li){
let result = false;
for(let i=0; i < li.length; i++){
if(a === li[i]){
result = true;
}
}
return result;
}
const one = arr[0].toUpperCase().split("");
const two = arr[1].toUpperCase().split("");
let result = true;
for(let i= 0; i < two.length; i++){
if(helper(two[i],one) !== true){
result = false;
}
}
return result;
}
Thank you so much. It was a great lesson. It’s cleaner
function mutation(arr) {
const one = arr[0].toUpperCase().split("");
const two = arr[1].toUpperCase().split("");
let result = true;
for(let i= 0; i < two.length; i++){
if(one.includes(two[i]) !== true){
result = false;
}
}
return result;
}