Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
how do I find the middle index of an array?

I'm new, so this will be a stupid question. I got an assignment to create a global array, in which I will need to import objects, as they're added to an input field in the html document. Then from there, I need to update the current list on the html document, but I need to add the middle element of the array to the list, and then empty the array. My question is, how do I find the middle index and add it to the array? I tried the following code

const ulList = document.querySelector("ul");
                        const liElement = document.createElement("li");
                        liElement.innerHTML = `${globalArray[globalArray.length / 2 - 1].key} ${globalArray[globalArray.length / 2 - 1].manufacturer} ${globalArray[globalArray.length / 2 - 1].model}`
                        ulList.appendChild(liElement);
                        globalArray.splice(globalArray[globalArray.length / 2 - 1], 1);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

function getArrayMidKey(myArray) {
  let midKeyIndex = Math.floor(myArray.length / 2)
  return midKeyIndex;
}
let snacks = array("Cake", "Soda", "Apples", "Doritos");
midSnack = snacks[getArrayMidKey(snacks));
// midSnack --> "Soda"

This gives you the middle key of any given array..

If the array has 5 items, it will give you item number 2, if it has 6 or 7 items - it gives you item number 3 and so on.

about 4 years ago · Juan Pablo Isaza Report

0

Math.floor(globalArray.length / 2)


globalArray[Math.floor(globalArray.length / 2)]

Try this

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!