简体   繁体   中英

Need help understanding why this works

This function checks if a word is an isogram, meaning that there are no duplicate letters (not case sensitive). I understand why I have to change it to lowercase and iterate through the string, but I got confused on this part: "if(splitt.indexOf(splitt[i]).== i)"? Why do I have to check the index of, Maybe. I am confused on what this line of code is trying to say. Can someone help explain this line of code. Thank you!

function isIsogram(str) {
  //turn input string into all lowercase
   //console.log(str.toLowerCase());
  var lower = str.toLowerCase();
  // split lowercase string
    //console.log(lower.split(''));

  var splitt = lower.split('');

  //iterate through split string [A, l, g, o, r, i, s, m]
  for(var i = 0; i < splitt.length; i++){
   // console.log(splitt.indexOf(splitt[i]) !== i);

if(splitt.indexOf(splitt[i]) !== i){
  return false;
  }
 }
 return true;
// if we have encountered current element within srting again
  //return false
//otherwise
  //return true



}


var output = isIsogram("AlgoriSsm");
console.log('should be false:', output);

Array.prototype.indexOf() behaves in a very specific way:

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

The loop in which the call to indexOf() sits iterates over each character in the string one-by-one. The check here is to make sure when it encounters a particular character that same character does not appear at another (lower) index.

For example, take the string: juggling . Your loop will effectively perform the following steps:

juggling
^        --> character at index 0 is 'j'
         --> 'juggling'.indexOf('j') === 0 - no 'j's before this one
juggling
 ^       --> character at index 1 is 'u'
         --> 'juggling'.indexOf('u') === 1 - no 'u's before this one
juggling
  ^      --> character at index 2 is 'g'
         --> 'juggling'.indexOf('g') === 2 - no 'g's before this one
juggling
   ^
         --> character at index 3 is 'g'
         --> 'juggling'.indexOf('g') !== 3 - there's a 'g' before this one - not an isogram!
             --> return false as a result

In short, your logic is basically iterating over an array of characters, and for each character (at a particular index in array) its checking if 'first occurrence' of that character is not same as current index. If the first occurrence is anything else than current index, the character must have appeared somewhere else (before this index).

The whole logic goes like this:

  1. Convert the string to lower case (to make the logic case insensitive).
  2. Split the string in array of characters.
  3. For every index i get character c ( lower[i] ) at index i .
  4. Get first index of c in the array using indexOf function.
  5. If first index of c is not i , then c must have appeared before i at some point. Return false.
  6. If the above condition didn't match for any index, return true.

BTW, a considerably more efficient solution for this problem can be achieved by using a Map (or Object in JS).

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