if there are even number of elements in an array what is the index of the mid element code example

Example 1: create a function that checks the values of the indexes in two arrays and keep a score

const triplets = (arr1,arr2) => {
  let score1 = 0;
  let score2 = 0;
  let resultArr = [0,0]
  for (let i = 0; i < arr1.length; i++){
    if(arr1[i] === arr2[i]) {
      resultArr[0] = score1
      resultArr[1] = score2
    } else if (arr1[i] > arr2[i]) {
      score1++
      resultArr[0] = score1
    } else if (arr1[i] < arr2[i]) {
      score2++
      resultArr[1] = score2
    }
  }
  return resultArr
}

Example 2: Try out the enumerate function for yourself in this quick exercise. Complete the skip_elements function to return every other element from the list, this time using the enumerate function to check if an element is on an even position or an odd position.

def altElement(a):
    return a[::2]

Tags:

Php Example