[英]The 2nd biggest number of array
I am just trying to do this easy thing(make an array of numbers and then get the second biggest number of the same array) and I can't understand why it does not push chosen numbers from one array to the second one(from which it should print out the second biggest number). 我只是想做这个简单的事情(制作一个数字数组,然后获得同一数组的第二大数字),我无法理解为什么它不会将所选数字从一个数组推到第二个数组(从中它应该打印出第二大数字)。
var cisla = []; var i = 0; for(var i=0; i<5; i++){ var x = prompt("Put " + (i+1) + ". number"); cisla[i] = x; } var p = document.createElement("p"); p.innerHTML = "Your array: " + cisla; document.getElementsByTagName("body")[0].appendChild(p); var najvacsie; var index; var cisla2 = []; var dlzka = cisla.length; while(i<dlzka){ najvacsie = cisla[0]; for(var x=0; x<cisla.length; x++){ if(najvacsie < cisla[x]){ najvacsie = cisla[x]; index = cisla.indexOf(najvacsie); } } cisla2.push(najvacsie); cisla.splice(index, 1); i++; } var pp = document.createElement("p"); pp.innerHTML = "Ordered array: " +cisla2; document.getElementsByTagName("body")[0].appendChild(pp); var ppp = document.createElement("p"); ppp.innerHTML = "The 2nd biggest number of your array is: " + cisla2[1]; document.getElementsByTagName("body")[0].appendChild(ppp);
Thank you for your answers. 谢谢您的回答。
I made some changes, see comments. 我做了一些修改,看到了评论。
But just to show you the main problem in your code: 但只是为了向您展示代码中的主要问题:
if (najvacsie < cisla[x]){
najvacsie = cisla[x];
index = cisla.indexOf(najvacsie);
}
The problem is, if your najvacsie
has already the biggest value, than the index is never set, so the next 问题是,如果你的najvacsie
已经具有最大值,那么索引永远不会被设置,所以下一个
cisla.splice(index, 1);
is either spliced with the last value, or if undefined
it takes 0
as value for splicing (which could be expected behavior in the first round). 用最后一个值拼接,或者如果undefined
则拼接为0
(这可能是第一轮中的预期行为)。
Now here is the working model: 现在这里是工作模型:
var array = []; for (var i = 0; i < 5; i++) { var x = prompt("Put " + (i + 1) + ". number"); array[i] = parseInt(x, 10); // otherwise it will sorted by string } var p = document.createElement("p"); p.innerHTML = "Your array: " + array; document.getElementsByTagName("body")[0].appendChild(p); var tempValue; var index; var orderedArray = []; while (array.length) { // that is shorter tempValue = array[0]; for (var x = 0; x < array.length; x++) { if (tempValue < array[x]) { tempValue = array[x]; } } index = array.indexOf(tempValue); // move this here orderedArray.push(tempValue); array.splice(index, 1); } //orderedArray = array.slice(0).sort().reverse(); // that may be better ... var pp = document.createElement("p"); pp.innerHTML = "Ordered array: " + orderedArray; document.getElementsByTagName("body")[0].appendChild(pp); var ppp = document.createElement("p"); ppp.innerHTML = "The 2nd biggest number of your array is: " + orderedArray[1]; document.getElementsByTagName("body")[0].appendChild(ppp);
You can do it in linear time (I'll try to come up with a generic way to do it later) 你可以在线性时间内完成它(我会尝试用一种通用的方法来完成它)
function secondBiggestOf(array) {
var biggest = -Infinity;
var secondBiggest = -Infinity;
for (var i = 0, l = array.length; i < l; i++) {
var current = array[i];
if (biggest < current) {
var tmp = biggest;
biggest = array[i];
if (secondBiggest !== -Infinity) {
secondBiggest = tmp;
}
} else if (secondBiggest < current) {
secondBiggest = current;
}
}
return secondBiggest;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.