Connecting Tech Pros Worldwide Forums | Help | Site Map

need some quick help with this little variable question

rebeccatre@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '07
hi, i am still figuring out joins and whatnot, could someone help me
achieve this goal?

<script>
var setnumber = 5;
var total5 = 100;

alert(total+setnumber);

</script>

Ok, the alert does not work, but my goal is to do what I hope is
obvious, and that is to join the setnumber value with the word
'total', so it becomes 'total5', and so it can be accessed/utilized as
such.

Can someone please help me with this? It would be greatly
appreciated. Have a very nice day.

Rebecca.


Peter Michaux
Guest
 
Posts: n/a
#2: Jul 23 '07

re: need some quick help with this little variable question


Hi,

On Jul 22, 5:08 pm, rebecca...@gmail.com wrote:
Quote:
hi, i am still figuring out joins and whatnot, could someone help me
achieve this goal?
>
<script>
var setnumber = 5;
var total5 = 100;
Is the above line relevant?
Quote:
alert(total+setnumber);
alert("total"+setnumber);
Quote:
</script>
>
Ok, the alert does not work, but my goal is to do what I hope is
obvious, and that is to join the setnumber value with the word
'total', so it becomes 'total5', and so it can be accessed/utilized as
such.
>
Can someone please help me with this? It would be greatly
appreciated. Have a very nice day.
You might find this helpful.

<URL: http://www.jibbering.com/faq/#FAQ3_1>

Peter

David Mark
Guest
 
Posts: n/a
#3: Jul 23 '07

re: need some quick help with this little variable question


On Jul 22, 8:08 pm, rebecca...@gmail.com wrote:
Quote:
hi, i am still figuring out joins and whatnot, could someone help me
achieve this goal?
>
<script>
You are missing the type attribute (text/javascript.)
Quote:
var setnumber = 5;
var total5 = 100;
>
alert(total+setnumber);
>
</script>
>
Ok, the alert does not work, but my goal is to do what I hope is
I don't see "total" defined.
Quote:
obvious, and that is to join the setnumber value with the word
'total', so it becomes 'total5', and so it can be
Like this?

alert('total' + setnumber);


accessed/utilized as
Quote:
such.
You lost me there. And why do you have a variable called "total5" set
to 100?

ASM
Guest
 
Posts: n/a
#4: Jul 23 '07

re: need some quick help with this little variable question


En réponse à rebeccatre@gmail.com qui nous a susurré, en date du :
23/07/07 2:08, le message sibyllin suivant :
Quote:
>
<script>
var setnumber = 5;
var total5 = 100;
>
alert(total+setnumber);
>
</script>
>
Ok, the alert does not work, but my goal is to do what I hope is
obvious, and that is to join the setnumber value with the word
'total', so it becomes 'total5', and so it can be accessed/utilized as
such.

alert('total = ' + window['total'+setnumber]);

there is also the function eval()

alert('total = ' + eval('total'+setnumber));



--
Stephane Moriaux et son (moins) vieux Mac
Lee
Guest
 
Posts: n/a
#5: Jul 23 '07

re: need some quick help with this little variable question


rebeccatre@gmail.com said:
Quote:
>
>hi, i am still figuring out joins and whatnot, could someone help me
>achieve this goal?
>
><script>
>var setnumber = 5;
>var total5 = 100;
>
>alert(total+setnumber);
>
></script>
>
>Ok, the alert does not work, but my goal is to do what I hope is
>obvious, and that is to join the setnumber value with the word
>'total', so it becomes 'total5', and so it can be accessed/utilized as
>such.
The other two answers tell us that your goal is not obvious.

You want to reference a variable whose name is partially
supplied in the value of another variable.

alert(window["total"+setnumber]);

will alert the value of total5, if setnumber=5.

Global variables are attributes of the window object (in a browser).
If your attribute name is the value of an expression, you can use
this "square bracket notation", to access it.
This makes the object look sort of like an associative array.


--

Randy Webb
Guest
 
Posts: n/a
#6: Jul 23 '07

re: need some quick help with this little variable question


rebeccatre@gmail.com said the following on 7/22/2007 8:08 PM:
Quote:
hi, i am still figuring out joins and whatnot, could someone help me
achieve this goal?
>
<script>
var setnumber = 5;
var total5 = 100;
>
alert(total+setnumber);
>
</script>
>
Ok, the alert does not work, but my goal is to do what I hope is
obvious, and that is to join the setnumber value with the word
'total', so it becomes 'total5', and so it can be accessed/utilized as
such.
>
Can someone please help me with this? It would be greatly
appreciated. Have a very nice day.
alert(window['total' + setnumber])

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#7: Aug 1 '07

re: need some quick help with this little variable question


Peter Michaux wrote:
Quote:
On Jul 22, 5:08 pm, rebecca...@gmail.com wrote:
Quote:
>hi, i am still figuring out joins and whatnot, could someone help me
>achieve this goal?
>>
><script>
>var setnumber = 5;
>var total5 = 100;
>
Is the above line relevant?
>
Quote:
>alert(total+setnumber);
>
alert("total"+setnumber);
That would display "total5". However, it would see the OP wanted to
display "100":

window.alert(this["total"] + setnumber);

I recommend to use an array instead of numbered variables, though.
Quote:
<URL: http://www.jibbering.com/faq/#FAQ3_1>
You must be kidding.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#8: Aug 1 '07

re: need some quick help with this little variable question


Peter Michaux wrote:
Quote:
On Jul 22, 5:08 pm, rebecca...@gmail.com wrote:
Quote:
>hi, i am still figuring out joins and whatnot, could someone help me
>achieve this goal?
>>
><script>
>var setnumber = 5;
>var total5 = 100;
>
Is the above line relevant?
>
Quote:
>alert(total+setnumber);
>
alert("total"+setnumber);
That would display "total5". However, it would see the OP wanted to
display "100":

window.alert(this["total" + setnumber]);

I recommend to use an array instead of numbered variables, though.
Quote:
<URL: http://www.jibbering.com/faq/#FAQ3_1>
You must be kidding.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Randy Webb
Guest
 
Posts: n/a
#9: Aug 1 '07

re: need some quick help with this little variable question


Thomas 'PointedEars' Lahn said the following on 8/1/2007 9:16 AM:
Quote:
Peter Michaux wrote:
Quote:
On Jul 22, 5:08 pm, rebecca...@gmail.com wrote:
Quote:
>hi, i am still figuring out joins and whatnot, could someone help me
>achieve this goal?
>>
><script>
>var setnumber = 5;
>var total5 = 100;
>
Is the above line relevant?
>
Quote:
>alert(total+setnumber);
>
alert("total"+setnumber);
>
That would display "total5". However, it would see the OP wanted to
display "100":
>
window.alert(this["total" + setnumber]);
>
I recommend to use an array instead of numbered variables, though.
Do you ever read a thread before replying? It has been mentioned several
times before now that an array would work better for what the OP wanted.
Quote:
Quote:
<URL: http://www.jibbering.com/faq/#FAQ3_1>
>
You must be kidding.
Why would referring the OP to buy a book (via hint) be kidding?

BTW, your posting agent, newsserver or your setup is broken for you to
be posting your reply twice.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Closed Thread


Similar JavaScript / Ajax / DHTML bytes