0

Just started learning Javascript

example code:

while() {

var int1 = parseInt(prompt("Enter 1st number"));

var int2 = parseInt(prompt("Enter 2nd number"));

var int3 = parseInt(prompt("Enter 3rd number"));

var final = (int1 * 2) + (int2 * 3) + (int3 * 4);

document.write(final);

}

This prompting process will loop for lets say 3 times. There will be 3 var final values displayed.

How do I store the 3 var final values into an array and then display the highest var final value, the lowest var final value and the average var final value using document.write as well.

Pramod Gharu
  • 1,105
  • 3
  • 9
  • 18
LegoCamel
  • 45
  • 7
  • What is the problem you're facing while doing this? Create an array before loop, push result to array in each result. And then after iteration, compute required information, then display on screen. – niyasc Nov 27 '15 at 06:24

5 Answers5

1

Looping and pushing it into the array is like this;

var checkNumber = true, // Create a variable that eqausl true to start the while loop
    array = []; // creating the array

// Whenever checkNumber is true start!
while(checkNumber) {

var int1 = parseInt(prompt("Enter 1st number"));
array.push(int1) // Push int1 into the array
var int2 = parseInt(prompt("Enter 2nd number"));
array.push(int2) // Push int2 into the array
var int3 = parseInt(prompt("Enter 3rd number"));
array.push(int3) // Push int3 into the array
var final = (int1 * 2) + (int2 * 3) + (int3 * 4);
// Or like you have in final
// array.push(int1 * 2)
// Same for the rest!


// Checking with Math.max which number is greater
var largest = Math.max.apply(Math, array); 

alert(largest);

// Change the variable checkNumber to false to avoid a loop    
checkNumber = false;

}
Hakan Kose
  • 1,649
  • 9
  • 16
1

Actually, your provided code asks for 3 inputs within each cycle of the while() loop. Therefore, if you are only asking for 3 numbers, then you will not need the while() loop.

I've written a really basic set of codes below with 2 functions which find the maximum and the minimum value in the array.

var totalSum = 0;
// initialize the array with 0s.
var inputNums = [0, 0, 0];

// Loop through the input number array and ask for a number each time.
for (var i = 0; i < inputNums.length; i++) {
    inputNums[i] = parseInt(prompt("Enter Number #" + (i + 1)));
    //Add the number to the total sum
    totalSum = totalSum + inputNums[i];
}

// Calculate results
var average = totalSum / inputNums.length;

document.write("Total Inputs: " + inputNums.length + "<br>");
document.write("Sum: " + totalSum + "<br>");
document.write("Average: " + average + "<br>");
document.write("Max: " + findMaxValue(inputNums) + "<br>");
document.write("Min: " + findMinValue(inputNums) + "<br>");

function findMaxValue(numArray) {
    // Initialize with first element in the array
    var result = numArray[0];

    for(var i = 1; i < numArray.length; i++) {
        // loops through each element in the array and stores it if it is the largest
        if(result < numArray[i]) {
            result = numArray[i];
        }
    }
    return result;
}

function findMinValue(numArray) {
    // Initialize with first element in the array
    var result = numArray[0];

    for(var i = 1; i < numArray.length; i++) {
        // loops through each element in the array and stores it if it is the smallest
        if(result > numArray[i]) {
            result = numArray[i];
        }
    }
    return result;
}

Unfortunately the snippet system in SO does not support prompt().

I've put up a jsfiddle here: https://jsfiddle.net/hrt7b0jb/

shrmn
  • 1,303
  • 13
  • 19
  • Thank you for answering! My loop however is not based on a fixed number of times but rather a prompt for the user to choose the "number" of ints to enter. – LegoCamel Nov 27 '15 at 07:41
  • That's easy. I wrote the above code to be as flexible as possible. You just need to modify the declaration of the array var inputNums . – shrmn Nov 27 '15 at 07:43
0

declare an array variable outside the loop. push it to array with format like {iteration: i, value: ''} and sort the array after the loop with a compare function using item.value.

Tuan Anh Tran
  • 6,807
  • 6
  • 37
  • 54
  • Thanks but can you share an example code with reference to mine? I'm really a beginner! Much appreciated!! – LegoCamel Nov 27 '15 at 06:28
0

You can use the following code.

Notice I have modified prototype of Array object to add two new methods to get min and max (extracted from this answer). If you need to enhance this solution, you'll have to change the code a bit.

Array.prototype.max = function () {
    return Math.max.apply(null, this);
};

Array.prototype.min = function () {
    return Math.min.apply(null, this);
};

var arr = [];

arr[0] = parseInt(prompt("Enter 1st number"));

arr[1] = parseInt(prompt("Enter 2nd number"));

arr[2] = parseInt(prompt("Enter 3rd number"));

var final = (arr[0] * 2) + (arr[1] * 3) + (arr[2] * 4);


document.write(final + "<br/>");
document.write(Math.min.apply(null, arr) + "<br/>");
document.write(Math.max.apply(null, arr) + "<br/>");
document.write(arr.reduce(add, 0));

function add(a, b) {
    return a + b;
}

link to jsfiddle http://jsfiddle.net/gveuenhh/

Community
  • 1
  • 1
Nipuna
  • 6,846
  • 9
  • 64
  • 87
0
var array = [];
while() {

var int1 = parseInt(prompt("Enter 1st number"));

var int2 = parseInt(prompt("Enter 2nd number"));

var int3 = parseInt(prompt("Enter 3rd number"));

var final = (int1 * 2) + (int2 * 3) + (int3 * 4);

array.push(final);

document.write(final);

}
var min = array[0], max = array[0];
for(var i = 0; i < array.length; i++){
     console.log(min, max)
if(min > array[i+1]){
console.log("if")
min = array[i+1]
}
else if(max < array[i+1]){
console.log("elseif")
max = array[i+1]
}
else{}
}
Sapanjeet
  • 66
  • 2