Just wondering if its acceptable to use a for in loop on a string. Not sure if there could be weird results or bad practice but my solution works at least in this example.
Coding practice question. Also, if anyone has a way to improve my solution I'm open to advice.
function firstNonRepeatingLetter(str) {
const lowerStr = str.toLowerCase();
for (let char in lowerStr) {
if (lowerStr.lastIndexOf(lowerStr[char]) === parseInt(char) &&
lowerStr.indexOf(lowerStr[char]) === parseInt(char)) {
return str[char];
}
}
return "";
}
Write a function named first_non_repeating_letter
that takes a string
input, and returns the first character that is not repeated anywhere in the string.
Examples:
firstNonRepeatingLetter('a') => 'a'
firstNonRepeatingLetter('stress') => 't'
firstNonRepeatingLetter('sTreSS') => 'T'