Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 295
alik33 Avatar asked Jan 22 '26 08:01

alik33


1 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

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).

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);
like image 110
Nina Scholz Avatar answered Jan 23 '26 22:01

Nina Scholz