Home › Forums › Programming › Looping through VARS inside of the “exec script” puzzle
- This topic has 4 replies, 2 voices, and was last updated 2 years, 2 months ago by praeluceo.
-
AuthorPosts
-
2022-09-30 at 1:36 pm #56260praeluceoParticipant
Hi everybody,
as I am still quite a newbie to Javascript, sorry if the question is kinda dumb. I am using the “exec script” puzzle.I am trying to iterate through a large number of variables.
Trying to push the variables into an array doesn’t work (console says that the array remains empty). Accessing the variables individualy though does.
How would I go about it using a for loop?
Thanks a lot!let varsToSet = []; varsToSet.push(VARS.front_on_seen); varsToSet.push(VARS.front_round_seen); varsToSet.push(VARS.front_valve_seen); varsToSet.push(VARS.back_AC_seen); varsToSet.push(VARS.back_foot_seen); varsToSet.push(VARS.back_micro_seen); varsToSet.push(VARS.back_potential_seen); varsToSet.push(VARS.back_SCB_seen); varsToSet.push(VARS.back_sd_seen); // this doesn't work varsToSet.forEach(setTrue); function setTrue(item) { item = 1; } // this on the other hand does work VARS.front_on_seen = 1; VARS.front_round_seen = 1; VARS.front_valve_seen = 1; VARS.back_AC_seen = 1; VARS.back_foot_seen = 1; VARS.back_micro_seen = 1; VARS.back_potential_seen = 1; VARS.back_SCB_seen = 1; VARS.back_sd_seen = 1;
2022-09-30 at 3:33 pm #56266kdvParticipantyou should create a list of keys but not values
this code will create a list of valueslet varsToSet = []; varsToSet.push(VARS.front_on_seen); varsToSet.push(VARS.front_round_seen);
and this code will create a list of keys (or names of variables)
let varsToSet = []; varsToSet.push('front_on_seen'); varsToSet.push('front_round_seen'); varsToSet.forEach(setTrue); function setTrue(item) { VARS[item] = true; }
Puzzles and JS coding. Fast and expensive.
If you don’t see the meaning in something it primarily means that you just don’t see it but not the absence of the meaning at all.
2022-10-01 at 10:43 pm #56298praeluceoParticipantkdv77kd thx a lot, sincerely thank you for all the great help you’re providing around the forum!
I understand now my error, great explanation. I was basically storing the values of my variables inside the array, instead of actually storing the variables themself (which I guess are always stored as references in Javascript when put inside of an array). Still a long way to go learning Javascript, but it’s a lot of fun :)2022-10-01 at 11:03 pm #56299kdvParticipantIn this case it’s just an array of string values (no references) and
VARS
contains keys with names corresponding to those string values.Puzzles and JS coding. Fast and expensive.
If you don’t see the meaning in something it primarily means that you just don’t see it but not the absence of the meaning at all.
2022-10-02 at 8:53 am #56303praeluceoParticipantThanks a lot, got it :)!
-
AuthorPosts
- You must be logged in to reply to this topic.