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

157
Views
Need help making a function to take in an int N and make an array with N elements. Each element should be half of the previous element starting from 1

Problem: Write a function that takes in an int n and returns a double[] of length n where the starting element (value) is 1.0 and the other elements are the previous divided by 2. For example, halves(4) should return a double[] with numbers {1.0, 0.5, 0.25, 0.125}. (JAVASCRIPT)

Stuck on where to continue, currently just have setting the array to length. Couldn't find any questions that already answered this.

void halves(int n){
    arrhalves[] n;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are writing in JavaScript, define an array with only first element [1.0]. Then, define for-loop and loop over the array n times. Starting index should be 1 (because we already have one element in the array) and on each iteration push (arr[i - 1]) / 2 to the array.

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!