Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    HTML Programming Maximum Number With Window Prompt?

    This is what I have so far which prompts the user three numbers and then displays the max of the numbers and all three of them.

    Now the last thing I was wanting to do is change it from three prompts to just one prompt. How could I prompt to type in three numbers in one prompt and get the max from those three numbers.? Any help would be greatly appreciated. Thank you in advance!



    Code:
    <head>
    <title>Maximum Number</title>
    </head>
    
    <body>
    <center><h2>Maximum Number</h2>
    <script language="Javascript" type="text/javascript"> 
    
    function Max(num1, num2,num3)
    {
    var largest=arguments[num1, num2, num3]
    for (i=0; i<arguments.length; i++)
    {
    if ((num1>num2)&&(num1>num3))
    largest=num1
    else if((num2>num1)&&(num2>num3))
    largest=num2
    else
    largest=num3
    }
    return(largest)
    }
    
    document.write("</br>")
    var num1=prompt("Enter the first number: ", "")
    var num2=prompt("Enter the second number: ", "")
    var num3=prompt("Enter the third number: ", "")
    var max= Max(num1, num2, num3)
    document.write("You entered: ", num1, ", ", num2, ", ", num3)
    document.write("</br>")
    document.write("The Maximum number is: ", max)
    </script></center>
    
    </body>
    </html>

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,918
    Thanks
    56
    Thanked 545 Times in 542 Posts
    I'm going to start applying for equivalency credits on these homework assignments - I'll graduate yet dagnammit.

    Code:
    <html>
    <head>
    <title>Maximum Number</title>
    </head>
    <body>
    <center><h2>Maximum Number</h2>
    <div id="res"></div>
    </center>
    <script type="text/javascript"> 
    var nums=prompt("Enter three numbers separated by commas: ", "")
    var numarray=nums.split(",")
    var maxnum=Math.max.apply(Math, numarray);
    document.getElementById("res").innerHTML="You entered: "+numarray[0]+", "+numarray[1]+", "+numarray[2]+"<br>";
    document.getElementById("res").innerHTML+="The Maximum number is: "+maxnum;
    </script>
    </body>
    </html>
    notes:
    - IRL you'd probably want to add some validation that they are in fact numbers being entered
    - document.write() is either for hacks or losers, depending on who you ask
    - prompt() isn't that far behind
    - <center> runs a close third
    - a decent javascript reference should point you towards things like the Math object, which will save you from writing your own functions to accomplish what you can do easily with its built in methods. If your book/website/teacher doesn't tell you about them it is time to get a new one.

  • #3
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    You can simplify your code with by

    Code:
            
            <script>
    
                var strNums = window.prompt('Enter 3 numbers separated by a space','');
                if(strNums != null && strNums != ''){
                    var strOut = '';
                    var nums = strNums.split(/\s+/);
                    for(i=0; i<nums.length; i++){
                        if(Number(nums[i]) != 'NaN'){
                            strOut += nums[i]+' ';
                        }
                    }
                    document.write('You entered '+strOut);
                    document.write('Max = '+Math.max.apply(Math, nums));
                }
    
            </script>
    but document.write should be used only for learning purposes. For writing to the page it's better to use the proper DOM methods like createElement() etc.
    Last edited by Mishu; 03-27-2012 at 06:14 AM.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    You need to allow for the situation where the user enters fewer or more than 3 numbers.

    Code:
    <script type = "text/javascript">
    
    var strNums = window.prompt('Enter 3 numbers separated by a space','');
    if(strNums != null && strNums != '') {
    var strOut = '';
    var nums = strNums.split(/\s+/);
    for(i=0; i<3; i++) {
    nums[i] = Number(nums[i]) || 0;
    strOut += nums[i]+' ';
    }
    document.write('You entered '+strOut);
    var max = Math.max(nums[0], nums[1], nums[2]);
    document.write("The maximum value is " + max)
    }
    
    </script>

    Quizmaster: At more than 250 miles in length, which motorway is the longest on the UK road network?
    Contestant: Route 66

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Philip M View Post
    You need to allow for the situation where the user enters fewer or more than 3 numbers.
    In a real situation that's correct and how you handle that would need to specified in the business rules for the website. I don't think the best option is to blindly assume the first 3 are the intended numbers or a 0 if fewer than 3 numbers. The user might be suffering from "fat fingers" and really meant the 1st, 2nd and 4th numbers to be their 3 numbers. So if they enterd 4 numbers I would ask then which ones do they really want.

    Where something other that 3 numbers are entered I would normally suggest to the client that the user be made to enter the 3 numbers or cancel out.
    Last edited by Mishu; 03-27-2012 at 08:40 AM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Mishu View Post
    In a real situation that's correct and how you handle that would need to specified in the business rules for the website. I don't think the best option is to blindly assume the first 3 are the intended numbers or a 0 if fewer than 3 numbers. The user might be suffering from "fat fingers" and really meant the 1st, 2nd and 4th numbers to be their 3 numbers. Where something other that 3 numbers are entered I would normally suggest to the client that the user be made to enter the 3 numbers or cancel out.
    Ah, same old argumentative bullant/webdev1958, ever ready to start a pointless disagreement. It beats me why the mods do not eject you (yet again).

    This is homework, yes? The idea is to identify the maximum of three numbers. There are no "business rules for the website". No business would use prompt() as the way of entering data. But your poorly-thought out code allowed the user to enter 2 or 4 numbers, and thus return a silly answer.
    Last edited by Philip M; 03-27-2012 at 08:53 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Philip M View Post
    This is homework, yes?
    I do not know. But in any case the op didn't state how it was to be handled and so I don't see the point in guessing how. I just explained what I would suggest, and why, in a real situation and that is to make the user enter 3 numbers if other than 3 numbers were entered.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Mishu View Post
    I do not know. But in any case the op didn't state how it was to be handled and so I don't see the point in guessing how. I just explained what I would suggest, and why, in a real situation and that is to make the user enter 3 numbers if other than 3 numbers were entered.
    What else would it be except homework? Please stop being so childish. But it would be very simple to check the length of the nums array and if it is not 3 return an error message. There are many ways of skinning a cat, and your habit of arguing the toss about nothing continues to weary me. Go away, troll!
    Last edited by Philip M; 03-27-2012 at 09:01 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Philip M View Post
    What else would it be except homework?
    But even if it is, so what? It could be the op later wants to change the number of entries from 3 to something else. With code I posted you only have to change the 3 in propmt box to something else. The rest of the js remains the same. With what you posted you would have to make at least 2 changes to your code.

    The aim of my example was to make it as flexible as possible with minimal code changes if the number of entries needed to be changed.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Mishu View Post
    But even if it is, so what? It could be the op later wants to change the number of entries from 3 to something else. With code I posted you only have to change the 3 in propmt box to something else. The rest of the js remains the same. With what you posted you would have to make at least 2 changes to your code.

    The aim of my example was to make it as flexible as possible with minimal code changes if the number of entries needed to be changed.
    Horsepoo and bluster! Why do you now want to start guessing? If the OP changes 3 to 4 in the prompt box in your code it is still poorly thought-out and amateurish.
    So 2 changes are needed to my code? - shock! horror! For heaven's sake - grow up!
    Last edited by Philip M; 03-27-2012 at 09:10 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Philip M View Post
    So 2 changes are needed to my code? - shock! horror! For heaven's sake - grow up!
    But even if I added the code to ask again if other than the required numbers was entered I would still need to make only 1 change to my code if 3 was changed to something else.

    Why do you have an issue with me showing the op example code where only 1 change would be needed if they wanted to change the number of entries?

    Quote Originally Posted by Philip M View Post
    So 2 changes are needed to my code?
    it's actually at least 3.

    1. The prompt text

    2. the maximum value of i in your for loop

    3. the number of array elements in your Math.max()
    Quote Originally Posted by Philip M View Post

    Code:
    <script type = "text/javascript">
    
    var strNums = window.prompt('Enter 3 numbers separated by a space','');
    if(strNums != null && strNums != '') {
    var strOut = '';
    var nums = strNums.split(/\s+/);
    for(i=0; i<3; i++) {
    nums[i] = Number(nums[i]) || 0;
    strOut += nums[i]+' ';
    }
    document.write('You entered '+strOut);
    var max = Math.max(nums[0], nums[1], nums[2]);
    document.write("The maximum value is " + max)
    }
    
    </script>
    Last edited by Mishu; 03-27-2012 at 10:31 AM.

  • #12
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    to the op:

    This is an off the cuff example of making the user enter the required number of numbers and if you want to change the number of required numbers the only single change you need to make is resetting reqNums = 3

    Code:
            <script>
                
                var reqNums = 3, nums = [];
                do {
                    var strNums = window.prompt('Enter '+reqNums+' numbers separated by a space','');
                    count=0;
                    if(strNums != null && strNums != ''){
                        var strOut = '';
                        strNums.replace(/^\s+|\s+$/g, '');
                        nums = strNums.split(/\s+/);
                        for(i=0; i<nums.length; i++){
                            if(!isNaN(parseFloat(nums[i]))){
                                strOut += nums[i]+' ';
                                ++count;
                            }
                        }   
                    }  
            }  while(count != reqNums && nums.length > 0 && strNums != null)
                if(count == reqNums){
                    document.write('You entered '+strOut);
                    document.write('Max = '+Math.max.apply(Math, nums));
                }
    
            </script>
    I haven't fully tested it with all input possibilities but hopefully you can see the concept behind what I have done which is the aim of the example.

    I haven't run pm's code in post 11, but it will need at least 3 changes to the code if you vary the required numbers.
    Last edited by Mishu; 03-27-2012 at 10:37 AM.

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Mishu View Post
    Why do you have an issue with me showing the op example code where only 1 change would be needed if they wanted to change the number of entries?

    It's actually at least 3.
    Because you are an evil troll, banned multiple times from this forum (but for some unfathomable reason you seem to be allowed by the mods to crawl back in again), always seeking a confrontation or an argument.

    It is not at least 3. It is exactly 3. So what? As I say, do try and grow up (although I realise that is probably impossible for you now). There is not the slightest suggestion from anyone but you that the OP wants to change the number of numbers - so as usual your objection is artificial and manufactured.
    Last edited by Philip M; 03-27-2012 at 11:08 AM. Reason: Typo

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #14
    Banned
    Join Date
    Mar 2012
    Posts
    306
    Thanks
    1
    Thanked 28 Times in 28 Posts
    Quote Originally Posted by Philip M View Post
    There is not the slightest suggestion from anyone but you that the OP wants to change the number of numbers -
    I didn't say the op wants to change anything. I suggested that if in the future the op does want to change the number of entries then they would need to make only 1 change to the example I posted. I always aim to write as flexible and as low maintenace as possible code. That's how I was taught. I don't see the point of imposing 3 changes when 1 is all that is needed.
    Last edited by Mishu; 03-27-2012 at 11:18 AM.

  • #15
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Once more, so what, evil troll?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •