This is the code I tried and it runs well in the editor but whenever I try to submit it, it shows "Exceeded execution timeout of 40 seconds". Please help.
export const pow = (x, y) => { let power = x; if(y === 0) { return 1; } if(y === 1) { return x; } for(let i = 2; i <= y; i++) { power *= x; } return power; }
What if x is 0 or 1? Can you return the result without entering the loop and wasting computational resources?
Coderslang_Master Thanks for helping and after a bit of searching and tries, I finally got it. Here's how I did it.
export const pow = (x, y) => { let power = x ** y; return power; }
If there's any other better ways, please let me know.
@rookieronnie you took a shortcut, but that's a good one ๐
Coderslang_Master Thanks๐ but then, What the actual code is? I am unable to figure it out. Please let me know.
An alternative would be to add extra handling for the corner cases into your original code.
You already did it for y. Now you could do the same for x.
y
x
Something fun to do on this one is try an edge case, such as x = 0, y = 99999999999999, press run, then lift your laptop up to your ear and listen to it stream through millions of iterations of your loop!
p.s. I am not responsible for crashing your laptop ๐
Hacktinium ๐คฃ