JavaScript

Kayc M
Kayc M
12,593 Points

Can I use a forEach loop instead?

I tried this but it won't work

     createTokens(num) {
        const tokens = new Array(num);
        tokens.forEach((token) => {
            new Token(this);
            tokens.push(token);
        });
        return tokens;   
    }

5 Answers

Steven Parker
Steven Parker
203,176 Points

Without knowing anything about what this is supposed to do, this stands out:

            new Token(this);     // <-- this creates an object, but doesn't save it anywhere
            tokens.push(token);  // <-- did you mean "tokens.push(new Token(this))" instead?

To allow a more thorough analysis and complete answer, please provide a link to the page you are working with and if possible, make a snapshot of your workspace and post the link to it here.

Kayc M
Kayc M
12,593 Points

Basically I downloaded the finished project changed the for loop in the Player.js file to a forEach and now it won't work, I did this because I had to test if it would work before I was too deep in the project. Here's the snapshot: https://w.trhou.se/hvj3wtncs5

Steven Parker
Steven Parker
203,176 Points

Now that I see the original code, here are the issues I notice with the intended replacement:

  • the "Token" constructor takes 2 arguments but is only being given one
  • "Array" is being used to pre-allocate the elements, but they are all being left undefined
  • a "forEach" callback is not invoked on uninitialized elements
  • "push" is being used to add additional elements onto the undefined ones
  • the "push" call is missing a closing parentheses after the argument
Steven Parker
Steven Parker
203,176 Points

Applying the hints would produce something like this:

     createTokens(num) {
        const tokens = new Array(num);
        tokens.fill(null);
        let i = 0;
        tokens.forEach(token => {
            token = new Token(i++, this);
        });
        return tokens;   
    }

While this would work, it's not nearly as concise as the original method.

Kayc M
Kayc M
12,593 Points

Still doesn't work because Array() doesn't accept native array methods such as push(), so this wouldn't work the way I wanted but thanks anyway.

Steven Parker
Steven Parker
203,176 Points

The point of one of my hints is that you shouldn't be using "push" in this case. The elements have been pre-allocated so they only need to have values assigned.

The forEach idea can work if the hints are implemented.

And while not needed here, the "Array" prototype does implement "push". You may have had some other issue. Example:

var test = new Array(2);
test.push("It works!");
console.log(test);  // [undefined, undefined, "It works!"]
Kayc M
Kayc M
12,593 Points

You're right my test on node was

var test = new Array(2);
test[0].push("It works!");
console.log(test);  // TypeError: a[0].push is not a function

As if I was accessing an array inside an array

Steven Parker
Steven Parker
203,176 Points

At that point, "test[0]" is still "undefined".

Kayc M
Kayc M
12,593 Points

I don't know what should I reference the num parameter to

    createTokens(num) {
        const tokens = Array();
        tokens.forEach(i => {
            let token = new Token(i, this);
           tokens.push(token);

        });
        return tokens;
    }
Steven Parker
Steven Parker
203,176 Points

The "num" is the quantity of tokens to create, you had the right idea the first time to use it in constructing the array.
See the example I added to my answer.

Kayc M
Kayc M
12,593 Points

Yeah, that's what I wanted but I forgot about the fill method and the original method is far easier, thanks again Steven Parker