As a novice, I am trying to tackle this challenging kata:
This particular kata requires you to take a string and replace each letter with its corresponding position in the alphabet.
If there are any characters in the text that are not letters, simply ignore them and do not include them in the result.
"a" = 1, "b" = 2, and so on.
Although my solution works well until it encounters a whitespace within the string, which then gets included as an element in the positions array. I understand that filtering out the whitespaces could resolve this issue, but I would really like to understand why my code is failing.
function alphabetPosition(text) {
const alphabet="abcdefghijklmnopqrstuvwxyz".split('');
const letters = text.toLowerCase().split('');
let positions =[];
for (i=0; i<letters.length; i++){
if (!(alphabet.includes(letters[i]))){
continue;
}
positions[i]=(alphabet.indexOf(letters[i])+1);
}
return (positions.join(' '));
}
I believed that using continue should skip over the whitespaces within the letters array, but it seems that the continue keyword does not have the expected effect. I have tried researching the correct usage of continue, without finding where I went wrong. Any guidance or assistance would be greatly appreciated. Thank you in advance!