4

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

3
  • 1
    Have you read anything about the javascript arrays? Commented Oct 11, 2013 at 16:20
  • I am new to Javascript, Just started the chapter this week Commented Oct 11, 2013 at 16:34
  • 1
    For a newcomer, this question is pretty well formatted as well as it includes information on what you have tried already along with the code. +1 for that. Make sure to read the about page it includes quite useful information :) Commented Oct 11, 2013 at 17:33

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

2

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 −∞.

Comments

2
  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>");
}

Comments

1

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

1 Comment

It's always better to add some code to explain what you actually did.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.