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

134
Views
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 answers
Answer question

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 Report

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 Report

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 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!