need some quick help with this little variable question

R

rebeccatre

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

Peter Michaux

Hi,

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?
alert(total+setnumber);
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.

You might find this helpful.

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

Peter
 
D

David Mark

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

You lost me there. And why do you have a variable called "total5" set
to 100?
 
R

rebeccatre

I am very sorry posters, please let me refresh this:

The output should be 100 if all goes well,

let me expand this with more texture:

<script>
var total1 = 60;
var total2 = 70;
var total3 = 80;
var total4 = 90;
var total5 = 100;
</script>

So, now ok, let's look at 'total5'.

<script>
alert(total5);
</script>

The above generates 100. Good.

Now, how do I separate the word 'total' from the accumulating number,
and express them as separate variables?

So, in theory if it could work, I would write it like:

<script>
var whichtotal = 5;

alert( ???? )
</script>

So, somehow in the alert, I want to join the word 'total' with
whichever number I choose. The alert is just for testing, but as long
as this is not achieved via document.write then all is good.

BEST REGARDS, REBECCA
 
L

Lee

(e-mail address removed) said:
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.


--
 
A

ASM

En réponse à (e-mail address removed) qui nous a susurré, en date du :
23/07/07 2:08, le message sibyllin suivant :
<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));
 
T

Tim Ferguson

(e-mail address removed) wrote in @q75g2000hsh.googlegroups.com:
<script>
var total1 = 60;
var total2 = 70;
var total3 = 80;
var total4 = 90;
var total5 = 100;
</script>

So, now ok, let's look at 'total5'.

<script>
alert(total5);
</script>

The above generates 100. Good.

Now, how do I separate the word 'total' from the accumulating number,
and express them as separate variables?

var total=Array(NaN,60,70,80,90,100);

WScript.echo(total[5]);



Hope that helps


Tim F
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn [...]:
Peter said:
On Jul 22, 5:08 pm, (e-mail address removed) wrote:
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);

alert("total"+setnumber);

That would display "total5". However, it would see the OP wanted to
display "100":

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

This one was new and important, however. Identifying the Global Object
with `window' is an error-prone approach.
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.

I read the threads in the order that they were posted. If duplicates
bother you, just leave Usenet.
Why would referring the OP to buy a book (via hint) be kidding?

Because there is no good book about J(ava)Script/ECMAScript so far (for
which the reasons have been laid out brilliantly by Richard recently),
and most certainly no book in which using the interoperable built-in
`this' reference to the Global Object instead of `window' is promoted.
BTW, your posting agent, newsserver or your setup is broken for you to
be posting your reply twice.

Your newsserver or your newsreader appears to be unable to handle Cancel
control messages. If you have a closer look, you will see that my two
postings differ slightly. I am expecting your rant for that soon.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Randy said:
Thomas 'PointedEars' Lahn said the following on 8/1/2007 10:14 AM:
Randy said:
Thomas 'PointedEars' Lahn [...]:
window.alert(this["total" + setnumber]);
This one was new and important, however. Identifying the Global Object
with `window' is an error-prone approach.

Then why did you write window.alert if using window is error prone?

alert() is a documented method of Window objects since JavaScript 1.0.
`window' is a documented host-defined property of the Global Object to
refer to a Window object since JavaScript 1.0. I thought you got the
grasp of that by now.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top