+ 1
Not repeated character JS
Someone explain me this, like how is this possible! I'm literally tired understanding this. Point of interest, how indexof and lastindexof getting === to each other and what's the flow of code. function notRepeatedChr(x){ console.log(`not repeated character is ${x.split('').find(y => x.indexOf(y) === x.lastIndexOf(y)}`); } notRepeatedChr("hello world")
1 Antwort
+ 4
Here s the logic:
-IndexOf returns you the index of the first character from the array it finds that is "y".
-lastIndexOf returns you the index of the last character from the array that is "y".
-If the character does not repeat, then the index is the same.
"Next" index of 'e" is 1, last undex of "e" is also 1. So no repeat.
"Welcome" index of e is 1, last index of "e" is 6, so it repeats.