Solved

If statement inside for loop

Posted on 2014-03-07
6
219 Views
Last Modified: 2014-03-08
I have a for loop going through an array with an if...else statement inside of it, but only the else part is getting read. I want to split the array element into two pieces. if I put the split code in the else statement it will work, but I want to be able to perform the split before the if statement. How do I go about getting this done?
0
Comment
Question by:Barbara69
6 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39914201
Are we supposed to guess what your code is?
0
 

Author Comment

by:Barbara69
ID: 39914268
for (var i=0; i < formArray.length; ++i){
        var formArray1 = formArray[0].split("=");
         var formArray2 = formArray[1].split("=");        
                 
          if (formArray[2] >= 65)     {                           
                    document.writeln("Hi  "+formArray1[1]+". At age "+formArray2[1]+" you may retire.") }                    
                     else   {
                                 var formArray1 = formArray[0].split("=");
                            	    var formArray2 = formArray[1].split("=");
 		   document.writeln("Hi "+formArray1[1]+". At age "+formArray2[1]+" you are not ready to retire. Get to work." ) }}

Open in new window

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39914382
Without seeing what your formArray data is it's difficult to know, but if all your formArray[] elements are key/value pairs joined by '=' then you probably need to split formArray[2] before using it in the if statement.

Either that or your if statement should be checking against formArray1 or formArray2
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 9

Expert Comment

by:Sar1973
ID: 39914611
It is also possible that you never get the if condition satisfied, so every time the algorythm goes to the else statement.
0
 
LVL 38

Expert Comment

by:Tom Beck
ID: 39914657
Line 5 seems wrong. If formArray holds keys (names) and values (ages) separated by "=" then formArray1[1] or formArray2[1] could possibly hold a value greater than 65, but formArray[2] never could.

EDIT: Just realized that Chris is saying essentially the same thing.
0
 

Author Closing Comment

by:Barbara69
ID: 39914688
Thanks Chris, the if wasn't checking against the correct element of the array.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
align Linkedin sign to the right page 5 37
Google Maps API and PHP 25 36
div to fit another div 8 23
Html form and modal / img src -problem 3 12
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question