minMax.js (*** Revise for math.max and math.min)
/*Algorithms: Min Max
For a given sequence of integers, find the minimum and maximum integers in the sequence.
Input
First line contains
n
, the number of integers.
Next
n
lines contain n integers, one integer per line.
Output
First line integer is the maximum
Second line integer is the minimum
Example
Input:
4
73
344
56
11
Output:
344
11
*/
let fs = require("fs");
let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');
function readLine() {
idx++;
return data[idx - 1].trim();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
let lengthofintegers = parseInt(readLine()); // 4
let arr = []; // initialising an array
for (i = 0; i < lengthofintegers; i++) // 1.(i=0), 2.(i=1), 3.(i=2), 4.(i=3),
5.(i=4<4 false exit the loop).
{ arr[i] = parseInt(readLine()); } // 1.(73), 2.(344) 3.(56) 4.(11)
//console.log(arr);
console.log(Math.max(...arr));// 344
console.log(Math.min(...arr));// 11
///////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:

Comments
Post a Comment