Why this code isn't working? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why this code isn't working?

Could anyone please tell me why this code isn't working? This return NaN and I don't know why. Array.prototype.exe = () => { return (Math.max(this)); } let arr = [3,9,6]; console.log(arr.exe()); Many thanks

31st Jul 2019, 8:42 AM
Krzysztof
Krzysztof - avatar
2 Answers
+ 3
'this' works differently in arrow functions. Arrow functions refer to the scope they were declared so this is the global window object. Using the good old function operator solves this. Also you need to spread the array using ...this because Math.max uses individual arguments instead of whole arrays https://code.sololearn.com/WzPeHfAq2YMM/?ref=app
31st Jul 2019, 9:35 AM
Aaron Eberhardt
Aaron Eberhardt - avatar
+ 1
Thank you very much. Your explanation really helped me.
31st Jul 2019, 9:55 AM
Krzysztof
Krzysztof - avatar