Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] correct dependencies and [refactor] rewrite app.js using react hooks and [chore] clean up #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

anton-karlovskiy
Copy link

No description provided.

let result = [];

for (let index = 0; index < totalSupply; index++) {
color = await contract.colors(index);
const color = await contract.colors(index);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you redeclaring the const in each loop run?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there was no declaration of color variable.
Now I've fixed like so:

let color;
for (let index = 0; index < totalSupply; index++) {
  color = await contract.colors(index);
  result.push(color);
};

Copy link

@KinectTheUnknown KinectTheUnknown Mar 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you redeclaring the const in each loop run?

It doesn't redeclare since const is block-scoped. Therefore, each iteration is completely isolated (in terms of block-scope). const inside the for loop ensures (and make it clear that) the variable is not being reassigned during any given iteration and keeps the variable only where it is needed/used.

You can try out the following snippet for proof of concept.

const array = ["foo", "bar", "baz"];
for (let i = 0; i < array.length; i++) {
  const item = array[i];
  console.log(item);
}

It should log each item in the array and not throw an error about item already being declared (because each iteration is isolated by block-scope).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks. I was not aware of that. I assumed it was not optimized that way. :)

KinectTheUnknown added a commit to KinectTheUnknown/nft that referenced this pull request Mar 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants