function alphabetPosition(text) {
let smallCase = text.toLowerCase();
let array = smallCase.replace(/\s+/gi, "");
console.log(array);
let finalArr = array.split("");
let newArr = [];
for (let i = 0; i<finalArr.length; i++){
let str = finalArr[i].charCodeAt(0) - 96; // is this line the culprit?
newArr.push(str);
} return newArr.join(" ");
}
The above works exclusively with strings containing letters only
Struggle street is how to exclude symbols/special characters like a single quotation mark?
Here you need to replace not only whitespaces, but any non-alphabet characters with ''. That would be [^a-z] I guess
The rest of the code is implementation of map() method and can be replaced with it or even better, without need to convert string into array, you can simply .replace every character with it’s code - 96. Note that .replace method can take replacer function as the second argument:
This means one or more whitespace characters followed by one non-alphabet character. You don’t need this regex at all, as /[^a-z]+/g covers all non-alphabet characters including whitespaces.