Computer >> Computer tutorials >  >> Programming >> Javascript

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 −

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