อาร์เรย์เป็นอาร์เรย์พิเศษหาก −
--All the elements at odd indices are odd. --All the elements at even indices are even.
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่รับอาร์เรย์และตรวจสอบว่าเป็นอาร์เรย์พิเศษหรือไม่
ตัวอย่าง
ต่อไปนี้เป็นรหัส -
const arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const isSpecial = (arr = []) => {
for(let i = 0; i < arr.length; i++){
if(arr[i] % 2 === i % 2){
continue;
};
return false;
};
return true;
};
console.log(isSpecial(arr)); ผลลัพธ์
ต่อไปนี้เป็นผลลัพธ์ในคอนโซล -
true