ฉันต้องฟังก์ชันนั้นได้ตารางคู่ลำดับและกลับมาอาเรย์กับพลังงาน 2 ของทุกตารางคู่ลำดับองค์ นี่คือรหัสแหล่งข่าว
const firstArr = [1, 2, 3, 7, 4, 9];
function arrayPow(arr) {
const outputArray = [];
arr.forEach(el => {
console.log(el);
outputArray.splice(-1, 0, el**2);
})
return outputArray;
}
console.log(arrayPow(firstArr));
ฉันได้มันเป็นตัวเลือกการไล่ระดับสี
script.js:8 1
script.js:8 2
script.js:8 3
script.js:8 7
script.js:8 4
script.js:8 9
script.js:14 (6) [4, 9, 49, 16, 81, 1]
Scedule ของ elments ถูกต้องในเรื่อยๆ แต่ตอนนี้อยู่ในอาเรย์มีแรกธาตุในบาง reson อยู่ในท้ายที่สุด ฉันพยายามจะลบ"1"จาก firstArr แล้ว"4"ไปที่ตำแหน่งสุดท้าย. ทำไมเหรอ?
const arrayPow = (arr, pow = 2) => arr.map(el => el**pow);