Jessica
Jessica

Reputation: 189

Finding largest element in an array

I am trying to write a JavaScript program that will display the largest number out of 10 numbers inputted by the user. This is what I have so far but it isn't working.

var counter = 1;
var number = new Array();
number.length = 9;
var largest = 0;

while (counter <= 10) {
    number = window.prompt("Enter Numbers 1-10 Number:" + counter + ".");
    number[counter] = parseInt(number);
    counter++;
}

largest = Math.max.apply(Array);
document.writeln("<h1>Largest number is " + largest + "</h1>");

Upvotes: 4

Views: 4851

Answers (5)

Ted Hopp
Ted Hopp

Reputation: 234795

The main problems with your code are:

  1. You are missing a this argument from the Math.max.apply function call.

  2. You are destroying the number array every time you read a new value from the user. You need a separate variable for the user input.

However, you don't really need to build an array at all for this task. Inside the loop, just compare largest with the number just supplied by the user and update largest if appropriate:

var counter;
var number;
var largest = Number.NEGATIVE_INFINITY;

for (counter = 1; counter <= 10; counter++) {
    number = window.prompt("Enter Numbers 1-10 Number:" + counter + ".");
    number = parseInt(number);
    if (number > largest) {
        largest = number;
    }
}

document.writeln("<h1>Largest number is " + largest + "</h1>");

Upvotes: 1

carter
carter

Reputation: 5432

var counter = 1;
var number = [];
var largest = 0;

while (counter <= 10) {
    number.push(Number(window.prompt("Enter Numbers 1-10 Number:" + counter + "."), 10));
    counter++;
}

Array.prototype.max = function () {
    return Math.max.apply(Math, this);
};

largest = number.max();
document.writeln("<h1>Largest number is " + largest + "</h1>");

Upvotes: 1

McGarnagle
McGarnagle

Reputation: 102753

  1. You're mixing up the array with the temporary variable to hold the input.
  2. apply takes two arguments, the context (irrelevant in this case) and the arguments array.

Should be like this:

var counter = 1;
var arr = [];
var count = 3;

while (counter <= count) {
    number = window.prompt("Enter Numbers 1-" + count + " Number #" + counter + ": ");
    if (number === null) break;
    arr[counter++] = parseInt(number);
}

if (arr.length > 0) {
    var largest = Math.max.apply(Array, arr);
    alert("<h1>Largest number is " + largest + "</h1>");
}

Upvotes: 2

Snowburnt
Snowburnt

Reputation: 6922

You have several issues:

  1. Arrays are 0 indexed. You skipped the 0 index by starting the counter at 0, this will screw up the array calculation. If you go with Math.max.apply(array, number), it will work without the 0 indexing.

  2. You overwrote the number variable with every prompt, either use the window.prompt to feed into the parseInt or feed it into a temporary variable.

  3. You had incorrect syntax for the apply variable.

var counter = 0;
var number = new Array();
number.length = 9;
var newnumber;
var largest = 0;

while (counter <= 10) {
    newnumber = window.prompt("Enter Numbers 1-10 Number:" + counter + ".");
    number[counter] = parseInt(newnumber);

    counter++;
}

largest = Math.max.apply(Math, number);
document.writeln("<h1>Largest number is " + largest + "</h1>");

Upvotes: 2

Moritz Roessler
Moritz Roessler

Reputation: 8611

You're simply missing the expected thisArg for Function.prototype.apply.

The Syntax, as described by MDN is

fun.apply(thisArg[, argsArray])

As shown in this simple example

Math.max.apply (null,[5,4,3,7,9,]) //9
                ^^^^

where null is used in the example for simplicity, as Math doesn't expect an specific context

What you are trying to do is passing the array number to Math.max, which would then be the thisArg (though you seem to have it mistaken with Array) which would result either way in Math.max being called with zero arguments, which yields according to §15.8.2.11

Given zero or more arguments, calls ToNumber on each of the arguments and returns the largest of the resulting values.

  • If no arguments are given, the result is −∞.

Upvotes: 2

Related Questions