Link to home
Start Free TrialLog in
Avatar of Starr Duskk
Starr DuskkFlag for United States of America

asked on

Javascript AND syntax

I need to know how to turn this javascript two liner into a oneliner.

I don't want the if/else. Instead I want to combine the statements in the code below. I don't know the syntax.

if majorversion <10 AND minorversion < 1 then
do this


                    if ((version['major'] < 10)) {
                        pageLoc = PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }
                    else if ((version['minor'] < 1)) {
                        pageLoc = PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }

Open in new window


thanks!
ASKER CERTIFIED SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can do this with () ? :; the () is the if statement, the ? designates the THEN and the : designates the ELSE.  to combine the ELSE if you nest the next () ? : in the ELSE of the first :) confusing?? :P

Can't test this exactly but I know it works with simple substitution:

pageLoc = (version['major'] < 10) ? PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure) : ((version['minor'] < 1)?PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure) : 0  );

Open in new window

Here is a demo using the same structure of a comparison and then assigning to a final variable:  http://jsbin.com/IfuFaWE/1/edit

var version = [];
version.major = Math.random()*20;
version.minor = Math.random()*2;

pageLoc = (version.major < 10) ? "Major less than 10: " + version.major : ((version.minor < 1) ? "Minor less that 1: " + version.minor : "Didn't match either - major: " + version.major + " minor: " + version.minor  );

console.log(pageLoc);

Open in new window

Why do you need the if / else when the ultimate result is the same?

pageLoc = PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);

Open in new window

Avatar of Starr Duskk

ASKER

I think this is what I actually need:
if (version['major'] == 10 && version['minor'] < 1) 

Open in new window


is that == right?
>>Why do you need the if / else when the ultimate result is the same?

I didn't provide my final else statement. You only need to see what I need assistance with.
btw, I have an if/else if/else so I need to keep the structure Gary suggested.
Here's what I'm going for. To see if they are using a version less than 10.1:

                  if ((version['major'] < 10)) {
                        pageLoc = PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }
                    else if (version['major'] == 10 && version['minor'] < 1) {
                        pageLoc = PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }
                    else {
                        pageLoc = PageMethods.GetSuccessPath(location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }

Open in new window

Did you see my comment above? That is an if else if else
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If this is just about getting anything lower than version 10.1 then yes you're on the right track... sorry my thinking was you wanted to condense the code.

The logic you've got is right:  though why wouldn't you just go with this:

if (version['major'] == 10 && version['minor'] < 1) {
                        pageLoc = PageMethods.GetFailurePath(ver,location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }
                    else {
                        pageLoc = PageMethods.GetSuccessPath(location.href, location.pathname, OnGetMessageSuccess, OnGetMessageFailure);
                    }

Open in new window


That tests for less than 10.1 and everything else
thanks!
Thanks @BobCSD - Can you post the code you ended up using, cheers,