Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    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!

  • #2
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    ....put the if/else into a function and call the function.

  • #3
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts

    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)

  • #4
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.

  • #5
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    This is still not working. The $ is for jQuery, so does it have to be somewhere else? Thanks!

  • #6
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.

  • #7
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #9
    Regular Coder Mooseman's Avatar
    Join Date
    Sep 2010
    Posts
    123
    Thanks
    7
    Thanked 3 Times in 3 Posts
    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.

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    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.

  • #11
    New Coder
    Join Date
    Aug 2009
    Location
    Dirty Jersey
    Posts
    30
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •