Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The maximum value of the CoffeeScript array


May 09, 2021 CoffeeScript


Table of contents


The maximum value of the array

Problem

You need to find out the maximum value contained in the array.

Solution

You can use the JavaScript implementation to use Math on the basis of list .max ():

Math.max [12, 32, 11, 67, 1, 3]... 
# => 67

Another approach, in ECMAScript 5, is to use Array's reduce method, which is compatible with the old JavaScript implementation.

# ECMAScript 5 
[12,32,11,67,1,3].reduce (a,b) -> Math.max a, b 
# => 67

Discuss

Math .max two values here and returns the larger one. O dd (...) C onverts the value of each array into an argument to the function. You can also use it to discuss with other parameters with variable numbers, such as .log.