1

I'm sending a string as parameter to a function, but i already have a global variable in that name, i want to get the value of that variable but its sending as undefined..

My example code

i have a array as reg[0][0],reg[0][1],reg[1][0],reg[1][0],reg[2][0],reg[2][1]

and i have some global variables as tick1, tick2, tick3...

it will either have the values as 0,1 or 2

and in a function i called

calc_score(id) //id will return as either tick1,tick2,tick3
{
    alert(eval("reg[id][1]")); // it should return the value of reg[0][1] if id is 0

}

But its not working.

The id wont be a numeral it will be string .. So how can i do this?

Vijay
  • 5,331
  • 10
  • 54
  • 88

3 Answers3

6

You shouldn't use eval for things like this. If you need to convert id to a number, use the unary + operator:

calc_score(id) //id will return as either tick1,tick2,tick3 
{ 
    alert(reg[+id][1]); // it should return the value of reg[0][1] if id is 0 
} 

or parseInt()

calc_score(id) //id will return as either tick1,tick2,tick3 
{ 
    alert(reg[parseInt(id, 10)][1]); // it should return the value of reg[0][1] if id is 0 
} 


If you need to parse a string like "tick1, tick2" then you have a few options. If the first part will always be "tick", you can slice the end off the string like so:
calc_score(id)
{
    id = +id.slice(4);         // or +id.substring(4) if you prefer
    alert(reg[id][1]); 
}

If tick1, tick2, tick3 are global variables, then instead of using eval(), you should reference them via the window object like so:

calc_score(id)   //id will return as either "tick1","tick2","tick3"
{
    alert(window[id]);
} 
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • +1 for unary + for conversion, which I've never heard of before. – OregonGhost Feb 22 '10 at 10:20
  • @OregonGhost: It's essentially the same as using `Number()` to cast, but shorter and useful in so many situations: http://stackoverflow.com/questions/61088/hidden-features-of-javascript/2243631#2243631 – Andy E Feb 22 '10 at 10:27
  • i forgot to emphasize "The id wont be a numeral it will be string .. " The id will be a string but it contains a number value, so how can i do that? – Vijay Feb 22 '10 at 10:42
  • The id will be like 'str1' , 'str2' etc.. The var str1 will always have nos.. I've denoted this under the code but not clearly and bold enough to catch the notice. Sorry .. – Vijay Feb 22 '10 at 11:08
  • @Vijay, sorry I misunderstood that part. See my update at the bottom. – Andy E Feb 22 '10 at 11:37
  • @Vijay: Thanks. I've added another update because I finally understand what you were asking for :-) `eval()` is not the right tool for the job here, but you should be able to access global variables via the window object: eg. `window[tick1]`. – Andy E Mar 06 '10 at 10:01
  • All object properties in Javascript are strings; lookups with numbers are implicitly converted to strings. `reg[1]` is the same as `reg['1']`. – Miles Mar 06 '10 at 10:39
0

Use this:

alert(reg[Number(id)][1]);

of course you should check that id can be cast to a number before you do it. I don't really think you need the eval, unless you are trying to do something else that you haven't mentioned.

slugster
  • 49,403
  • 14
  • 95
  • 145
-1

Oh! You change the code like the following:

calc_score(id) //id will return as either tick1,tick2,tick3
{
    alert(eval("reg[" + id + "][1]")); // it should return the value of reg[0][1] if id is 0

}
Kangkan
  • 15,267
  • 10
  • 70
  • 113
  • Thanks. Its really interesting to know that a downvoted answer solves a problem. Really amazing. – Kangkan Mar 02 '10 at 08:28
  • Generally people see downvotes in different ways, sometimes as a method to indicate an incorrect answer, sometimes to mark down the answer to improve visibility of other answers. In this case it's probably a disagreement that this is the best solution to the problem (because of the use of `eval()`). Nevertheless, let me even it out a bit for you. – Andy E Mar 06 '10 at 11:13