...

View Full Version : Variable for if...else statements?



Mooseman
03-03-2012, 07:48 PM
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! :D

TooCrooked
03-03-2012, 08:27 PM
....put the if/else into a function and call the function.

Mooseman
03-03-2012, 08:29 PM
....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)

TooCrooked
03-03-2012, 08:38 PM
pass those along as variables to your function


<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>

Mooseman
03-03-2012, 08:44 PM
This is still not working. The $ is for jQuery, so does it have to be somewhere else? Thanks!

TooCrooked
03-03-2012, 11:10 PM
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.

Mooseman
03-03-2012, 11:12 PM
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.

felgall
03-03-2012, 11:17 PM
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.

Mooseman
03-03-2012, 11:21 PM
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.

Old Pedant
03-04-2012, 07:08 AM
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.

TooCrooked
03-04-2012, 03:56 PM
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!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum