ปัญหา
เราต้องเขียนฟังก์ชัน JavaScript ที่รับข้อความที่ถอดรหัสแล้ว และคืนค่าข้อความต้นทาง (source message) ทั้งที่เรารู้คืออัลกอริทึมที่ใช้เข้ารหัส
อัลกอริทึมคือ:
- กลับลำดับสตริงข้อความ
- แทนที่ทุกตัวอักษรด้วยรหัส ASCII ในเครื่องหมายคำพูด (A เป็น '65', h เป็น '104' ฯลฯ)
- ตัวเลขและช่องว่างแทรกตามเดิม
ตัวอย่าง
โค้ดตัวอย่างดังนี้:
const str = '12 hello world 30';
const decryptString = (str = '') => {
const alpha = 'abcdefghijklmnopqrstuvwxyz';
let res = '';
for(let i = str.length - 1; i >= 0; i--){
const el = str[i];
if(alpha.includes(el.toLowerCase())){
res += `'${el.charCodeAt(0)}'`;
}else{
res += el;
};
};
return res;
};
console.log(decryptString(str));
ผลลัพธ์
ผลลัพธ์ในคอนโซล:
03 '100''108''114''111''119' '111''108''108''101''104' 21