刚开始学习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 final值存储到数组中,然后使用document.write显示最高var final值,最低var final值和average var final值。
答案 0 :(得分:1)
循环并将其推入阵列是这样的;
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;
}
答案 1 :(得分:1)
实际上,您提供的代码会在while()
循环的每个循环中请求3个输入。因此,如果您只需要3个数字,那么您将不需要while()
循环。
我在下面编写了一组非常基本的代码,其中包含两个函数,可以找到数组中的最大值和最小值。
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()
。
我在这里提出了一个问题:https://jsfiddle.net/hrt7b0jb/
答案 2 :(得分:0)
在循环外声明一个数组变量。将其推送到格式为{iteration: i, value: ''}
的数组,并使用item.value
使用比较函数对循环后的数组进行排序。
答案 3 :(得分:0)
您可以使用以下代码。
注意我修改了Array对象的原型,添加了两个新方法来获取min和max(从this answer中提取)。如果您需要增强此解决方案,则必须稍微更改一下代码。
config.asset_host = 'http://example.com'
链接到jsfiddle http://jsfiddle.net/gveuenhh/
答案 4 :(得分: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{}
}