Joanne
Joanne

Reputation: 11

Finding minimum and maximum from an array using functions

I am supposed to find minimum and maximum from function "getNumber". I will need to prompt for the user to stop, if user does not want to stop, function "getNumber" will be carried out repeatedly till the user ask to stop. When the user decided to stop, it will find the max and min from inputs which user keyed in. May I know how to link these 3 functions tgr to call the max and min?

//prompt to get number
function getNumber() {
    var myNumber;
    do {
        myNumber = Number(prompt("Enter an unsigned base 10 number:")); 
    } while (myNumber < 0) //loop will run again and again as long as the number is less than zero
    return myNumber;
}

//function to find the maximum number in the array
function myMax(myArray) {
    var max = myArray[0];
    for (var z = 0; z < myArray.length; z++) {
        if (myArray[z] > max) {
            max = myArray[z];
        }
    }
    return max;
}

//function to find the minimum number in the array
function myMin(myArray) {
var min = myArray[0];
for (var z = 0; z < myArray.length; z++) {
    if (myArray[z] > min) {
        min = myArray[z];
    }
}
return min;
}

Upvotes: 0

Views: 232

Answers (3)

CY_
CY_

Reputation: 7628

var numbers = [];
var isStopped = false;
//prompt to get number
function getNumber() {
    var input = prompt('Enter an unsigned base 10 number or Input S/s to stop:');
    var myNumber = Number(input);
    if (input == 'S' || input == 's'){
        finish();
    }
    else if (myNumber > 0 && myNumber < 10){
        console.log("input number is: " + myNumber);
        numbers.push(myNumber);
    }
}

//function to find the maximum number in the array
function myMax(myArray) {
    var max = myArray[0];
    for (var z = 0; z < myArray.length; z++) {
        if (myArray[z] > max) {
            max = myArray[z];
        }
    }
    return max;
}

function javascript_abort()
{
    throw new Error('Programme Ends');
}

//function to find the minimum number in the array
function myMin(myArray) {
    var min = myArray[0];
    for (var z = 0; z < myArray.length; z++) {
        if (myArray[z] < min) {
            min = myArray[z];
        }
    }
    return min;
}

function finish(){
    isStopped = true;
    alert('Max:' + myMax(numbers) + ' Min:' + myMin(numbers));
}

$(document).ready(function() {
    while(!isStopped){
        getNumber();
    }
});
<html>
    <title>Title</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="app.js"></script>
</head>
<body>
</body>
</html>

Upvotes: 1

Redu
Redu

Reputation: 26201

A straightforward approach could be

function getNumber() {
  var myNumber;
  do myNumber = prompt("Enter an unsigned base 10 number:"); 
  while (myNumber*1 < 0 && myNumber != "exit"); //loop will run again and again as long as the number is less than zero
    return myNumber;
}

function getMinMax(a){
  return [Math.min.apply(Math,a),Math.max.apply(Math,a)]
}

function doTheLoop(){
  var num = getNumber();
  num === "exit" ? alert("thanks. bye..!") : (arr.push(num*1),
                                              console.log(arr,getMinMax(arr)),
                                              doTheLoop())
}
var arr = [];
doTheLoop();

check the results at the console.

Upvotes: 0

David Hor&#225;k
David Hor&#225;k

Reputation: 5565

function getMinMaxFromUsersInput(){
    // User inputted numbers
    var numbers = [];

    // Get numbers till user ask to stop
    do {
       numbers.push(getNumber());
    } while(confirm("Continue?"));

    // Return min and max
    return {
       min: myMin(numbers),
       max: myMax(numbers)
    }
}

Upvotes: 1

Related Questions