2016-11-27 76 views
-1

如何添加到此程序中以查找最高和最低数字?我尝试了几个不同的东西,但它只是不断给我输入的最后一个数字。如何在javascript中查找循环中的最高和最低数字

<meta charset="UTF-8"> 
<title>Laskenta</title> 
<script> 

var yht=0; //sum 
var luku=0; //number 
var laske; //count 

laske=Number(prompt("how many numbers would you like to add?")) 
for (var i=0; i<laske; i++){luku = Number(prompt("give number", "number")); 

yht=yht+luku; 

} 
while(i<laske); 

document.write("the sum of the numbers you entered is " ,yht, "<br>"); 
document.write (" and the average is o " + yht/laske); 

我翻译了芬兰语的大部分内容,并放在旁边的芬兰语中。 任何帮助将不胜感激。 谢谢

回答

0

第一组操作解决了您希望在循环中执行这些任务的需求,但您并不需要传统的循环来完成这些任务。新的spread operator along with Math.maxArray.prototype.reduce()方法可以轻松获得最大值,总和或平均值。

var result = null; 
 
var nums = []; 
 

 
// Remember, a propmt will always return a string, you must convert that 
 
// to a number if you want to do math with it. 
 
var count = parseInt(prompt("How many numbers do you want to work with?"), 10); 
 

 
// Build up the input values into an array: 
 
for(var i = 0; i < count; ++i){ 
 
    nums.push(parseInt(prompt("Enter number " + (i + 1)),10)); 
 
} 
 

 
// The "traditional" way to get the max value from a loop would be to 
 
// compare the value that you are iterating and the next value in the 
 
// array and store the higehr one: 
 
var max = null; 
 
var sum = 0; 
 
for(var x = 0; x < nums.length-1; ++x){ 
 
    max = (nums[x] > nums[x + 1]) ? nums[x] : nums[x + 1]; 
 
    sum += nums[x]; 
 
} 
 
console.log("Max number is: " + max); 
 

 
var sum = 0; 
 
for(var y = 0; y < nums.length; ++y){ 
 
    sum += nums[y]; 
 
} 
 
console.log("Sum is: " + sum); 
 
console.log("Average is: " + sum/nums.length); 
 
    
 
// ******************************************************************************* 
 
// But, we have much better ways of doing these jobs: 
 
console.log("Max number is: " + Math.max(...nums)); 
 

 
result = nums.reduce(function(accumulator, currentValue, currentIndex, array) { 
 
    return accumulator + currentValue; 
 
}); 
 

 
console.log("The sum is: " + result); 
 
console.log("The average is: " + result/nums.length);

+0

谢谢你的回复。我刚刚开始,这个问题来自循环的话题。它表示作为暗示采取第一个数字并将其用作比较对象。我怎样才能比较未知?我尝试将它们设置为0,但随后我都会返回作为输入的最后一个数字。 – sueT

+0

@sueT看到我更新的答案。 –

0

var yht=0; //sum 
 
var luku=0; //number 
 
var laske; //count 
 
var highest; 
 
var lowest; 
 

 
laske=Number(prompt("how many numbers would you like to add?")) 
 
for (var i=0; i<laske; i++){luku = Number(prompt("give number", "number")); 
 
if (i == 0) { 
 
highest = luku; 
 
lowest = luku; 
 
} 
 
else { 
 
    if (luku > highest) highest = luku; 
 
    if (luku < lowest) lowest = luku; 
 
} 
 
yht=yht+luku; 
 

 
} 
 
while(i<laske); 
 

 
document.write("the sum of the numbers you entered is " ,yht, "<br>"); 
 
document.write (" and the average is o " + yht/laske); 
 
document.write("<br />Highest value="+highest); 
 
document.write("<br />Lowest value="+lowest);

添加两个变量的代码追踪进入最高值和最低值。将输入的第一个数字设置为最高和最低。那么遇到新的低点时,请更换最低点。遇到新的高值时,请更换最高值。

+0

arghhh谢谢!我可以踢自己!我已经这样做了,但没有写if(i == 0)位。我只是不断收回我输入的最后一个号码。我已经坐了几个小时试图弄清楚。 :D – sueT

+0

很高兴提供帮助。如果确实回答了你的问题,请接受我的回答。 –

相关问题