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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Please Help, function call problems

    I am relatively new to JavaScript, and I am having serious problems over a JavaScript function.

    I have 2 x 2 dimensional arrays of data to display on the screen, each array being 18 x 18. The arrays and displaying them is working fine, I have 2 lots of X and Y variable (LocX1, LocY1, LocX2 and LocY2) which I use to reference the cells of the arrays.

    I have 2 similar (but not identical) functions to move around these arrays using links on the screen.

    Function 1 moves around the first array and is fine -

    Code:
    function MoveMapView(strDirection) {
        if (strDirection == "North") {
            LocY2 -= 1;
            if (LocY2 < 0){
                LocY2 = 0;
            }
        }
        if (strDirection == "East") {
            LocX2 += 1;
            if (LocX2 > 17){
                LocX2 = 17;
            }
        }
        if (strDirection == "South") {
            LocY2 += 1;
            if (LocY2 > 17){
                LocY2 = 17;
            }
        }
        if (strDirection == "West") {
            LocX2 -= 1;
            if (LocX2 < 0){
                LocX2 = 0;
            }
        }
        //Call the display functions
    ..
    ..
    }
    However function 2 (below) adds one to the end of LocY2 as though it was a string when you click the link for 'South'.

    Code:
    function MoveSector(strDirection) {
          if (strDirection == "North") {
                LocY1 -= 1;
                if (LocY1 < 0){
                      LocY1 = 0;
                }
          }
          if (strDirection == "East") {
                LocX1 += 1;
                if (LocX1 > 17){
                      LocX1 = 17;
                }
          }
          if (strDirection == "South") {
                LocY1 += 1;
                if (LocY1 > 17){
                      LocY1 = 17;
                }
          }
          if (strDirection == "West") {
                LocX1 -= 1;
                if (LocX1 < 0){
                      LocX1 = 0;
                }
          }
          //Sava data to the cookie
          strText = "LocX1=" + LocX1;
          document.cookie = strText;
          strText = "LocY1=" + LocY1;
          document.cookie = strText;
          //Redirect
    location.replace('load.asp');
    
    }
    At the top of the page I have a routine to make sure that the numbers are numbers (as below).

    Code:
    // Make sure Location data is a number, may have been turned into a string
    //    LocX1= LocX1 + 0;
    //    LoxY1= LocY1 + 0;
    //    LocX2= LocX2 * 1;
    //    LoxY2= LocY2 * 1;
    
        LocX1= Number (LocX1);
        LoxY1= Number (LocY1);
        LocX2= Number (LocX2);
        LoxY2= Number (LocY2);
    As you can see, I have tried multiplying the variable by 1, adding zero and using the Number command. All other directions work fine and all links are identical, just passing a different 'direction' through an onClick.

    I know I am probably making a very simple and obvious mistake, but ANY help is appreciated. At this rate I will have no hair as well as no life!

    Thanks

    P.s. I miss placed this post before, and it was sugested I re-check the links that set the functions off. They are all identical (all 8), except for the 'north' 'south' etc. bit.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    So you ensure that LocY1 is a number at the top of the page, but are you 100% sure nothing is changing that later on the page???

    And easy answer would be to do:
    Code:
    if (strDirection == "South") {
                LocY1 = parseInt(LocY1) + 1;
                if (LocY1 > 17) LocY1 = 17;
          }
    But if you post a link to a working example of your page, we could maybe figure out where you are converting LocY1 to a string, somehow.

  • Users who have thanked Old Pedant for this post:

    bilbo.baggins (03-18-2009)

  • #3
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, computers are strange ...

    I pasted your code in place of mine, and the 2nd function worked fine, but the 1st function, that had worked ok, started having the same problems. I even went back to my original code to make sure I wasn't finally going over the edge, but no, my original post was right and the problem moved with the new code.

    I pasted your code (modified) into the first function, and now it all works ... lets hope it still does in the morning!

    Just to let you know, I had even coppied the code that ensures the variables are numbers (in all its forms!) into the functions to make sure it was the last thing run, but that made no difference!

    I am now changing all the maths in these functions to your code, hopefully that will be problem solved.

    Thankyou so so much for your help , though I still don't understand why the other 3 methods didn't work . The joys of computer programing!

    Bilbo


  •  

    Posting Permissions

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