Answer the question
In order to leave comments, you need to log in
How to solve a problem with codewars?
Hello guys. There is a task to find the first not the repetition. letter in a string.
I did so.
const firstNonRepeatingLetter = str => {
let charMap = {};
let strToLower = str.toLowerCase();
for (let char of strToLower) {
if (!charMap[char]) {
charMap[char] = 1;
} else {
charMap[char]++;
}
}
for (let char in charMap) {
if (charMap[char] === 1) {
return char;
}
}
return '';
};
sTreSS
and should return "T", and I have "t". Tell me what needs to be fixed? Answer the question
In order to leave comments, you need to log in
Well, if not rewriting the code, but adding something like this
const firstNonRepeatingLetter = str => {
let charMap = {};
let strToLower = str.toLowerCase();
for (let char of strToLower) {
if (!charMap[char]) {
charMap[char] = 1;
} else {
charMap[char]++;
}
}
for (let char in charMap) {
if (charMap[char] === 1) {
var i = str.indexOf(char);
if( i == -1)
i = str.indexOf(char.toUpperCase())
else if(str.indexOf(char.toUpperCase()) != -1 && str.indexOf(char.toUpperCase()) < i)
i = str.indexOf(char.toUpperCase());
return str.charAt(i);
}
}
return '';
};
function firstNonRepeatingLetter(str) {
for(var i = 0 ; i < str.length;i++)
if((str.substring(0,i)+str.substring(i+1)).indexOf(str.charAt(i).toLowerCase()) == -1 && (str.substring(0,i)+ str.substring(i+1)).indexOf(str.charAt(i).toUpperCase()) == -1)
return str.charAt(i);
return '';
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question