Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2012, 06:48 PM   PM User | #1
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Question Variable for if...else statements?

Hello,
I have a long string of if..else statements, and the same is used in multiple functions. How do a make a variable that I can call instead of copying 40 lines of code?
Thanks!
__________________
Ask me anything about CSS! Learning PHP.
Mooseman is offline   Reply With Quote
Old 03-03-2012, 07:27 PM   PM User | #2
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
....put the if/else into a function and call the function.
TooCrooked is offline   Reply With Quote
Old 03-03-2012, 07:29 PM   PM User | #3
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Question

Quote:
Originally Posted by TooCrooked View Post
....put the if/else into a function and call the function.
I know this is a newbie question, but how would I do that with using two outside variables? ($ and num)
Mooseman is offline   Reply With Quote
Old 03-03-2012, 07:38 PM   PM User | #4
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
pass those along as variables to your function

Code:
<script>
function ifElse(x,y) // any arguments passed to this function are referenced as "x" and "y" in that order
{
alert(x+"\n"+y) // random usage of the arguments in javascript... x = $ and y = num in this case
}
ifElse("$", "num") // call ifElse function, sending it two arguments
</script>

Last edited by TooCrooked; 03-03-2012 at 07:40 PM..
TooCrooked is offline   Reply With Quote
Old 03-03-2012, 07:44 PM   PM User | #5
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
This is still not working. The $ is for jQuery, so does it have to be somewhere else? Thanks!
Mooseman is offline   Reply With Quote
Old 03-03-2012, 10:10 PM   PM User | #6
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
ah. i don't screw around with frameworks. they only cause trouble when you start with them instead of understanding the underlying javascript.

you may want to try your luck in the "JavaScript frameworks" section.
TooCrooked is offline   Reply With Quote
Old 03-03-2012, 10:12 PM   PM User | #7
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by TooCrooked View Post
ah. i don't screw around with frameworks. they only cause trouble when you start with them instead of understanding the underlying javascript.

you may want to try your luck in the "JavaScript frameworks" section.
Yeah, I kinda started with jQuery and am now learning real code. I only need jQuery is this function for animations.
Mooseman is offline   Reply With Quote
Old 03-03-2012, 10:17 PM   PM User | #8
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by Mooseman View Post
Yeah, I kinda started with jQuery and am now learning real code.
That's the wrong way around to do it. You can only use a framework properly once you understand enough JavaScript to be able to understand what the framework does.

You might try to get the code without the animation working without the jQuery calls first and then add them once you know all os the other code works.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 03-03-2012, 10:21 PM   PM User | #9
Mooseman
Regular Coder

 
Mooseman's Avatar
 
Join Date: Sep 2010
Posts: 118
Thanks: 7
Thanked 3 Times in 3 Posts
Mooseman is an unknown quantity at this point
Quote:
Originally Posted by felgall View Post
You might try to get the code without the animation working without the jQuery calls first and then add them once you know all os the other code works.
I have done that by substituting alerts for the jQuery animation code. I just don't know how to include $ or jQuery variables.
Mooseman is offline   Reply With Quote
Old 03-04-2012, 06:08 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, for starters, you don't put quotes around the $.

Or any other variable name.

In the example in post #4, he was *NOT* calling his function passing variables. He was just passing strings.

Maybe if you showed us your "long string of if..else statements" we could make more sense of your question.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-04-2012, 02:56 PM   PM User | #11
TooCrooked
New Coder

 
Join Date: Aug 2009
Location: Dirty Jersey
Posts: 30
Thanks: 0
Thanked 2 Times in 2 Posts
TooCrooked is an unknown quantity at this point
Quote:
Originally Posted by old pedant View Post
in the example in post #4, he was *not* calling his function passing variables. He was just passing strings.
WOW! IT S A GOOD THING I CALLED THEM "ARGUMENTS" THEN!!! WHEH! DODGED a weak semantical bullet THERE!!!

Last edited by TooCrooked; 03-04-2012 at 02:59 PM..
TooCrooked is offline   Reply With Quote
Reply

Bookmarks

Tags
if...else, javascript, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:55 AM.


Advertisement
Log in to turn off these ads.