Polynomial Evaluator

Evaluate a polynomial expression at a given value of x using recursion

// ==UserScript==
// @name         Polynomial Evaluator
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Evaluate a polynomial expression at a given value of x using recursion
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to evaluate a polynomial at a given value of x
    function evaluatePolynomial(coefficients, x, n) {
        // Base case: if n is 0, return the constant term
        if (n === 0) {
            return coefficients[0];
        }
        // Recursive case: compute the polynomial value
        return coefficients[n] * Math.pow(x, n) + evaluatePolynomial(coefficients, x, n - 1);
    }

    // Example coefficients for P(x) = 2x^3 + 3x^2 + 4x + 5
    let coefficients = [5, 4, 3, 2]; // Coefficients are in increasing order of power

    // Value of x at which to evaluate the polynomial
    let x = 2;

    // Degree of the polynomial (highest power)
    let n = coefficients.length - 1;

    // Evaluate the polynomial
    let result = evaluatePolynomial(coefficients, x, n);

    console.log(`The value of the polynomial at x = ${x} is: ${result}`);
})();