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

JavascriptWeb DevelopmentFront End Technology

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
raja
Published on 20-Apr-2021 09:26:56
Advertisements