简体   繁体   中英

JavaScript indexOf within a for loop

Problem

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?

Script

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;
    }
  }
 }
}

Result

mutation(["hello", "hey"]);
true //should return false

arr = ["hello", "hey"];
arr[0].indexOf(arr[1][2]);
-1

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.

There is a typo in your code!

//This condition will always be true!
// Change 'legth' to 'length'
if (count.legth == lower.legth) {
  return true;
}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM