JavaScript Programming Problems Series
I am going to start a Programming test series where i will share commonly asked interview questions and their solutions for JavaScript developers.
Problem # 1
Replace With Alphabet Position
you are given a string, replace every letter with its's position in the alphabet, if the string has a value which is not an alphabet then ignore it. The Output should also be a String telling about the position of Alphabet.
Solution
function alphabetPosition(str){
str = str.split("");
const position =[];
const alpha = "abcdefghijklmnopqrstuvwxyz";
for(let wo of str) {
if (alpha.indexOf(wo)>=0) {
position.push(alpha.indexOf(wo)+1, " ")
}
}
return position.join("")
}
alphabetPosition("21a dsz")
Top comments (5)
I am not sure 100% about correctitude, I wrote it from my phone, but I hope it's doing well.
That's faster than the original solution
it's working correctly <3
Great idea for a series! But I think the question is a little under-specified. It'd be helpful to state that the output should also be a string (not an array), that alphabet places are 1-indexed, that each one must be followed by a space, and that you only need to handle lowercase Latin-alphabet letters without diacritics.
Anyway, here's my attempt: