เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้อาร์เรย์ของตัวอักษรและตรวจสอบว่าองค์ประกอบเหมือนกันหรือไม่ว่าอ่านจากด้านหน้าหรือด้านหลังเช่น palindrome
ตัวอย่าง
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน −
const arr = [1, 5, 7, 4, 15, 4, 7, 5, 1];
const isPalindrome = arr => {
const { length: l } = arr;
const mid = Math.floor(l / 2);
for(let i = 0; i <= mid; i++){
if(arr[i] !== arr[l-i-1]){
return false;
};
};
return true;
};
console.log(isPalindrome(arr)); ผลลัพธ์
ผลลัพธ์ในคอนโซล:−
true