find the nth square number in typescript

To find the nth square number in TypeScript, we can write a function that makes use of the formula n^2 to calculate and return the nth square number. Here is the function:

index.ts
function findNthSquareNumber(n: number): number {
  // Calculate the nth square number using the formula n^2
  return Math.pow(n, 2);
}
136 chars
5 lines

To use this function, we can call it with the value of n for which we want to find the nth square number. For example, to find the 5th square number, we can call the function like this:

index.ts
const fifthSquareNumber = findNthSquareNumber(5);
console.log(fifthSquareNumber); // Output: 25
96 chars
3 lines

This will calculate and return the 5th square number, which is 25.

gistlibby LogSnag