简体   繁体   中英

Calling a function of a loop - what am I missing?

So I have a function inside of an object that chooses one of the 4 exercises at random.

    this.lift = function (number, group) {
        const random = Math.floor(Math.random() * Math.floor(number));
        const pick = group[random]
        return pick
    }

And then I have arrays of exercises corresponding to each group for it to pick from

 this.chest = ["Bench Press", "Incline Bench Press", "Weighted Dips", "Chest Fly"]
 this.back = ["Wide Grip Pulldown", "Close Grip Pulldown", "Barbell Row", "Seated/Supported Row"]

etc. etc. etc.

I can call the function individually to pick an exercise for each group, something like this:

console.log(work.lift(4, work.chest))
console.log(work.lift(4, work.back))
etc. etc.

But that's kinda ugly, and it would literally call this function sometimes 24+ times at once. so I want to just make it run on a nice clean loop. So I have this:

            const arnie = ["chest", "back", "delt", "bicep", "tricep", "quad", "ham", "core"]

            for (const muscle of arnie) {
                console.log(work.lift(4, whatgoeshere))
            }

And yet I cannot get it to work, have tried work.muscle, work.arnie[i] using var i in the loop, none of that runs. I must be missing something very small. So whatgoeshere?

The muscle var references the muscle group, you can use it to index into work

work.lift(4, work[muscle])

See property accessors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM