Solved

If statement inside for loop

Posted on 2014-03-07
6
213 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 42

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now