Home

>

Prompts Database

>

Coding
Write JSDoc comments
avatar
Buffy Summers
Share
Leave a comment
614 days ago

Prompt type:

Write JavaScript code, Write code

Category:

Coding

Media type:

Use with AI:

ChatGPT from OpenAI
View Full Output

Example output

Here's an example with JSDoc comments for a function that calculates the factorial of a number using recursion:

/** * Calculates the factorial of a given number using recursion. * * @param {number} n - The number to calculate the factorial for. * @returns {number} The factorial of the given number. * @throws {Error} Throws an error if the input is not a non-negative integer. */ function factorial(n) { // Check if n is a non-negative integer if (typeof n !== 'number' || n < 0 || !Number.isInteger(n)) { throw new Error('Input must be a non-negative integer.'); } // Base case: factorial of 0 is 1 if (n === 0) { return 1; } // Recursive case: n * factorial(n - 1) return n * factorial(n - 1); }

In this example:

  • @param {number} n indicates that the n parameter should be a number.
  • @returns {number} indicates that the function returns a number, which is the factorial of the given number.
  • @throws {Error} indicates that the function may throw an error if the input is not a non-negative integer.

The comments explain the purpose of the function, the input parameter, the return value, and any potential errors that the function may throw.

Description or instructions

Write JSDoc comments for the below JavaScript function: [code snippet]

Prompt

In this task, I will write JSDoc comments for the provided JavaScript function. JSDoc comments are used to document JavaScript code and provide information about the function's parameters, return type, description, and other relevant details. The comments will follow the JSDoc syntax and conventions to ensure clarity and understanding for developers who may use or interact with the function.