简体   繁体   English

数组中最高和最低的问题

[英]problems with highest and lowest in an array

I have got this code working just as the exercise asked except for the highest and lowest values. 除了最高和最低值之外,我的代码工作正如练习所要求的那样。 I have tried doing the max and min thing but Im either putting it in the wrong place or missing something. 我试过做最大和最小的事情,但我要么把它放在错误的地方或丢失的东西。 The way it is now works apart from that it will if you enter for example 1 , 2 , 3, 11, 20 it says the highest is 3. 它现在的工作方式除了它将如果你输入例如1,2,3,11,20,它表示最高为3。

It needs to come back as such that it shows what you entered and then sorts it, it shows the sum of the numbers and then the highest and lowest. 它需要回来,它显示你输入的内容,然后对其进行排序,它显示数字的总和,然后是最高和最低。

Where am I going wrong? 我哪里错了? Im guessing I should be using max and min but I just cant seem to get it to work using those. 我猜我应该使用max和min但是我似乎无法使用它们来使用它。

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



var yht=0; //total
var luku=0; //numbers entered
var suurin;// largest number entered
var pienin; // smallest number entered
var i;//counter

var heitto=new Array(5);//heitto= throw

for( var i = 0; i < heitto.length; i++) {
heitto[i]=prompt("anna ", i + 1 + " . heitto") 

document.write ("Index " + i + ": " + "heitto = "  + heitto[i]+"<br>");

if (i == 0) {
suurin = heitto[i];
pienin = heitto[i];
}
else {
  if (heitto[i] > suurin) suurin = heitto[i];
  if (heitto[i] < pienin) pienin = heitto[i];
}
yht+=parseInt(heitto[i]);
} 

document.write ("<br>" + "suurin - pienin heittojen " + heitto.sort(function(a, b){return b-a}) + "<br>");
document.write("<br>" +"Syöttämiesi heittojen summa on " ,yht, "<br>");
document.write("<br />Suurin heitto on "+suurin);
document.write("<br />Pienin heitto  on "+pienin);
</script>

Basically you have two issues: 基本上你有两个问题:

  • Strings instead of numerical values for input, thats why 3 is the biggest value, but only when 3 is a string. 字符串而不是输入的数值,这就是为什么3是最大值,但只有当3是一个字符串时。 Order by strings '1', '11', '2', '20', '3' . 按字符串'1', '11', '2', '20', '3'

     heitto[i] = +prompt("anna ", i + 1 + " . heitto"); // get the numerical value of input // ^ 
  • Results for comparisons does not have the right start value. 比较结果没有正确的起始值。

     suurin = -Infinity; // initialize with a small number pienin = Infinity; // initialize with a big number 

In the loop for getting the highest and the lowest value, you are compare strings, then you get the result, you have, because of the order of strings. 在获取最高值和最低值的循环中,你是比较字符串,然后你得到结果,因为字符串的顺序。 For a numerical comparison, it is better to convert both values in advance to a numerical value with either implicit casting to Number with unary + , or explicit with parseInt , parseFloat or Number . 对于数值比较,最好将两个值预先转换为数值,隐式转换为带有一元+ ,或带有parseIntparseFloatNumber显式。

Your sort function is using an implicit casting to number with - . 您的排序函数使用隐式转换来编号- Taht is the reason why you get a numericl sorted array instead of an array with the sort order like above. Taht是你得到一个numericl排序数组而不是像上面这样的排序顺序的数组的原因。

 var yht = 0; //total var luku = 0; //numbers entered var suurin; // largest number entered var pienin; // smallest number entered var i; //counter var heitto = new Array(5);//heitto= throw suurin = -Infinity; // initialize with a small number pienin = Infinity; // initialize with a big number for (var i = 0; i < heitto.length; i++) { heitto[i] = +prompt("anna ", i + 1 + " . heitto"); // get the numerical value of input document.write("Index " + i + ": " + "heitto = " + heitto[i] + "<br>"); if (heitto[i] > suurin) { suurin = heitto[i] }; if (heitto[i] < pienin) { pienin = heitto[i] }; yht += parseInt(heitto[i]); } document.write("<br>" + "suurin - pienin heittojen " + heitto.sort(function (a, b) { return b - a }) + "<br>"); document.write("<br>" + "Syöttämiesi heittojen summa on ", yht, "<br>"); document.write("<br />Suurin heitto on " + suurin); document.write("<br />Pienin heitto on " + pienin); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM