Returning the nth even number using JavaScript

JavascriptWeb DevelopmentFront End Technology

Problem

We are required to write a JavaScript function that takes in a number n. And our function should simply return the nth even number in natural numbers.

Example

Following is the code −

 Live Demo

const num = 67765;
const nthEven = (num = 1) => {
   const next = num * 2;
   const res = next - 2;
   return res;
};
console.log(nthEven(num));

Output

135528
raja
Published on 17-Apr-2021 13:23:33
Advertisements