How to do reverse value alert using for statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tangara
    New Member
    • Jan 2010
    • 27

    How to do reverse value alert using for statement

    Hi,

    Could someone advise me how to do the reverse values in this code? 300, 200, 100. Think I'm really hopeless when come to programming :-(

    for (intIndex = 0; intIndex > ayrElements.len gth ;intIndex--)// this is the part that I'm stuck.

    Here's the code:

    Code:
    <html>
    <head>
    <style>
    #divMessage { width : 500px;
    	height : 30px;
    	border : 1px solid black;
    	}
    
    body { font : arial;
    	font-size : 10px;}
    
    </style>
    <script language="javascript">
    
    
      function inspectCheckBoxes(){    
    
       var aryElements = new Array(); 
       var intValue = new Number(0); 
       var intIndex  = new Number(0); 
    
         aryElements = document.getElementsByName("chkData");
     
       for (intIndex= 0 ;intIndex <aryElements.length;intIndex++){
    	if (aryElements[intIndex].checked)
          		 alert(aryElements[intIndex].value);
       }
    }
    </script>
    </head>
    <body>
    <table border="1">
      <tr>
       <td colspan="2">
        <b>Experiment 1</b><br>
        Experiment using JavaScript to communicate and 
        control the checkbox controls.</td>
      </tr>
      <tr>
        <td style="width:10%"><input type="checkbox" name="chkData" value="100"/></td>
        <td>100</td>
      </tr>
    <tr>
        <td><input type="checkbox" name="chkData" value="200"/></td>
        <td>200</td>
      </tr>
      <tr>
        <td><input type="checkbox" name="chkData" value="300"/></td>
        <td>300</td>
      </tr>
      <tr>
         <td colspan="2">
         <div id="divMessage" ></div>
         </td>
      </tr> 
      <tr>
        <td colspan="2" align="right">
         <input type="button" id="btnExperiment" value="Experiment" onclick="inspectCheckBoxes()" >
        </td>
      </tr>
    </table>
    </body>
    </html>
    Last edited by Dormilich; Jan 19 '10, 02:23 PM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    for (intIndex = 0; intIndex > ayrElements.len gth ;intIndex--)
    let’s apply some common logic:

    if you count down, do it from the largest value to the smallest value.

    counting down from 0 will get you in negative numbers …
    quitting the loop when the counter variable is above the maximum value … is a condition you never reach with decrementing

    try it, it should be easier now.

    Comment

    • tangara
      New Member
      • Jan 2010
      • 27

      #3
      Hi,

      It doesn't work. I run it and nothing happens.

      By right, it should give me alert message of 300, 200, 100.

      :-(

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well even when you do understand how for loops work (http://www.javascriptkit.com/javatutors/loop3.shtml), is the "collection of objects" returned by getElementsByNa me() garunteed to be in the same order all the time?
        Last edited by Plater; Jan 19 '10, 03:03 PM. Reason: corrected for dorm's nitpicking

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          let me guess … you didn’t select any checkbox?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Well even when you do understand how for loops work, is the array returned by getElementsByNa me() garunteed to be in the same order all the time?
            a) it’s not an Array
            b) NodeLists are in document order

            EDIT: some thougths on the code
            Code:
            function inspectCheckBoxes(){    
               // unnecessary, aryElements’ type will be set later
               var aryElements = new Array(); 
               // not used, why define?
               var intValue = new Number(0); 
               // var intIndex = 0 would have been sufficient
               var intIndex  = new Number(0); 
             
               // declare the variable here: [I]var elements = document…[/I]
                 aryElements = document.getElementsByName("chkData");
             
               // avoid calculations in the abort condition part, i.e. calculate the length only once:
               // for (var i=0, l<elements.length; i<l, i++)
               for (intIndex= 0 ;intIndex <aryElements.length;intIndex++){
                // it’s good coding practice to wrap every code block of [I]if[/I] in { } even if it is only one line
                if (aryElements[intIndex].checked)
                           alert(aryElements[intIndex].value);
               }
            }
            Last edited by Dormilich; Jan 19 '10, 03:10 PM. Reason: commenting the code

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              after dorm's nitpicking:
              is the "collection of objects" returned by getElementsByNa me() garunteed to be in the same order all the time?
              yes, it is.

              Comment

              • tangara
                New Member
                • Jan 2010
                • 27

                #8
                I have found the solution already.

                Just use :-
                Code:
                for(intIndex=aryElements.length-1; intIndex>=0; intIndex--){
                alert(aryElements[intIndex].value)
                }
                Last edited by gits; Jan 20 '10, 04:41 AM. Reason: added code tags

                Comment

                Working...