
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Expanding binomial expression using JavaScript
Problem
We are required to write a JavaScript function that takes in an expression in the form (ax+b)^n where a and b are integers which may be positive or negative, x is any single character variable, and n is a natural number. If a = 1, no coefficient will be placed in front of the variable.
Our function should return the expanded form as a string in the form ax^b+cx^d+ex^f... where a, c, and e are the coefficients of the term, x is the original one-character variable that was passed in the original expression and b, d, and f, are the powers that x is being raised to in each term and are in decreasing order
Example
Following is the code −
const str = '(8a+6)^4'; const trim = value => value === 1 ? '' : value === -1 ? '-' : value const factorial = (value, total = 1) => value <= 1 ? total : factorial(value - 1, total * value) const find = (str = '') => { let [op1, coefficient, variable, op2, constant, power] = str .match(/(\W)(\d*)(\w)(\W)(\d+)..(\d+)/) .slice(1) power = +power if (!power) { return '1' } if (power === 1) { return str.match(/\((.*)\)/)[1] } coefficient = op1 === '-' ? coefficient ? -coefficient : -1 : coefficient ? +coefficient : 1 constant = op2 === '-' ? -constant : +constant const factorials = Array.from({ length: power + 1 }, (_,i) => factorial(i)) let result = '' for (let i = 0, p = power; i <= power; ++i, p = power - i) { let judge = factorials[power] / (factorials[i] * factorials[p]) * (coefficient * p * constant * i) if (!judge) { continue } result += p ? trim(judge) + variable + (p === 1 ? '' : `^${p}`) : judge result += '+' } return result.replace(/\+\-/g, '-').replace(/\+$/, '') }; console.log(find(str));
Output
576a^3+1152a^2+576a
- Related Articles
- Expanding Numerals in JavaScript
- How to create Expanding Cards using HTML, CSS, and JavaScript
- How to find the binomial coefficient of two integers using JavaScript?
- How to create an expanding grid with CSS and JavaScript?
- Expanding Your Business Enterprise Internationally
- How to define a JavaScript function using the expression?
- Expanding the Boundaries of International Human Rights
- Binomial Heap in C++?
- Binomial Coefficient in java
- Binomial Coefficient in C++
- What Is Binomial Nomenclature?
- C++ Return Previous Element in an Expanding Matrix
- Named function expression in JavaScript
- Regular Expression Matching in JavaScript
- Binomial Distribution in Data Structures

Advertisements