-1

As the title suggests, i have to write a program where the user enters numbers and when the number entered is 0, it outputs the highest number which was entered. i've done most of it and i know there is only a little missing but i cant figure it out.

<!DOCTYPE html>
<html>
<head> <meta charset="utf-8" />
<title></title>

<script style="text/javascript">
    function numbers(){
    var numbers = 0;
    var outMessage = "";
    var numbersArray = new Array(numbers);

    do{
        numbers = prompt('enter 0 to cancel', 0);
    }while(isNaN(numbers));






    var maxNum = Math.max.apply(null, arr);
    alert(maxNum);
    document.getElementById('result').innerHTML = maxNum;

    }
</script>
<body>
<p>Numbers Exercise</p>
<p id="numbers"></p>
<input type="button" value="Start" onClick = "numbers();">

<label id='result' />
</body>
</html>
prince
  • 1
  • 6
  • What is the issue? – TeaCoder Apr 02 '16 at 01:47
  • i dont know how to go any further, im not great at js. im really new to it – prince Apr 02 '16 at 01:48
  • You create an array `numbersArray` then you add nothing to it and then you `Math.max` on an undeclared variable `arr`? Is that about it? – Wainage Apr 02 '16 at 01:48
  • i dont know, can you show me please – prince Apr 02 '16 at 01:51
  • Where's this code from? – Wainage Apr 02 '16 at 01:52
  • What was wrong with the answer [the last time you asked this question](https://stackoverflow.com/questions/36359495/user-enters-amount-on-numbers-then-output-the-highest-number-javascript)? – Andy Apr 02 '16 at 01:54
  • its from a previous working program i have. i just tried to modify it. – prince Apr 02 '16 at 01:56
  • nothing andy, its a new excersise. you would be suprised at how many i have managed to do today – prince Apr 02 '16 at 01:56

2 Answers2

0

One of your issues is that you write var maxNum = Math.max.apply(null, arr); but you mean var maxNum = Math.max.apply(null, numbersArray);. I think that was just forgetting to change that array name.

Then in your do while you are checking NaN(numbers) which will always return false if they type in valid input. You want to check `!(NaN(numbers)) so the loop quits when the number is invalid

I then check if(numbers != 0) inside your do while, determining if you want to push it into an array using numbersArray.push(numbers) and if not then break the loop.

I created a JSFiddle of this: https://jsfiddle.net/4otwrnzL/

theblindprophet
  • 7,767
  • 5
  • 37
  • 55
  • No problem, you just made some naive errors. One helpful thing would be to hit F12 on your keyboard. This will bring up the developer console which will show you compile and runtime errors. – theblindprophet Apr 02 '16 at 01:59
0

I think this does the job, do sure if this is what you want.

<!DOCTYPE html>
<html>
<head> <meta charset="utf-8" />
<title></title>

<script style="text/javascript">
    function numbers(){
    var numbers = 0;
    var outMessage = "";
    var numbersArray = new Array(numbers);

    do{
        var number = prompt('enter 0 to cancel', 0);
        numbersArray.push(number);
    }while(isNaN(number)||number != 0);

    var maxNum = Math.max.apply(null, numbersArray);
    alert(maxNum);
    document.getElementById('result').innerHTML = maxNum;
    }
</script>
<body>
<p>Numbers Exercise</p>
<p id="numbers"></p>
<input type="button" value="Start" onClick = "numbers();">

<label id='result' />
</body>
</html>
Zhiliang Xing
  • 1,057
  • 1
  • 8
  • 19
  • This almost works. If 0 is the first number it will push the 0 into the array. Which of course is not a big deal, depends on how he wants to approach the problem. – theblindprophet Apr 02 '16 at 02:04