gistlib
Here's a function that calculates the nth nonagonal number in javascript:
index.tsxfunction nonagonalNumber(n) { return n * (5 * n - 3) / 2; } console.log(nonagonalNumber(5)); // Output: 70 console.log(nonagonalNumber(8)); // Output: 181 158 chars7 lines
function nonagonalNumber(n) { return n * (5 * n - 3) / 2; } console.log(nonagonalNumber(5)); // Output: 70 console.log(nonagonalNumber(8)); // Output: 181
The formula for the nth nonagonal number is n(5n - 3) / 2. We simply plug in the value of n in the formula and return the result.
gistlibby LogSnag