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

202
Views
Javascript identify sum of array elements randomly located within array

So.. currentArray=[n1, n2, n3, n4, sum(currentArray), n5, n6] <--psuedocode obv

So another way would be currentArray.push(sum(oldArray)).shuffle(random) <--psuedo

I thought this would be as easy as using Math.max(...array), like in this example..

shuffled=[1, 12, 3, 6, 2]

I would be looking for the 12 here, as removing the 12 would result in sum(array)=12.

However, it got confusing and difficult when the input had positive and negative numbers included. As in this example..

shuffled=[1, -3, -5, 7, 2]

In this case, the solution would be 1, as removing the 1 would result in sum(array)=1

UPDATE

Question had a pretty simple, but not obvious solution, at least to me haha. Here is the updated, working code.

Array.prototype.polysplice = function (criteria)  {
    this.splice(this.indexOf(criteria),1)
    return this }
    
Array.prototype.polysort = function () {
    return this.sort((a, b) => a - b)  }
    
Array.prototype.polysum = function (subject) {
    return this.reduce((a,b) => a + b, 0) }

const solution = shuffled => shuffled.polysplice(shuffled.polysum()/2).polysort()

ORIGINAL

My code, but obv its no bueno..

Array.prototype.sliceAll = function (criteria) {
    return this.filter(e=>e !== criteria) }
    
Array.prototype.polysort = function () {
    return this.sort((a, b) => a - b)  }
    
const solution = shuffled => shuffled.sliceAll(Math.max(...shuffled)).polysort()
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

For both examples you can simply add all vars togehther and divide the sum by 2

sum(array)/2

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!