alik33
alik33

Reputation: 141

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.

Upvotes: 3

Views: 93

Answers (2)

Nina Scholz
Nina Scholz

Reputation: 386654

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

Upvotes: 2

axelduch
axelduch

Reputation: 10849

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;
}

Upvotes: 0

Related Questions