JavaScript bootcamp · Lab 17

Maximum in an array

mediumArrays12 minLesson: Arrays

Read the question, write JavaScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

The first line is n, the count. The second line has n integers separated by spaces. Log the maximum.

split + map(Number) turns a line of text into an array of numbers.

Input. Line 1: n. Line 2: n integers separated by spaces.

Output. One integer: the maximum.

Constraints

  • 1 ≤ n ≤ 1000

Examples

Example 1
Input
5
3 1 8 2 4
Output
8
Example 2
Input
1
42
Output
42
Hint
  1. readLine().trim().split(/\s+/).map(Number) builds the array.
  2. Math.max(...nums) spreads the array into arguments.
Show correct code

Peek only after you have tried. You can still Check your own version.

const n = Number(readLine());
const nums = readLine().trim().split(/\s+/).map(Number);
console.log(Math.max(...nums));
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.