Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda