Web Analytics Made Easy -
StatCounter Arrays/variables - CodingForum

Announcement

Collapse
No announcement yet.

Arrays/variables

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

  • Arrays/variables

    OK. It seems I have a big problem identifying how to use variables!! In this script I have two arrays of the same length. The index number on one corresponds to the other as it has to do with holidays, etc., in that month. The object of the script is to prompt the visitor to put in a number for a month then a couple of document.write statements will send back:
    "You chose July."
    "In this month, we celebrate 4th of July."
    I don't know how to make the response to the prompt also call the index to the second array and write that second line!
    Will you assist me please? Thanks!
    Here's the script:
    <html>
    <head>
    <title>months</title>
    <script language="JavaScript" type="text/javascript"> <!--
    var months = new Array(11)
    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(11)
    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 = prompt("Enter a number from 1 to 12 that corresponds with the month: ", "")

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

    var response = i
    for (i=0; i<dates.length; i++) {
    document.write("This month we celebrate ", + dates[response] + ".") }
    //-->
    </script>
    </body>
    </html>

  • #2
    Hi,
    you need to replace this code


    var response = i
    for (i=0; i<dates.length; i++) {
    document.write("This month we celebrate ", + dates[response] + ".") }


    with
    document.write("This month we celebrate ", + dates[response] + ".")

    ez
    Animation Rule #64
    Poor quality images are often artistic,
    Poor quality sound is ALWAYS annoying.

    Comment


    • #3
      Thanks for the reply but there have been other developments. I had just made that change right before your answer posted. But when I use it, I get a result that writes, You chose July. This month we celebrate NaN Then, to further complicate issues, I realized that the way I have it set up, January and December will NEVER be called! The array begins at 0 and goes thru 11 but the months need to be 1 thru 12! It must be very frustrating dealing with beginners like me. I hope you or someone else will help me more. Here's the code with the change:

      html>
      <head>
      <title>months</title>
      <script language="JavaScript" type="text/javascript"> <!--
      var months = new Array(11)
      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(11)
      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 = prompt("Enter a number from 1 to 12 that corresponds with the month: ", "")

      if (response < 1 || response >= 12){
      alert("That is not a valid choice. Choose another.") }
      else {
      document.write("You selected " + months[response] + ".")document.write("This month we celebrate ", + dates[response] + ".") }
      //-->
      </script>
      </body>
      </html>

      Comment


      • #4
        var response = parseInt( prompt("Enter a number from 1 to 12 that corresponds with the month: ", ""), 10 );
        My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
        “Minds are like parachutes. They don't work unless they are open”
        “Maturity is simply knowing when to not be immature”

        Comment


        • #5
          Nope. No Go......

          I updated to the suggested variable plus changed the index numbers to 1 thru 12 so that the numbers would correspond correctly with the months. But, even after the addition of parseInt to the response variable, the dates still return NaN.
          Sigh......
          <html>
          <head>
          <title>months</title>
          <script language="JavaScript" type="text/javascript"> <!--
          var months = new Array(11)
          months[1] = "January"
          months[2] = "February"
          months[3] = "March"
          months[4] = "April"
          months[5] = "May"
          months[6] = "June"
          months[7] = "July"
          months[8] = "August"
          months[9] = "September"
          months[10] = "October"
          months[11] = "November"
          months[12] = "December"

          var dates = new Array(11)
          dates[1] = "New Year's Day"
          dates[2] = "President's Day"
          dates[3] = "St. Patrick's Day"
          dates[4] = "Bunny Day"
          dates[5] = "Memorial Day"
          dates[6] = "Flag Day"
          dates[7] = "4th of July"
          dates[8] = "Back to School"
          dates[9] = "Labor Day"
          dates[10] = "Halloween"
          dates[11] = "Thanksgiving"
          dates[12] = "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
          {
          document.write("You selected " + months[response] + ".")

          document.write("This month we celebrate ", + dates[response] + ".")
          }
          //-->
          </script>
          </body>
          </html>

          Comment


          • #6
            Code:
            document.write("This month we celebrate ", + dates[response] + ".")
            // What's he doing here? ----------------^
            Just remove that comma and you're golden.

            BTW, here's a cooler way to do your error-checking
            Code:
            var response = getResponse();
            document.write("You selected " + months[response] + ".")
            document.write("This month we celebrate " + dates[response] + ".")
            
            function getResponse( error, oldResponse )
            {
            	if ( error )
            	{
            		response = parseInt( prompt( oldResponse + " is not a valid choice. Please choose again: ", ""), 10 );
            	} else {
            		response = parseInt( prompt( "Enter a number from 1 to 12 that corresponds with the month: ", ""), 10 );
            	}
            	if ( response < 1 || response > 12 )
            	{
            		return getResponse( true, response );
            	}
            	return response;
            }
            My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
            “Minds are like parachutes. They don't work unless they are open”
            “Maturity is simply knowing when to not be immature”

            Comment


            • #7
              Are We Happy Now :0) YES!

              Bless you! What a great forum!!!

              Comment


              • #8
                There is a problem though.
                If a user puts 5 in the prompt, it will write:
                document.write("You selected " + months[5] + ".")
                document.write("This month we celebrate " + dates[5] + ".")

                But months[5] is June. That's because you start your array with 0, which is january. In reality, people will think january is 1, not 0.
                So by entering 5, the user is thinking about May.
                It should be:

                response=response-1;
                document.write("You selected " + months[response] + ".")
                document.write("This month we celebrate " + dates[response] + ".")
                So, if the user types in 5, the javascript will change it to 4 and show months[4], which is in fact the fifth month (may)

                Does that make any sence?
                Shawn

                Comment


                • #9
                  Perfectly! Thanks!

                  Comment

                  Working...
                  X