← Back to challenges

(a+b)^n Formula

JavaScriptHardstringslogicmathalgebra

Instructions

Create a function that takes a integer number n and returns the formula for (a+b)^n as a string.

Examples

formula(0) ➞ "1"

formula(1) ➞ "a+b"

formula(2) ➞ "a^2+2ab+b^2"

formula(-2) ➞ "1/(a^2+2ab+b^2)"

formula(3) ➞ "a^3+3a^2b+3ab^2+b^3"

formula(5) ➞ "a^5+5a^4b+10a^3b^2+10a^2b^3+5ab^4+b^5"

Notes

Don't put the following in your string:

  • spaces
  • *
  • ^1
  • a^0
  • b^0
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.