find the nth hexagonal number in javascript

To find the nth hexagonal number in JavaScript, we can use the following formula:

Hexagonal number = n(2n-1)

where n is the nth number to be found.

Here's the code that implements this formula in JavaScript:

index.tsx
function findHexagonalNumber(n) {
  return n * (2 * n - 1);
}

console.log(findHexagonalNumber(5)); // Output: 61
114 chars
6 lines

In the example code, the function findHexagonalNumber() takes an argument n, which represents the nth hexagonal number to be found. The function then applies the formula n(2n-1) to calculate the hexagonal number and returns it.

To test the function, we pass 5 as the argument, which represents the 5th hexagonal number. The output, 61, is correct as the 5th hexagonal number is 61.

gistlibby LogSnag