#index

2 messages · Page 1 of 1 (latest)

worn mural
#

okay so i got a for loop a object variable and a function for it and trying to make alert say spells[index of the variable which is what function is in] + "spell"

for (var i = 0, spells = ["Empty", "Shield", "Charged Beam", "Movment", "Invisible", "Telekinesis", "Tracker"], spells_function = {}; i < 7; i++) {
    Object.assign(spells_function, {
        [spells[i]]: function () {
            alert(spells[i] + " spell");
        }
    });
}

earlier i thought i could solve this by my self, i was wrong lol

using i of course wont work because it would be 7 at the end of the for loop so basically each function i would need it to store the i which i was thinking maybe on each object i could add a var that would store it, but here's the thing i don't know how also unsure if this theory is correct but anyway uh i think that's all the info needed

note: i cannot use the parameters for this(though if i could it would of made it a lot easier)

worn mural
#

solved js var spells = [ "Empty", "Shield", "Charged Beam", "Movement", "Invisible", "Telekinesis", "Tracker" ]; var spells_function = { }; spells.forEach((key, index) => { spells_function[key] = function() {alert(spells[index] + " Spell")}; });