รหัส ASCII
ASCII คือโค้ดอักขระ 7 บิตที่ทุกบิตแสดงถึงอักขระที่ไม่ซ้ำกัน
ตัวอักษรภาษาอังกฤษทุกตัวมีรหัส ascii ทศนิยมที่ไม่ซ้ำกัน
เราจำเป็นต้องเขียนฟังก์ชันที่ใช้สองสตริงและคำนวณคะแนน ascii (เช่น ผลรวมของทศนิยม ascii ของอักขระแต่ละตัวของสตริง) และคืนค่าส่วนต่าง
ตัวอย่าง
มาเขียนโค้ดสำหรับสิ่งนี้กัน −
const str1 = 'This is the first string.';
const str2 = 'This here is the second string.';
const calculateScore = (str = '') => {
return str.split("").reduce((acc, val) => {
return acc + val.charCodeAt(0);
}, 0);
};
const compareASCII = (str1, str2) => {
const firstScore = calculateScore(str1);
const secondScore = calculateScore(str2);
return Math.abs(firstScore - secondScore);
};
console.log(compareASCII(str1, str2)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
536