Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using an Array entry to refer to another Array

Status
Not open for further replies.

pob327

Technical User
Jul 19, 2007
13
0
0
US
G'day all, I have an Array composed of various numbers. I want to select one of those entries then add or subtract from that entry and use the result to refer to a second Array.
The problem I am having is that javascript keeps assuming I am dealing with string values. How can I get round this ?

specifically if starSquare is 45 (see code below) and startSquaresList[startSquare] is 77, I want startSquaresList[startSquare] + 1 to be 78, but it keeps coming out 771 !

Here is the code I have:

- startSquare is a number generated by Math function.
Code:
document.getElementById("coords").value = "startSquare =" + startSquare + " - " + startSquaresList[startSquare] + " - " + startSquaresList[startSquare] + 1;
 
Ok, as is so often the case, I was just being a bit thick about this.
For anyone who was as thick as me, you have to do the addition/subtraction then write it to "coords".

Code:
var surroundSquares = Number(startSquaresList[startSquare]) + 1;
document.getElementById("coords").value = "startSquare =" + startSquare + " - " + startSquaresList[startSquare] + " - " + surroundSquares;
 
You could do it inline, too, forcing the order of operation using parentheses and parseInt():
Code:
document.getElementById("coords").value = "startSquare =" + startSquare + " - " + startSquaresList[startSquare] + " - " + (parseInt(startSquaresList[startSquare], 10) + 1);

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top