[英]Javascript var prompt into Array, document.write(Max/Min/Average);
剛開始學習Javascript
示例代碼:
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);
}
這個提示過程將循環播放3次。 將顯示3個var最終值。
如何將3個var最終值存儲到數組中,然后也使用document.write顯示最高var最終值,最低var最終值和平均var最終值。
循環並推入數組就像這樣;
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;
}
實際上,您提供的代碼在while()
循環的每個周期內要求3個輸入。 因此,如果只要求輸入3個數字,則不需要while()
循環。
我在下面編寫了一套非常基本的代碼,其中包含2個函數,它們在數組中找到最大值和最小值。
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;
}
不幸的是,SO中的代碼段系統不支持prompt()
。
我在這里放了一個jsfiddle: https ://jsfiddle.net/hrt7b0jb/
在循環外聲明一個數組變量。 將其推送到格式為{iteration: i, value: ''}
的數組,並在循環后使用item.value
的compare函數對數組進行排序。
您可以使用以下代碼。
注意,我修改了Array對象的原型,以添加兩個新方法來獲取min和max(從此答案中提取)。 如果您需要增強此解決方案,則必須稍作更改。
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;
}
鏈接到jsfiddle http://jsfiddle.net/gveuenhh/
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{}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.