LearnFrenzy
Sign in Take a free test

Spread and Rest Operators · Find the Error · Question 9

Error: spreading millions of numbers into Math.max Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
const readings = new Array(5_000_000).fill(1);
readings[123] = 99;
console.log(Math.max(...readings));
  • A It prints 99
  • B It prints 1
  • C RangeError: Maximum call stack size exceeded – five million arguments are too many; use reduce or a loop Correct
  • D It prints Infinity
Correct Answer: Option C

Every element becomes a separate argument on the stack. readings.reduce((m, x) => (x > m ? x : m), -Infinity) works for any size.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links