คำอธิบาย
เขียนฟังก์ชันที่รับสตริงและแปลงตัวอักษรแต่ละตัวเป็นตัวเลขตามลำดับอักษร (a=1, b=2, ..., z=26) ลบอักขระพิเศษและช่องว่างออก
ตัวอย่าง
"hello man" → "8,5,12,12,15,13,1,14"
ตัวอย่างโค้ด
const str = 'hello man';
const charPosition = str => {
str = str.split('');
const arr = [];
const alpha = /^[A-Za-z]+$/;
for (let i = 0; i < str.length; i++) {
if (str[i].match(alpha)) {
const num = str[i].charCodeAt(0) - 96;
arr.push(num);
} else {
continue;
}
}
return arr.toString();
};
console.log(charPosition(str));
ผลลัพธ์
"8,5,12,12,15,13,1,14"