How to Replace Letters With Position In Alphabet in javaScript?
I need to create a function that takes a string and replaces each letter with its appropriate position in the alphabet
1 Answer
Try this function using indexOf() function
function alphabetIndex(str) {
const letters = 'abcdefghijklmnopqrstuvwxyz';
return [...str.toLowerCase()]
.filter(s => letters.includes(s))
.map(s => letters.indexOf(s) + 1)
.join(' ');
}
Example
alphabetIndex('abcd') // "1 2 3 4"
answer Link