We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Looping through VARS inside of the “exec script” puzzle

Home Forums Programming Looping through VARS inside of the “exec script” puzzle

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #56260
    praeluceo
    Participant

    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;
    
    #56266
    kdv
    Participant

    you should create a list of keys but not values
    this code will create a list of values

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

    #56298
    praeluceo
    Participant

    kdv77kd 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 :)

    #56299
    kdv
    Participant

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

    #56303
    praeluceo
    Participant

    Thanks a lot, got it :)!

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.