mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
17 lines
487 B
JavaScript
17 lines
487 B
JavaScript
/**
|
|
* Returns the evaluation of a polynomial function at a certain point.
|
|
* Uses straightforward approach with powers.
|
|
*
|
|
* @param {number[]} coefficients - i.e. [4, 3, 2] for (4 * x^2 + 3 * x + 2)
|
|
* @param {number} xVal
|
|
* @return {number}
|
|
*/
|
|
export default function classicPolynome(coefficients, xVal) {
|
|
return coefficients.reverse().reduce(
|
|
(accumulator, currentCoefficient, index) => {
|
|
return accumulator + currentCoefficient * (xVal ** index);
|
|
},
|
|
0,
|
|
);
|
|
}
|