Replace With Alphabet Position - Codewars


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 :slight_smile:

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:

ha! see I tried that and for some reason, changing the regex to:

/\s+[^a-z]/gi from just /\s+/gi brought back the spaces as a coded number. I had to add a new replace method replace(/[^a-z]/gi, '')

In the end though it passed! (not sure why the one replace method with the combined regex didn’t work - any idea ?? )

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.

of course I should have known that ! thank you sir !