Min & Max Values in Array
given an array myArray of integers, write a JS program to find the maximum and minimum values in the array.
input1
[1,4,2,7,9,3,5]
output1
{min: 1, max: 9}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function findMinAndMaxValuesInArray(arr) {
/* Write your code here */
}
/* Please do not modify anything below this line */
function main() {
let myArray = JSON.parse(readLine());
console.log(findMinAndMaxValuesInArray(myArray));
}
"use strict";
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let inputString = "";
let currentLine = 0;
process.stdin.on("data", (inputStdin) => {
inputString += inputStdin;
});
process.stdin.on("end", (_) => {
inputString = inputString
.trim()
.split("\n")
.map((str) => str.trim());
main();
});
function readLine() {
return inputString[currentLine++];
}
/* Please do not modify anything above this line */
function findMinAndMaxValuesInArray(arr) {
let min = arr[0], max = arr[0];
arr.map((e) => {
if (e < min) min = e
if (e > max) max = e
})
return {'min': min, 'max': max}
}
/* Please do not modify anything below this line */
function main() {
let myArray = JSON.parse(readLine());
console.log(findMinAndMaxValuesInArray(myArray));
}
Comments
Leave a comment