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