JavaScript combining function parameter with excisting variable
I am creating a piece of code that repeats for multiple variables and I was wondering if it might be easier to make a function for it, and in the function 'dynamically' adjust the variable that I need to change.
Example: I am creating 31 doors that are either open or closed. So I have:
var door1State = 0;
var door2State = 0;
...
var door31State = 0;
My question is wether it's possible to create a function where I pass the door number as a parameter, like this:
function openDoor(doorNumber) {
door+doorNumber+State = 1;
}
So when I bind a listener to the door, I don't have to write anonymous functions everytime.
I hope my explanation is clear, if not I'm happy to clarify it a bit more.
Thanks in advance!
EDIT: Thanks for all the helpfull replies, I have a next question regarding this issue. I am working with 'Blippar', and they have an API which works fairly easy, but now I need to animate the doors to open. First I've declared the 'sprites' in the 'scene', and next I need to animate the specific parts
deur1.onTouchEnd = function() {
if (door1Openable == 1) {
deur1.animate().rotationY(180).scale(600,360).translation(0,0,95).duration(1000);
deur1text.animate().rotationY(0).scale(600,360).translation(0,0,100).duration(1000);
setTimeout(function(){
door1State = 1;
}, 1500);
} else {
return false;
}
This happens 31 times, for 31 doors. My question is, how would it be easiest to covert this into a function with parameters? The only required parameters are 'door number', 'x position' and 'y position'.
I tried something like this, which obviously didn't work, but I'm having a hard time finding the correct way to do it.
function() openDoor(doorNumber,xPos,yPos) {
if ('door'+doorNumber+'Openable' == 1) {
'deur'+doorNumber.animate().rotationY(180).scale(600,360).translation(xPos,yPos,95).duration(1000);
'deur'+doorNumber+'text'.animate().rotationY(0).scale(600,360).translation(xPos,yPos,100).duration(1000);
setTimeout(function(){
'door'+doorNumber+'Openable' = 1;
}, 1500);
} else {
return false;
}
Again, hopefully it's clear enough, I am basically trying to dynamically use variables within a function by using parameters.
EDIT 2: The API doesn't accept window for some reason, it runs in-app.
javascript variables parameters
add a comment |
I am creating a piece of code that repeats for multiple variables and I was wondering if it might be easier to make a function for it, and in the function 'dynamically' adjust the variable that I need to change.
Example: I am creating 31 doors that are either open or closed. So I have:
var door1State = 0;
var door2State = 0;
...
var door31State = 0;
My question is wether it's possible to create a function where I pass the door number as a parameter, like this:
function openDoor(doorNumber) {
door+doorNumber+State = 1;
}
So when I bind a listener to the door, I don't have to write anonymous functions everytime.
I hope my explanation is clear, if not I'm happy to clarify it a bit more.
Thanks in advance!
EDIT: Thanks for all the helpfull replies, I have a next question regarding this issue. I am working with 'Blippar', and they have an API which works fairly easy, but now I need to animate the doors to open. First I've declared the 'sprites' in the 'scene', and next I need to animate the specific parts
deur1.onTouchEnd = function() {
if (door1Openable == 1) {
deur1.animate().rotationY(180).scale(600,360).translation(0,0,95).duration(1000);
deur1text.animate().rotationY(0).scale(600,360).translation(0,0,100).duration(1000);
setTimeout(function(){
door1State = 1;
}, 1500);
} else {
return false;
}
This happens 31 times, for 31 doors. My question is, how would it be easiest to covert this into a function with parameters? The only required parameters are 'door number', 'x position' and 'y position'.
I tried something like this, which obviously didn't work, but I'm having a hard time finding the correct way to do it.
function() openDoor(doorNumber,xPos,yPos) {
if ('door'+doorNumber+'Openable' == 1) {
'deur'+doorNumber.animate().rotationY(180).scale(600,360).translation(xPos,yPos,95).duration(1000);
'deur'+doorNumber+'text'.animate().rotationY(0).scale(600,360).translation(xPos,yPos,100).duration(1000);
setTimeout(function(){
'door'+doorNumber+'Openable' = 1;
}, 1500);
} else {
return false;
}
Again, hopefully it's clear enough, I am basically trying to dynamically use variables within a function by using parameters.
EDIT 2: The API doesn't accept window for some reason, it runs in-app.
javascript variables parameters
add a comment |
I am creating a piece of code that repeats for multiple variables and I was wondering if it might be easier to make a function for it, and in the function 'dynamically' adjust the variable that I need to change.
Example: I am creating 31 doors that are either open or closed. So I have:
var door1State = 0;
var door2State = 0;
...
var door31State = 0;
My question is wether it's possible to create a function where I pass the door number as a parameter, like this:
function openDoor(doorNumber) {
door+doorNumber+State = 1;
}
So when I bind a listener to the door, I don't have to write anonymous functions everytime.
I hope my explanation is clear, if not I'm happy to clarify it a bit more.
Thanks in advance!
EDIT: Thanks for all the helpfull replies, I have a next question regarding this issue. I am working with 'Blippar', and they have an API which works fairly easy, but now I need to animate the doors to open. First I've declared the 'sprites' in the 'scene', and next I need to animate the specific parts
deur1.onTouchEnd = function() {
if (door1Openable == 1) {
deur1.animate().rotationY(180).scale(600,360).translation(0,0,95).duration(1000);
deur1text.animate().rotationY(0).scale(600,360).translation(0,0,100).duration(1000);
setTimeout(function(){
door1State = 1;
}, 1500);
} else {
return false;
}
This happens 31 times, for 31 doors. My question is, how would it be easiest to covert this into a function with parameters? The only required parameters are 'door number', 'x position' and 'y position'.
I tried something like this, which obviously didn't work, but I'm having a hard time finding the correct way to do it.
function() openDoor(doorNumber,xPos,yPos) {
if ('door'+doorNumber+'Openable' == 1) {
'deur'+doorNumber.animate().rotationY(180).scale(600,360).translation(xPos,yPos,95).duration(1000);
'deur'+doorNumber+'text'.animate().rotationY(0).scale(600,360).translation(xPos,yPos,100).duration(1000);
setTimeout(function(){
'door'+doorNumber+'Openable' = 1;
}, 1500);
} else {
return false;
}
Again, hopefully it's clear enough, I am basically trying to dynamically use variables within a function by using parameters.
EDIT 2: The API doesn't accept window for some reason, it runs in-app.
javascript variables parameters
I am creating a piece of code that repeats for multiple variables and I was wondering if it might be easier to make a function for it, and in the function 'dynamically' adjust the variable that I need to change.
Example: I am creating 31 doors that are either open or closed. So I have:
var door1State = 0;
var door2State = 0;
...
var door31State = 0;
My question is wether it's possible to create a function where I pass the door number as a parameter, like this:
function openDoor(doorNumber) {
door+doorNumber+State = 1;
}
So when I bind a listener to the door, I don't have to write anonymous functions everytime.
I hope my explanation is clear, if not I'm happy to clarify it a bit more.
Thanks in advance!
EDIT: Thanks for all the helpfull replies, I have a next question regarding this issue. I am working with 'Blippar', and they have an API which works fairly easy, but now I need to animate the doors to open. First I've declared the 'sprites' in the 'scene', and next I need to animate the specific parts
deur1.onTouchEnd = function() {
if (door1Openable == 1) {
deur1.animate().rotationY(180).scale(600,360).translation(0,0,95).duration(1000);
deur1text.animate().rotationY(0).scale(600,360).translation(0,0,100).duration(1000);
setTimeout(function(){
door1State = 1;
}, 1500);
} else {
return false;
}
This happens 31 times, for 31 doors. My question is, how would it be easiest to covert this into a function with parameters? The only required parameters are 'door number', 'x position' and 'y position'.
I tried something like this, which obviously didn't work, but I'm having a hard time finding the correct way to do it.
function() openDoor(doorNumber,xPos,yPos) {
if ('door'+doorNumber+'Openable' == 1) {
'deur'+doorNumber.animate().rotationY(180).scale(600,360).translation(xPos,yPos,95).duration(1000);
'deur'+doorNumber+'text'.animate().rotationY(0).scale(600,360).translation(xPos,yPos,100).duration(1000);
setTimeout(function(){
'door'+doorNumber+'Openable' = 1;
}, 1500);
} else {
return false;
}
Again, hopefully it's clear enough, I am basically trying to dynamically use variables within a function by using parameters.
EDIT 2: The API doesn't accept window for some reason, it runs in-app.
javascript variables parameters
javascript variables parameters
edited Nov 26 '18 at 17:10
Bas
asked Nov 26 '18 at 11:57
BasBas
112
112
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
Try using an array that holds your state
const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]this is much easier to handle and manipulate than a huge number of variables.
To open the door you can do something like this :
const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}and then OpenDoor(3);
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
add a comment |
It's possible, if you change the door#State variables to a single object instead of 31 separate variables. Then, you can just use bracket notation to look up the correct object.:
const doorStates = { door1State: 0, door2State: 0, ... };
function openDoor(doorNumber) {
doorStates['door' + doorNumber + 'state'] = 1;
}
Or, you might consider using an array instead:
// Create an array of length 31, all elements are 0:
const doorStates = Array.from({ length: 31 }, () => 0);
function openDoor(doorNumber) {
// arrays are zero-indexed
doorStates[doorNumber - 1] = 1;
}
add a comment |
You could try to make a dictionary of the doors.
It seems like your usage might merit the use of a dictionary instead of multiple variables.
Here's an example:
list_dict = {};
function(doorname)
{
this.list_dict[doorname] = 0;
}
You might also want to read a bit about maps, here: Javascript Maps
add a comment |
Yes, it's possible.
function openDoor(number) {
window['door'+number+'State'] = 1;
}
for (var i = 1; i <= 31; i++) {
window['door'+i+'State'] = 0;
}
openDoor(2)
console.log(door2State)
You can also use an array for that:
var doors = [
0,0,0,0,0,0,...0 // to 31 0s
];
Then reference door1State using doors[0]
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480620%2fjavascript-combining-function-parameter-with-excisting-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try using an array that holds your state
const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]this is much easier to handle and manipulate than a huge number of variables.
To open the door you can do something like this :
const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}and then OpenDoor(3);
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
add a comment |
Try using an array that holds your state
const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]this is much easier to handle and manipulate than a huge number of variables.
To open the door you can do something like this :
const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}and then OpenDoor(3);
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
add a comment |
Try using an array that holds your state
const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]this is much easier to handle and manipulate than a huge number of variables.
To open the door you can do something like this :
const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}and then OpenDoor(3);
Try using an array that holds your state
const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]this is much easier to handle and manipulate than a huge number of variables.
To open the door you can do something like this :
const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}and then OpenDoor(3);
const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]const Doors=[
{
id:1,
isOpen:true,
},
{
id:2,
isOpen:true,
},
{
id:3,
isOpen:false,
}
//..etc
]const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}const OpenDoor=(doorNumber)=>{
Doors.forEach(function(door, index) {
if(door.id===doorNumber)
Doors[index].isOpen = true;
});
}edited Nov 26 '18 at 12:05
answered Nov 26 '18 at 12:00
FawziFawzi
267110
267110
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
add a comment |
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
Thanks, would you also have an answer for my edit? It must be in ES5 tho, since the API doesn't accept ES6.
– Bas
Nov 26 '18 at 17:11
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
it's better to close this one and open a new question.
– Fawzi
Nov 27 '18 at 6:37
add a comment |
It's possible, if you change the door#State variables to a single object instead of 31 separate variables. Then, you can just use bracket notation to look up the correct object.:
const doorStates = { door1State: 0, door2State: 0, ... };
function openDoor(doorNumber) {
doorStates['door' + doorNumber + 'state'] = 1;
}
Or, you might consider using an array instead:
// Create an array of length 31, all elements are 0:
const doorStates = Array.from({ length: 31 }, () => 0);
function openDoor(doorNumber) {
// arrays are zero-indexed
doorStates[doorNumber - 1] = 1;
}
add a comment |
It's possible, if you change the door#State variables to a single object instead of 31 separate variables. Then, you can just use bracket notation to look up the correct object.:
const doorStates = { door1State: 0, door2State: 0, ... };
function openDoor(doorNumber) {
doorStates['door' + doorNumber + 'state'] = 1;
}
Or, you might consider using an array instead:
// Create an array of length 31, all elements are 0:
const doorStates = Array.from({ length: 31 }, () => 0);
function openDoor(doorNumber) {
// arrays are zero-indexed
doorStates[doorNumber - 1] = 1;
}
add a comment |
It's possible, if you change the door#State variables to a single object instead of 31 separate variables. Then, you can just use bracket notation to look up the correct object.:
const doorStates = { door1State: 0, door2State: 0, ... };
function openDoor(doorNumber) {
doorStates['door' + doorNumber + 'state'] = 1;
}
Or, you might consider using an array instead:
// Create an array of length 31, all elements are 0:
const doorStates = Array.from({ length: 31 }, () => 0);
function openDoor(doorNumber) {
// arrays are zero-indexed
doorStates[doorNumber - 1] = 1;
}
It's possible, if you change the door#State variables to a single object instead of 31 separate variables. Then, you can just use bracket notation to look up the correct object.:
const doorStates = { door1State: 0, door2State: 0, ... };
function openDoor(doorNumber) {
doorStates['door' + doorNumber + 'state'] = 1;
}
Or, you might consider using an array instead:
// Create an array of length 31, all elements are 0:
const doorStates = Array.from({ length: 31 }, () => 0);
function openDoor(doorNumber) {
// arrays are zero-indexed
doorStates[doorNumber - 1] = 1;
}
answered Nov 26 '18 at 12:00
CertainPerformanceCertainPerformance
85.9k154472
85.9k154472
add a comment |
add a comment |
You could try to make a dictionary of the doors.
It seems like your usage might merit the use of a dictionary instead of multiple variables.
Here's an example:
list_dict = {};
function(doorname)
{
this.list_dict[doorname] = 0;
}
You might also want to read a bit about maps, here: Javascript Maps
add a comment |
You could try to make a dictionary of the doors.
It seems like your usage might merit the use of a dictionary instead of multiple variables.
Here's an example:
list_dict = {};
function(doorname)
{
this.list_dict[doorname] = 0;
}
You might also want to read a bit about maps, here: Javascript Maps
add a comment |
You could try to make a dictionary of the doors.
It seems like your usage might merit the use of a dictionary instead of multiple variables.
Here's an example:
list_dict = {};
function(doorname)
{
this.list_dict[doorname] = 0;
}
You might also want to read a bit about maps, here: Javascript Maps
You could try to make a dictionary of the doors.
It seems like your usage might merit the use of a dictionary instead of multiple variables.
Here's an example:
list_dict = {};
function(doorname)
{
this.list_dict[doorname] = 0;
}
You might also want to read a bit about maps, here: Javascript Maps
answered Nov 26 '18 at 12:04
Oren_COren_C
158112
158112
add a comment |
add a comment |
Yes, it's possible.
function openDoor(number) {
window['door'+number+'State'] = 1;
}
for (var i = 1; i <= 31; i++) {
window['door'+i+'State'] = 0;
}
openDoor(2)
console.log(door2State)
You can also use an array for that:
var doors = [
0,0,0,0,0,0,...0 // to 31 0s
];
Then reference door1State using doors[0]
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
add a comment |
Yes, it's possible.
function openDoor(number) {
window['door'+number+'State'] = 1;
}
for (var i = 1; i <= 31; i++) {
window['door'+i+'State'] = 0;
}
openDoor(2)
console.log(door2State)
You can also use an array for that:
var doors = [
0,0,0,0,0,0,...0 // to 31 0s
];
Then reference door1State using doors[0]
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
add a comment |
Yes, it's possible.
function openDoor(number) {
window['door'+number+'State'] = 1;
}
for (var i = 1; i <= 31; i++) {
window['door'+i+'State'] = 0;
}
openDoor(2)
console.log(door2State)
You can also use an array for that:
var doors = [
0,0,0,0,0,0,...0 // to 31 0s
];
Then reference door1State using doors[0]
Yes, it's possible.
function openDoor(number) {
window['door'+number+'State'] = 1;
}
for (var i = 1; i <= 31; i++) {
window['door'+i+'State'] = 0;
}
openDoor(2)
console.log(door2State)
You can also use an array for that:
var doors = [
0,0,0,0,0,0,...0 // to 31 0s
];
Then reference door1State using doors[0]
answered Nov 26 '18 at 12:16
Onwuka GideonOnwuka Gideon
564
564
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
add a comment |
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
Unfortunately I can't use window in this API
– Bas
Nov 26 '18 at 17:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53480620%2fjavascript-combining-function-parameter-with-excisting-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown