Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

136
Visualizações
Compressing multiple equations into one JavaScript

I have 5 equations in Javascript, all are very similar, they take a number value, divide it by 60, then multiply it by another value. e.g.:

var a = 10;
var b = 1;
var c = 1;
var d = 15.5;
var e = 5

var a1 = 10;
var b1 = 1;
var c1 = 10;
var d1 = 15;
var e1 = 5


var calcOne = (a/60)*a1
var calcTwo = (b/60)*b1
var calcThree = (c/60)*c1
var calcFour = (d/60)*d1
var calcFive = (e/60)*e1

var finalValue = (calcOne + calcTwo + calcThree + calcFour + calcFive)

**finalValue = 6.154**

The values of the first two sets of variables can change, they can be any number at all, even 0's, what I want to do is compress the 5 calculations into 1, so I want to be able to get the 'finalValue' (6.154) value from one equation, and I would want it to work regardless of what the values are, e.g. I don't want to have to hardcode it.

For example, I have tried

(((a+b+c+d+e)/60) * (a1+b1+c1+d1+e1)) = 22.2425  //sum of first set, divided by 60, multiplied by sum of second set (value too big)

(((a+b+c+d+e)/300) * (a1+b1+c1+d1+e1)) = 4.4485  //sum of first set, divided by 300, multiplied by sum of second set (divided by 300 as there are 5 equations)

((((a+b+c+d+e)/5)/60) * (a1+b1+c1+d1+e1)) = 4.4485  //sum of first set divided by 5 , divided by 60, multiplied by sum of second set (divided by 5 before 60 as there are 5 equations)

(((a+b+c+d+e)/60) * ((a1+b1+c1+d1+e1/5))) = 4.4485  //sum of first set, divided by 60, multiplied by sum of second set (divided by 5 as there are 5 equations)

Any help would be appreciated, thank you.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could take all values into arrays and reduce the arrays.

const
    a = [10, 1, 1, 15.5, 5],
    b = [10, 1, 10, 15, 5],
    r = a.reduce((s, v, i) => s + v * b[i], 0) / 60;

console.log(r); // 6.154

A slightly better approach is to use paired values.

const
    values = [[10, 10], [1, 1], [1, 10], [15.5, 15], [5, 5]],
    r = values.reduce((s, [a, b], i) => s + a * b, 0) / 60;

console.log(r); // 6.154

about 4 years ago · Juan Pablo Isaza Relatório

0

you can just use the formula as

(a * a1 + b * b1 + c * c1 + d * d1 + e * e1)/60;

var a = 10;
var b = 1;
var c = 1;
var d = 15.5;
var e = 5

var a1 = 10;
var b1 = 1;
var c1 = 10;
var d1 = 15;
var e1 = 5


var calcOne = (a / 60) * a1
var calcTwo = (b / 60) * b1
var calcThree = (c / 60) * c1
var calcFour = (d / 60) * d1
var calcFive = (e / 60) * e1

var finalValue = (calcOne + calcTwo + calcThree + calcFour + calcFive);
var formula = (a * a1 + b * b1 + c * c1 + d * d1 + e * e1)/60;

console.log(finalValue,formula);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can easily achive the result using reduce

var a = 10;
var b = 1;
var c = 1;
var d = 15.5;
var e = 5;

var a1 = 10;
var b1 = 1;
var c1 = 10;
var d1 = 15;
var e1 = 5;


const first = [a, b, c, d, e];
const second = [a1, b1, c1, d1, e1];

const result = first.reduce((acc, curr, i) => acc + (curr / 60) * second[i], 0);
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda