เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้ตัวอักษรตัวเดียวเป็นอินพุตเดียว ฟังก์ชันควรคำนวณตำแหน่งของตัวอักษรนั้นตั้งแต่เริ่มต้นและส่งกลับตัวอักษรที่อยู่ในตำแหน่งเดิมแต่จากด้านหลัง
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
const alpha = 'g';
const findCounterPart = (alpha = '') => {
let alphabet = 'abcdefghijklmnopqrstuvwxyz';
let firstpart = alphabet.substring(0,13).split('');
let secondpart = alphabet.substring(13).split('').reverse();
let solution = '';
if (firstpart.indexOf(alpha) !== −1) {
solution = secondpart[firstpart.indexOf(alpha)];
} else {
solution = firstpart[secondpart.indexOf(alpha)];
};
return solution;
};
console.log(findCounterPart(alpha)); ผลลัพธ์
และผลลัพธ์ในคอนโซลจะเป็น −
t