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.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Assistance needed

    I am needing to use javascript to show a prompt, and when the user enters a number from 1-12 (1 for each month of the year) it adds it to a new array. Then when the user types "stop" it prints the corresponding months.

    I'm new to javascript so I am not quite sure how to go about doing it. This is what I have currently, but I don't think it is right.
    Code:
    <script type="text/javascript">
    
    
    var month=['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    
    var selmonth= new Array();
    
    
    
    
    
    var month2=prompt('Which Month', '');
    
    
    if (month2==1){
    selmonth[0] = month[1];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==2){
    selmonth[1] = month[2];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==3){
    selmonth[2] = month[3];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==4){
    selmonth[3] = month[4];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==5){
    selmonth[4] = month[5];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==6){
    selmonth[5] = month[6];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==7){
    selmonth[6] = month[7];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==8){
    selmonth[7] = month[8];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==9){
    selmonth[8] = month[9];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==10){
    selmonth[9] = month[10];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==11){
    selmonth[10] = month[11];
    var month2=prompt('Which Month', '');
    }
    
    if (month2==12){
    selmonth[11] = month[12];
    var month2=prompt('Which Month', '');
    }
    
    
    if (month2=='stop'){
    document.write(selmonth.join(" <br> "));
    }
    
    
    else{
    	alert('Please enter a valid value');
    }
    
    
    
    
    </script>
    It'd be great if someone is able to help me.
    Thanks.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    All that repetition ought to be a hint that you are doing this very inefficiently. And your script allows for only one entry, and has no way of displaying the result.

    There are many ways of approaching this - here is an example script for you to study and learn from. This prompts the user repeatedly until he enters 0 to finish, but there is nothing to prevent the user from entering the same month number twice (or more).

    Code:
    <html> 
    <head> 
    <body>
    
    <script type="text/javascript">
    
    var month=['', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var selmonth= new Array();
    var count = 0;
    
    var repeat = true;
    while (repeat) {
    var ans = parseInt(prompt("Enter a month number 1-12, or '0' to finish",""));
    if ((isNaN(ans)) || (ans == null) || (ans < 0) || (ans > 12)) {
    alert ("You must enter a month number 1-12 ");
    }
    else if (ans == 0) { // finish
    repeat = false;
    }
    
    if (ans > 0) {  // i.e. 1-12
    selmonth[count] = month[ans];  // add the month name corresponding to the month number to the array
    count ++;
    }
    
    } // end of while loop
    
    var str = "A total of " + count + " entries were made:- <br>";
    for (var i = 0; i <count; i++) {
    str += selmonth[i] + "<br>"
    }
    document.write (str);  // document.write() is obsolete and should not really be used
    
    </script>
    
    </body>
    </html>
    If you want the script to finish when the user enters "stop" rather than 0, I will leave you to do that yourself. Note that "stop" is a string value, not a number, so parseInt() will return NaN.

    It is not really in your best interests that others do your all or most homework for you. Your teacher may gain a false and exaggerated idea of your programming capabilities and so not offer you the support you need.

    Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.


    Where quality is the thing sought after, the thing of supreme quality is cheap, whatever the price one has to pay for it.
    --William James
    Last edited by Philip M; 05-11-2012 at 09:48 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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by iBall View Post

    I used a regular expression to check if the user entered a 2 digit number.
    The guy is a complete beginner and it is just plain silly to suggest using advanced concepts such as regular expressions - especially as there is absolutely no need for one. His assignment asks for him to use a prompt - not textboxes and/or buttons. As far as I know there is only one type of prompt in Javascript - window.prompt(). The idea is to assist the OP, not to add to your postcount or demonstrate how awfully clever and knowledgeable you are by posting a script which bears absolutely no relationship to what the OP requires for his assignment, or at this stage in his learning process.

    var intMonth = Number(strMonth);

    Wrong. Number will return a decimal if present. Should be parseInt(strMonth);
    "....check if the user entered a 2 digit number." Numbers smaller than 10 are not "2 digit numbers", or even 2-digit numbers.
    The textbox should have a size of 2 and maxlength of 2. That goes some way to preventing the user from entering decimal values.
    Last edited by Philip M; 05-11-2012 at 12:54 PM.

    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,869
    Thanks
    79
    Thanked 4,418 Times in 4,383 Posts
    It's also mildly nutso to use both a regular expression and range validation.

    One or the other.
    Code:
                function addMonth(){
                    var strMonth = document.getElementById('txtInp').value;
                    var reMonth = /^([1-9]|1[012])$/;
                    if ( ! reMonth.test(strMonth)
                    {
                        document.getElementById('txtInp').value = 'Invalid number';
                        return;
                    }
                    var intMonth = Number(strMonth);
                    ...
    or, as Philip points out, much simpler to do
    Code:
                function addMonth(){
                    var intMonth = parseInt(document.getElementById('txtInp').value,10);
                    if ( isNaN(intMonth) || intMonth < 1 || intMonth > 12 )
                    {
                        document.getElementById('txtInp').value = 'Invalid number';
                        return;
                    }
                    ...
    But of course Philip is also right that this is way way overkill for the rank beginner level the OP is.

    And before Felgall says it: And it's really unfortunate that poor shadoww1ng is apparently taking a class in the "History Of Javascript" instead of a class in "Javascript in the 21st Century".
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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