Solved

If statement inside for loop

Posted on 2014-03-07
6
229 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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 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…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

688 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