记录学习数据结构与算法,对自己的编码的整洁度、严谨度和调试能力进行一定的提升
第一题:实现重写数组的reverse方法
const arr = [1, 2, 3, 4, 5]
Array.prototype.newReverse = function () {
//// 使用 this 来引用当前数组实例 (this 来代替 arr,确保方法能够正确地引用当前数组实例)
for (let i = 0; i < Math.floor(this.length / 2); i++) {
let left = i
let right = this.length - 1 - i
let temp = this[left]
this[left] = this[right]
this[right] = temp
}
return this //返回修改后的数组
}