Answer to Question #303190 in HTML/JavaScript Web Application for king

Question #303190

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));

}



1
Expert's answer
2022-03-02T14:10:21-0500
"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));
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS