SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple JS - Need some help

    Does anyone see anything wrong with this? It doesnt seem to work...

    Code:
    function addition(a,b)
    {
    	var result = a + b;
    	return result;
    }
    
    var final = addition(3,4);
    document.write(final);
    ~("~) - Aloha!

  2. #2
    SitePoint Guru hgilbert's Avatar
    Join Date
    Dec 2004
    Location
    London
    Posts
    839
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very cryptic
    what are you trying to do?

    you can't have
    var x = function

    try

    Code:
    function addition(a,b)
    {
       return a+b
    }
    
    var final;
    final = addition(3,4);
    document.write(final);
    Last edited by hgilbert; Jun 2, 2005 at 02:55.


  3. #3
    SitePoint Guru hgilbert's Avatar
    Join Date
    Dec 2004
    Location
    London
    Posts
    839
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AhhhhHhhh !!
    OK

    the word final is reserved!
    try another variable.

    usually the simplest of things are the hardest to debug.
    please ignore all previous comments

    but try something else as the variable but not final.

    always use Hungarian notation
    so to keep to the same code as yours:

    Code:
    function addition(nX,nY)
    {
            var nResult = nX + nY;
            return nResult;
    }
    
    var nFinal = addition(3,4);
    document.write(nFinal);
    people say Hungarian notations are not necessary in Javascript
    but you can see how the prefix can be used to avoid reserved name conflict.
    the above code works (ive tested this time)


  4. #4
    SitePoint Zealot Ace02's Avatar
    Join Date
    Jun 2004
    Location
    SitePoint.com
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!
    ~("~) - Aloha!

  5. #5
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's right there are some reserved words, and final is one of them.

    But Hungarian notation is not the best way to name your variables


Bookmarks

Posting Permissions

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