bilbo.baggins

03-18-2009, 01:43 AM

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 -

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

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

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

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 -

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

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

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