Solved

If statement inside for loop

Posted on 2014-03-07
6
224 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

This article discusses four methods for overlaying images in a container on a web page
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

734 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