Hello again :)

I am having a problem where I want to change a particular variable based on what parameters I send to a js function.

In the code below, dnum is a number 1-6, and I want the global variable d1keep (or d2keep, d3keep etc.) to equal the dvalue.

function KeepDie(dnum, dvalue) {
	d+dnum+keep=dvalue;

         ... (more code)
}

I've tried using single quotes, double quotes, brackets, curly brackets, parentheses, and other punctuation in many locations all over that line of code, but I cannot get it to work.

Please help, thanks!

Recommended Answers

All 6 Replies

Hello again :)

I am having a problem where I want to change a particular variable based on what parameters I send to a js function.

In the code below, dnum is a number 1-6, and I want the global variable d1keep (or d2keep, d3keep etc.) to equal the dvalue.

function KeepDie(dnum, dvalue) {
	d+dnum+keep=dvalue;

         ... (more code)
}

I've tried using single quotes, double quotes, brackets, curly brackets, parentheses, and other punctuation in many locations all over that line of code, but I cannot get it to work.

Please help, thanks!

Pretty simple actually:

function KeepDie(dnum, dvalue) {
	window["d"+dnum+"keep"] = dvalue;

         ... (more code)
}

Thanks a lot, I'm finding the use of the window array is extraordinarily useful.

Thanks a lot, I'm finding the use of the window array is extraordinarily useful.

It's not an array, that's important to understand, it's a object.
In javascript obj.name is equivalent to obj["name"] .

Thanks for the lesson, I'm still trying to visualize the whole object thing. Knowing that window is an object helps. Learning how to code and understanding it all are sometimes different things; certainly they are for me with regards to javascript.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.