Solved

If statement inside for loop

Posted on 2014-03-07
6
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

740 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