find the nth nonagonal number in javascript

Here's a function that calculates the nth nonagonal number in javascript:

index.tsx
function nonagonalNumber(n) {
  return n * (5 * n - 3) / 2;
}

console.log(nonagonalNumber(5)); // Output: 70
console.log(nonagonalNumber(8)); // Output: 181
158 chars
7 lines

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