Returning the value of nth power of iota(i) using JavaScript


Problem

We are required to write a JavaScript function that takes in a number. Our function should return the value of −

(i)n

Here,

i = -11/2

Therefore,

i^2 = -1
i^3 = -i
i^4 = 1 and so on

Example

Following is the code −

 Live Demo

const num = 657;
const findNthPower = (num = 1) => {
   switch(num % 4){
      case 0:
         return '1';
      case 1:
         return 'i';
      case 2:
         return '-1';
      case 3:
         return '-i';
   };
};
console.log(findNthPower(num));

Output

i

Updated on: 20-Apr-2021

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements