2
Author: Dmitri Pavlutin
Translator: Front-end Xiaozhi
Source: Dmitri Pavlutin

If you have dreams and dry goods, search on [Moving the World] attention to this brush bowl wisdom who is still washing dishes in the early morning.

This article https://github.com/qq449245884/xiaozhi . There are complete test sites, materials and my series of articles for interviews with first-tier manufacturers.

Math.max() is a built-in method in JS, which can return the largest one from the incoming parameters. For example:

Math.max(1, 2, 3); // => 3

What if Math.max() only one parameter?

Math.max(1); // => 1

As expected, the maximum value of a number is itself.

But what if you call Math.max() without arguments?

Math.max(); // => -Infinity

The result returned Math.max() without parameters -Infinity . Next, let's see why this happens.

maximum value in an array

Before discussing this problem, let's first come to Math.max() gets the maximum value from the array.

Math.max(num1, num2, ..., numN) accepts multiple numeric arguments and returns their maximum number.

If we want to get the maximum value from the array, we can use the spread operator:

const numbers1 = [1, 2, 3];

Math.max(...numbers1); // => 3

2. Maximum value in two arrays

Now, let's take a look at the interesting thing, given two arrays, we first determine the maximum value in each array, and then determine the maximum value from the two maximum values.

const numbers1 = [1, 2, 3];
const numbers2 = [0, 6];

const max1 = Math.max(...numbers1);
const max2 = Math.max(...numbers2);

max1; // 3
max2; // 6
Math.max(max1, max2); // => 6

The [1, 2, 3] is 3, the maximum value of the array [0, 6] is 6, and the maximum value of the last 3 and 6 is 6.

No problem, let's continue.

What happens if an array is empty, let's try it out:

const numbers1 = [];
const numbers2 = [0, 6];

const max1 = Math.max(...numbers1);
const max2 = Math.max(...numbers2);

max1; // -Infinity
max2; // 6
Math.max(max1, max2); // => 6

Now, when the first array is empty, the maximum value above is also 6 .

More interesting here is Math.max(...numbers1) return value, when numbers1 array is empty, which is invoked without parameters Math.max() the same, the result is -Infinity .

So Math.max(max1,max2) equivalent to Math.max(-Infinity, 6) and the result is 6.

Now you know why Math.max() -Infinity: when called with no arguments This is a way to define the max

This is similar to an adder, max of -Infinity and addition 0 same.

Math.min() has the same behavior - when called with no arguments, it will return Infinity .

Regarding maximum operations on real numbers, -Infinity called Identity element

In this paper done for here, come here a challenge: Can you write with a Math.max() exactly the same sum(num1, num2, ..., numN) function, its function is to find all the elements and,

What is the Identity element , do you understand, welcome to leave a message to add knowledge points.


code is deployed cannot be known in real time. In order to solve these bugs afterwards, a lot of time is spent on log debugging. By the way, I recommend a useful bug monitoring tool Fundebug .

Original: https://dmitripavlun.com/javscript-math-max-infinity/

comminicate

If you have dreams and dry goods, search on [Moving the World] attention to this brush bowl wisdom who is still washing dishes in the early morning.

This article https://github.com/qq449245884/xiaozhi . There are complete test sites, materials and my series of articles for interviews with first-tier manufacturers.


王大冶
68k 声望104.9k 粉丝