Tengo 5 ecuaciones en Javascript, todas son muy similares, toman un valor numérico, lo dividen por 60 y luego lo multiplican por otro valor. p.ej:
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**Los valores de los dos primeros conjuntos de variables pueden cambiar, pueden ser cualquier número, incluso 0, lo que quiero hacer es comprimir los 5 cálculos en 1, así que quiero poder obtener el 'valor final' (6.154 ) valor de una ecuación, y me gustaría que funcione independientemente de cuáles sean los valores, por ejemplo, no quiero tener que codificarlo.
Por ejemplo, he intentado
(((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)Cualquier ayuda sería apreciada, gracias.
Puede tomar todos los valores en matrices y reducir las matrices.
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.154Un enfoque un poco mejor es usar valores emparejados.
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.154puedes usar la fórmula como
(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);Puede lograr fácilmente el resultado usando reducir
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);