2016-11-27 75 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

很高興提供幫助。如果確實回答了你的問題,請接受我的回答。 –

相關問題