我不太理解这个练习。
arr = [1, 2, 3];
arr.indexOf(2); // 1
arr.indexOf(4); // -1 since it doesn't exist.
// Write a function to determine if an element
// is in the first half of an array.
// If an element has an odd length, round up
// when counting the first half of an array.
// For example, 1 and 2 are in the first half
// arr.
function inFirstHalf(inputArray, element) {
}
我不明白,数组的前半部分是什么意思?
最佳答案
在将原始数组一分为二创建的新数组中查找值:
arr.slice(0, Math.ceil(arr.length/2)) . indexOf(val)
关于javascript - 在 Javascript 数组的前半部分查找元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33512458/