-
Notifications
You must be signed in to change notification settings - Fork 442
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
base: master
Are you sure you want to change the base?
Conversation
test/Color.test.js
Outdated
let result = []; | ||
|
||
for (let index = 0; index < totalSupply; index++) { | ||
color = await contract.colors(index); | ||
const color = await contract.colors(index); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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);
};
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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. :)
No description provided.