Web Analytics Made Easy -
StatCounter More on Arrays - CodingForum

Announcement

Collapse
No announcement yet.

More on Arrays

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • More on Arrays

    Thanks for the help on arrays yesterday. I finally had the script responding as it ought. Now I want to write out to screen, not only the choice the visitor selected and reply, but, under that, a bulleted list of all choices available, as in, (bullet)July - 4th of July

    But I'm wrong again! A list writes to screen, but it is not aligned correctly and all the way down the list is:
    You selected July.This month we celebrate 4th of July.
    Months - Dates
    months[i] dates[i],

    months[i] dates[i],

    months[i] dates[i],
    etc,etc,etc

    Would you please check to see where I am going wrong? Does it have to do with the "i"?

    Thank you!!

  • #2
    I suggest that in the future you post this in the preexisting thread so that any new people may know what you are talking about. I for example, don't have any idea of what code you're talking about or how to help you. Post some code and I would be more than happy to assist you

    Comment


    • #3
      I am SO sorry!! I forgot the CODE!! Here is the question and code:
      Thanks for the help on arrays yesterday. I finally had the script responding as it ought. Now I want to write out to screen, not only the choice the visitor selected and reply, but, under that, a bulleted list of all choices available, as in, (bullet)July - 4th of July

      But I'm wrong again! A list writes to screen, but it is not aligned correctly and all the way down the list is:
      You selected July.This month we celebrate 4th of July.
      Months - Dates
      months[i] dates[i],

      months[i] dates[i],

      months[i] dates[i],
      etc,etc,etc

      Would you please check to see where I am going wrong? Does it have to do with the "i"?

      Code:
      <html>
      <head>
      <title>months</title>
      <script language="JavaScript" type="text/javascript"> <!--

      var months = new Array(12)
      months[0] = "January"
      months[1] = "February"
      months[2] = "March"
      months[3] = "April"
      months[4] = "May"
      months[5] = "June"
      months[6] = "July"
      months[7] = "August"
      months[8] = "September"
      months[9] = "October"
      months[10] = "November"
      months[11] = "December"

      var dates = new Array(12)
      dates[0] = "New Year's Day"
      dates[1] = "President's Day"
      dates[2] = "St. Patrick's Day"
      dates[3] = "Bunny Day"
      dates[4] = "Memorial Day"
      dates[5] = "Flag Day"
      dates[6] = "4th of July"
      dates[7] = "Back to School"
      dates[8] = "Labor Day"
      dates[9] = "Halloween"
      dates[10] = "Thanksgiving"
      dates[11] = "Ho Ho Ho!"
      //-->
      </script>
      </head>
      <body>
      <script language="JavaScript" type="text/javascript"> <!--

      var response = parseInt( prompt("Enter a number from 1 to 12 that corresponds with the month: ", ""), 10 );

      if (response < 1 || response > 12)
      {
      alert("That is not a valid choice. Choose another.")
      }
      else
      {
      response=response-1;
      document.write("You selected " + months[response] + ".")

      document.write("This month we celebrate " + dates[response] + ".")
      }
      document.write("<ul>")
      document.write("<li>Months - Dates</li>")
      for (i=1; i<months.length; i++)
      {
      document.write("<li>months[i] dates[i], </li><br>")


      document.write("</ul>")
      }
      //-->
      </script>
      </body>
      </html>

      Thanks for your help!!

      Comment


      • #4
        Code:
        <html>
        <head>
        <title>months</title>
        <script language="JavaScript" type="text/javascript"> <!--
        
        var months = new Array(12)
        months[0] = "January"
        months[1] = "February"
        months[2] = "March"
        months[3] = "April"
        months[4] = "May"
        months[5] = "June"
        months[6] = "July"
        months[7] = "August"
        months[8] = "September"
        months[9] = "October"
        months[10] = "November"
        months[11] = "December"
        
        var dates = new Array(12)
        dates[0] = "New Year's Day"
        dates[1] = "President's Day"
        dates[2] = "St. Patrick's Day"
        dates[3] = "Bunny Day"
        dates[4] = "Memorial Day"
        dates[5] = "Flag Day"
        dates[6] = "4th of July"
        dates[7] = "Back to School"
        dates[8] = "Labor Day"
        dates[9] = "Halloween"
        dates[10] = "Thanksgiving"
        dates[11] = "Ho Ho Ho!"
        //-->
        </script>
        </head>
        <body>
        <script language="JavaScript" type="text/javascript"> <!--
        
        var response = parseInt( prompt("Enter a number from 1 to 12 that corresponds with the 
        
        month: ", ""), 10 );
        
        if (response < 1 || response > 12)
        {
        alert("That is not a valid choice. Choose another.")
        }
        else
        {
        response=response-1;
        document.write("You selected " + months[response] + ".")
        
        document.write("This month we celebrate " + dates[response] + ".")
        }
        document.write("<ul>")
        document.write("<li>Months - Dates</li>")
        for (i=1; i<months.length; i++)
        {
        [color=red]document.write("<li>" + months[i] + " - " + dates[i] + ", </li><br>")[/color]
        
        
        document.write("</ul>")
        }
        //-->
        </script>
        </body>
        </html>

        Comment


        • #5
          YES! but, one more problem......

          Yes! Thanks! That line solved the main problem and i'm glad to see I wasn't far off!!! But there is a problem with the way it writes. It won't copy here, but at the top is: Months - Dates, and just below in the proper spot is: January - New Year's Day. After that, the remainder of the list jumps to the left several spaces instead of staying in the list line. How do you keep the list aligned???

          Comment


          • #6
            What a stupid mistake on my part, I just realized that the </ul> was in the for loop. Try the following code, it should work.

            Code:
            <html>
            <head>
            <title>months</title>
            
            <script type="text/javascript">
            var months = new Array(12)
            months[0] = "January"
            months[1] = "February"
            months[2] = "March"
            months[3] = "April"
            months[4] = "May"
            months[5] = "June"
            months[6] = "July"
            months[7] = "August"
            months[8] = "September"
            months[9] = "October"
            months[10] = "November"
            months[11] = "December"
            
            var dates = new Array(12)
            dates[0] = "New Year\'s Day"
            dates[1] = "President\'s Day"
            dates[2] = "St. Patrick\'s Day"
            dates[3] = "Bunny Day"
            dates[4] = "Memorial Day"
            dates[5] = "Flag Day"
            dates[6] = "4th of July"
            dates[7] = "Back to School"
            dates[8] = "Labor Day"
            dates[9] = "Halloween"
            dates[10] = "Thanksgiving"
            dates[11] = "Ho Ho Ho!"
            </script>
            
            </head>
            <body>
            <script type="text/javascript">
            
            var response = parseInt( prompt("Enter a number from 1 to 12 that corresponds with the month:", ""), 10 );
            
            if (response < 1 || response > 12){
              alert("That is not a valid choice. Choose another.");
            }
            
            else{
              response--;
              document.write("You selected " + months[response] + ".");
              document.write("  This month we celebrate " + dates[response] + ".");
            }
            
            document.write("<ul>");
            document.write("<li><b>Months - Dates</b></li>")
            
            for (i = 0; i < months.length; i++){
              document.write("<li>" + months[i] + " - " + dates[i] + "</li>");
            }
            document.write("</ul>");
            </script>
            </body>
            </html>

            Comment


            • #7
              I'm Back!

              I had to be away the past couple of days and am just getting back to this. You are right! Just that one misplaced bracket made the difference! Thank you!

              Comment


              • #8
                It seems like you are trying to represent a table using a list. Why don't you use a real table for that tabular data of yours?
                Shawn

                Comment


                • #9
                  Please!! Don't confuse me with tables at this point!!

                  Comment


                  • #10
                    lol, ok, do as you like
                    Shawn

                    Comment


                    • #11
                      DOH!! Backflash on this ARRAY question!

                      Hi! I was looking at this script contained in this thread and found something confusing!

                      What is the 10 at the end of this line of code?

                      var response = parseInt( prompt("Enter a number from 1 to 12 that corresponds with the month: ", ""), 10 );


                      I've searched and can't figure out what it is.....

                      Thanks!

                      Comment


                      • #12
                        parseInt( prompt("Enter a number from 1 to 12 that corresponds with the month: ", ""), 10 );

                        the parseInt function uses two arguments, the number to "parse" and the radix in witch to give the result (in this case, 10)

                        so:

                        parseInt(3,2) would be 11 (3 in binary)
                        parseInt(255,16) would be ff (255 in hexadecimal)
                        parseInt(number,destination_radix)

                        I'm pretty sure about this, but not 100%...
                        Shawn

                        Comment

                        Working...
                        X