Reputation: 1
I wrote a function which takes an array and returns true
if the string in the first element of the array contains all of the letters of the string in the second element of the array. The function worked as intended till ["hello", "hey"]
given as an argument. It should return -1
since the first element of the array doesn't contain "y"
. How do I solve this problem?
function mutation(arr) {
let lower = arr[0].toLowerCase();
let lower1 = arr[1].toLowerCase();
let count = 0;
for (let i = 0; i < lower.length; i++) {
if (lower.indexOf(lower1[i]) == -1) {
return false;
}
else {
count += 1;
}
if (count > 0) {
if (count.legth == lower.legth) {
return true;
}
else {
return false;
}
}
}
}
mutation(["hello", "hey"]);
true //should return false
arr = ["hello", "hey"];
arr[0].indexOf(arr[1][2]);
-1
Upvotes: 0
Views: 245
Reputation: 1
I did solve my problem based on the above suggested answers. Here is the new code.
function mutation(arr) {
let lower = arr[0].toLowerCase();
let lower1 = arr[1].toLowerCase();
let found = lower.match('[' + lower1 + ']', 'g');
if ((found.length > lower1.length || lower1.length > found.length)) {
for (let i = 0; i < lower1.length; i++) {
if (lower.indexOf(lower1[i]) == -1) {
return false;
}
}
}
return true;
}
Result
mutation(["hello", "hey"])
false
mutation(["hello", "Hello"])
true
mutation(["zyxwvutsrqponmlkjihgfedcba", "qrstu"])
true
mutation(["Mary", "Aarmy"])
true
mutation(["floor", "for"])
true
mutation(["voodoo", "no"])
false
Upvotes: 0
Reputation: 1337
You have to loop over lower1 inspite of lower because the length of lower and lower1 are different so cannot find lower1[3] and greater.
function mutation(arr) {
let lower = arr[0].toLowerCase();
let lower1 = arr[1].toLowerCase();
let count = 0;
for (let i = 0; i < lower1.length; i++) {
if (lower.indexOf(lower1[i]) == -1) {
return false;
}
}
return true;
}
console.log(mutation(["hello", "hey"]));
console.log(mutation(["hello", "hell"]));
Upvotes: 0
Reputation: 100
First, you have misspelled "length"
if (count.legth == lower.legth)
Secondly, the count variable is not necessary here. If you don't want the first word to be shorter than the second just add a check at the beginning:
if(lower.length < lower1.length)
Third, what you are trying to do according to you is check if the first element contains all the characters of the second element, but you are doing here is actually completely the opposite!
I've changed the code for a bit and corrected it to check for the correct thing.
function mutation(arr) {
let first = arr[0].toLowerCase();
let second = arr[1].toLowerCase();
for (let i = 0; i < first.length && i < second.length; i++) {
if (second.indexOf(first[i]) == -1) {
return false;
}
}
return true;
}
This code doesn't handle some edge cases (like "b" and "baaa") but if you want to handle cases like this you will have to go for a different approach.
Let me know if you need any further help.
Upvotes: 1
Reputation: 5189
There is a typo in your code!
//This condition will always be true!
// Change 'legth' to 'length'
if (count.legth == lower.legth) {
return true;
}
Upvotes: 0