views:

388

answers:

6

"Find the two largest values among the 10 digits entered. p.s. Use 'prompt' to get the integer from user."

I would like to solve this question by "array", not "if", what should I do?

I have tried, but error.....

<script type="text/javascript">
var a, b, c;
var arr = new Array[3];
arr[0] = a;
arr[1] = b;
arr[2] = c;
a = window.prompt("Enter a number");
b = window.prompt("Enter a number");
c = window.prompt("Enter a number");
arr.sort(function(a,b){return a - b})
window.alert(arr[arr.length-1] + " and " + arr[arr.length-2]) 
</script>
+2  A: 

Sort the array and use the last 2 values as the answer. Is this homework?

astander
Yes, I have done it by "if", but I think "array" is better.I have tried to get second largest integer by "array", but it error when I add "prompt" box.
Programme Newbie
+1  A: 

Loop over the array, keeping track of the two largest encountered so far.

outis
+3  A: 

Iterate over the array, find the largest number, remember it somewhere, remove it, repeat.

Aaron Digulla
A: 

use javascript sort function: array.sort(). since this sorts alphabetically by default you're gonna have to define a sort function like this:

var theArray=[3,4,26,7]
theArray.sort(function(a,b){return b - a;})

then you'll have to get the second value in the array since the function sorts it in descending order

ive taken this from your code above. try this:

<script type="text/javascript">
var a, b, c;
var arr = new Array();
a = window.prompt("Enter a number");
arr[0] = a;
b = window.prompt("Enter a number");
arr[1] = b;
c = window.prompt("Enter a number");
arr[2] = c;

arr.sort(function(a,b){return a - b})
window.alert(arr[arr.length-1] + " and " + arr[arr.length-2]) 
</script>

you were inserting values that are not yet defined. you'll have to let the user enter the value first before putting it in the array

uji
but how to get number from prompt boxes and no error?I have tried before but error...
Programme Newbie
could you be more specific of what you're trying to do? from what i understand you'll be accepting an input from the user lets say this string: 3,4,26,7 .. you convert to array using string.split(',').. problem here is you'll have to find a way so that the user will write the integers separated by commas or any separator you like
uji
Thank you for correct my error.
Programme Newbie
From w3schools..."numbers will not be sorted correctly (40 comes before 5). To sort numbers, you must add a function that compare numbers."
Ben Shelock
+1  A: 

Here it is:

Array.prototype.simpleSortInsert = function (item) {
/*  Insert an element in an array keeping it sorted. Only supports numbers. */
    for (var i = 0, l = this.length, didit = false; i < l; ++i) {
        if (this[i] > item) {
            this.splice(i, 0, item);
            didit = true;
            break;
        }
    }
    if (!didit) {
        this.push(item);
    }
    return i;
};


var myArray = [], limit = 10;
for (var i = 0; i < limit; ++i) {
    myArray.simpleSortInsert(parseInt(prompt('Please enter a number (' + (i + 1) + '/10):')));
}

alert('The two largest numbers you entered are ' + myArray[limit - 1] + ' and ' + myArray[limit - 2]);
Kaze no Koe
Thanks, this is a very good example for me.
Programme Newbie
A: 

Try the following:

var arr=new Array();
var i=0;
var v1;
var v2;

arr[0]=10;
arr[1]=8;
arr[2]=2;
arr[3]=1;
arr[4]=4;
arr[5]=3;
arr[6]=9;
arr[7]=7;
arr[8]=5;
arr[9]=6;

for(i=0;i<10;i++)
{
if(i<2)
{
if(arr[0]<arr[1])
{
v1=arr[1];
v2=arr[0];
}
else
{v1=arr[0];
v2=arr[1];
}
}
else
{
if(arr[i]>v1)
{
v2=v1;
v1=arr[i];
}
else if(arr[i]<v1 && arr[i]>v2)
{
v2=arr[i];
}
}
}
document.write(v1);
document.write(v2);
Himadri