如何忽略数组的左边和中间元素(如果是奇数数组)以检查右边的元素是否存在?

时间:2018-10-07 09:58:16

标签: javascript arrays

我要退货

  • true ,如果某项位于数组的后半部分

  • false (如果该项目在上半部分之内)

  • false (如果它是奇数数组的中间项)。

我有一些工作代码对此进行测试,但是我坚持要确保未测试奇数数组的中间项。

function isItemPastHalf(array, item) {

  var halfWayThough = Math.floor(array.length / 2)

  var arrayFirstHalf = array.slice(0, halfWayThough);

  var arraySecondHalf = array.slice(halfWayThough, array.length);

  return arraySecondHalf.includes(item);

}

var array = [1, 2, 3];

console.log(isItemPastHalf(array, 2));

到目前为止,此代码不适用于array = [1、2、3] item = 2,因为2是一个奇数的中间,应该返回false。针对此特定问题的其他测试包括偶数长度数组。有人对我可以使用的东西有任何建议吗?

6 个答案:

答案 0 :(得分:2)

Math.ceil()将代替这里的Math.floor()

function isItemPastHalf(array, item) {

  var halfWayThough = Math.ceil(array.length / 2);

  var arrayFirstHalf = array.slice(0, halfWayThough);

  var arraySecondHalf = array.slice(halfWayThough, array.length);

  return arraySecondHalf.includes(item);

}

var array = [1, 2, 3, 4, 5, 6, 7];

console.log(isItemPastHalf(array, 3));
console.log(isItemPastHalf(array, 2));
console.log(isItemPastHalf(array, 4));

答案 1 :(得分:0)

function isItemPastHalf(array, item) {
  let itemIndex = array.lastIndexOf(item)
  let mid = Math.floor(array.length/2)
  return mid <  itemIndex;
}

var oddLengthArray = [1, 2, 3, 4, 5, 6, 7];
var evenLengthArray = [1, 2, 3, 4, 5, 6];

console.log("ODD LENGTH ARRAY")
console.log("3",isItemPastHalf(oddLengthArray, 3));
console.log("4",isItemPastHalf(oddLengthArray, 4));
console.log("5",isItemPastHalf(oddLengthArray, 5));

console.log("ODD LENGTH ARRAY")
console.log("3",isItemPastHalf(evenLengthArray, 3));
console.log("4",isItemPastHalf(evenLengthArray, 4));
console.log("5",isItemPastHalf(evenLengthArray, 5));

答案 2 :(得分:0)

您可以通过先搜索索引然后确定返回以下内容来增强功能

function isItemPastHalf (array, item) { 
    let idx = array.lastIndexOf(item);
    let middleIndex = Math.ceil(array.length/2)-1;
    return idx > middleIndex; 
 } 

答案 3 :(得分:0)

您可以使用Array的findIndex方法。

function isElementInTheSecondHalfOfTheArray(element, array) {
    const index = array.findIndex(arrayElement => arrayElement === element);
    return index > (array.length - 1) / 2; 
}

console.log( isElementInTheSecondHalfOfTheArray(1, [1, 2, 3]) );
console.log( isElementInTheSecondHalfOfTheArray(2, [1, 2, 3]) );
console.log( isElementInTheSecondHalfOfTheArray(3, [1, 2, 3]) );

答案 4 :(得分:0)

我建议采用以下方法:

function isItemPastHalf(array, item) {
  // we find the index of the item in the array (which returns -1
  // if the item is not found) and check to see if that is greater
  // than, or equal to, the floored length of the array when divided
  // by 2; we use Math.floor() rather than Math.ceil() because
  // JavaScript arrays are zero-indexed:
  return array.indexOf(item) >= (Math.floor(array.length / 2));
}

var array = [1, 2, 3];

console.log(isItemPastHalf(array, 2));

[JS Fiddle演示](https://jsfiddle.net/davidThomas/cp6vLmsy/

参考文献:

答案 5 :(得分:0)

let numbers = [1, 2, 3, 4, 5]

function isItemPastHalf(array, item){
  if((array.indexOf(item)*2) > array.length)
    return true;
    return false
}

console.log(isItemPastHalf(numbers,3)) //false if it is the middle item

如果该项目的索引乘以2仍小于数组长度,则该项目必须在前半部分。