ปัญหา
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้สตริงตัวอักษรตัวพิมพ์เล็ก ดัชนีของ 'a' ในตัวอักษรคือ 1 ของ 'b' คือ 2 'c' คือ 3 … ของ 'z' คือ 26
ฟังก์ชันของเราควรรวมดัชนีทั้งหมดของอักขระสตริงและส่งคืนผลลัพธ์
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const str = 'lowercasestring';
const findScore = (str = '') => {
const alpha = 'abcdefghijklmnopqrstuvwxyz';
let score = 0;
for(let i = 0; i < str.length; i++){
const el = str[i];
const index = alpha.indexOf(el);
score += (index + 1);
};
return score;
};
console.log(findScore(str)); ผลลัพธ์
ต่อไปนี้เป็นเอาต์พุตคอนโซล -
188