What does the reduction parameters mean?

1

I want to find the minimum sum of an arrangement

that is to say of this arrangement [5,4,2,3] the minimum sum is 22.

my solution is:

function minSum(arr) {
  arr = Object.values(arr).sort((a, b) => a - b);

  var j = arr.length - 1;
  var suma = 0;
  for (var i = 0; i < arr.length - 1 / 2; i++) {
    suma = suma + arr[i] * arr[j];
    j--;
  }
  return suma / 2;
}

console.log(minSum([5, 4, 2, 3]));

but also the following code would be another solution

minSum = a => (a = a.slice().sort((a, b) => a - b)).reduce((x, y, i) => x + (y * a[a.length - 1 - i]), 0) / 2

console.log(minSum([5, 4, 2, 3]));

I do not understand this part: reduce((x, y, i) what are these parameters and where do they come from?

    
asked by hubman 12.01.2018 в 15:36
source

2 answers

3

The parameter x is called accumulator or accumulator. This contains the value of the previous operation returned. For example:

var resultado = [1,1,1,1].reduce(function(acumulador ){ 
  console.log("El valor del acumulador es " + acumulador);
  
  return acumulador+1;
})

console.log("La suma total es " + resultado);

Notice how the value of the acomulator parameter increases from 1 to 1. This happens through the expression return acumulador + 1 . Just to show a clearer example, the accumulator would be like this:

var array = [1,1,1,1];
var acumulador = 0;
for(var i = 0; i < array.length;i++)
{
  acumulador = acumulador + array[i];
}

console.log(acumulador);

Note how the value of the same is added to the current value of the array. acumulador = acumulador + array[i]; is the same as return acumulador + 1; .

y represents the current value of the array being processed. In the example above, it would represent array[i] .

And finally i , represents the element index being processed. In this example it would represent the variable i defined in the for.

    
answered by 12.01.2018 / 15:54
source
3

Syntax:

var resultado = arr.reduce(funcion[, valorInicial]);

The reduce() method applies a function to an accumulator and to each value of an array (from left to right) to reduce it to a single value.

.reduce parameters:

  • funcion

Function to execute for each value of the array, which receives four parameters:

Function parameters:

  
  • valorAnterior
  •   

The value returned (returned) in the previous call of the function, or    valorInicial , if provided. (See below.)

     
  • valorActual
  •   

Current element that is being processed in the array.

     
  • indiceActual
  •   

Index of the current element that is being processed in the array.

     
  • array
  •   

The array on which the method was called reduces.

  • valorInicial

Object to be used as the first argument in the first call of the function.

Practical example:

[0,1,2,3,4].reduce(function(valorAnterior, valorActual, indice, vector){
  return valorAnterior + valorActual;
});

// Primera llamada
valorAnterior = 0, valorActual = 1, indice = 1

// Segunda llamada
valorAnterior = 1, valorActual = 2, indice = 2

// Tercera llamada
valorAnterior = 3, valorActual = 3, indice = 3

// Cuarta llamada
valorAnterior = 6, valorActual = 4, indice = 4

// el array sobre el que se llama a reduce siempre es el objeto [0,1,2,3,4] 

// Valor Devuelto: 10

With initial value:

[0,1,2,3,4].reduce(function(valorAnterior, valorActual, indice, vector){
  return valorAnterior + valorActual;
}, 10);

// Primera llamada
valorAnterior = 10, valorActual = 0, indice = 0

// Segunda llamada
valorAnterior = 10, valorActual = 1, indice = 1

// Tercera llamada
valorAnterior = 11, valorActual = 2, indice = 2

// Cuarta llamada
valorAnterior = 13, valorActual = 3, indice = 3

// Quinta llamada
valorAnterior = 16, valorActual = 4, indice = 4

// el array sobre el que se llama a reduce siempre es el objeto [0,1,2,3,4] 

// Valor Devuelto: 20

F Source and more examples here

    
answered by 12.01.2018 в 16:00