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

118
Views
with JavaScript i want to have the following output

Output an array where a value is added to the next value of the array. The Last value will be added with the first value.

Example: [45, 4, 9, 16, 25] Converted to: [49, 13, 25, 41, 70]

Map() method must be used.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can try following:

console.log([45, 4, 9, 16, 25].map((item, index, array) => item + array[(index + 1) % array.length]))

about 4 years ago · Juan Pablo Isaza Report

0

When using Array.prototype.map() you can use the index parameter and in each iteration
to see if it's the end of the array or not like so:

const arr = [45, 4, 9, 16, 25];

const newArr = arr.map((item, i) => {
  return i !== (arr.length - 1) ? item + arr[i + 1] : item + arr[0];
})

console.log(newArr);

Array.prototype.map()

about 4 years ago · Juan Pablo Isaza Report

0

This works fine.

const nums = [45, 4, 9, 16, 25];

const newNums = nums.map((item, index) => {
  if(index < nums.length - 1) {
    return item + nums[ index + 1]
  }
    return item + nums[0]
})

console.log(newNums) // [ 49, 13, 25, 41, 70 ]
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!