-
Notifications
You must be signed in to change notification settings - Fork 10
problem set complete #2
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
base: master
Are you sure you want to change the base?
Conversation
ROgbonna1
left a comment
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.
This is a really strong submission. Some critical changes to make but they are all pretty much based on one misunderstanding of how var variables are scoped. Make these changes and push them back up this week.
| **1. How do variables declared with `let`, `const`, and `var` differ? Be sure to touch on _scope_, _reassignment_, and _hoisting_ for each.** | ||
|
|
||
| To begin with, the declaration of all three of these variable types are automatically | ||
| hoisted by javascript right before runtime, simply because they are variables. |
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.
yes!
| To begin with, the declaration of all three of these variable types are automatically | ||
| hoisted by javascript right before runtime, simply because they are variables. | ||
| As for their differences, variables declared with `const`, are designed to | ||
| be constant and therefore, JavaScript does not allow it's value to be reassigned. |
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.
well said!
| On the other hand, using `let` to declare a variable does allow you to reassign | ||
| its value later on. `let` and `const` are scoped variables, which means that its | ||
| existence can only be detected within its parent scope, whether that be global | ||
| or local. As for `var` its properties are the same as `let`, except for the fact |
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.
This is incorrect. Variables declared with var have a scope as well. Their scopes are defined by functions. We say they are function scoped.
fellow-submissions/devonte-duncan.md
Outdated
| scope as well as in the local scope of the `testScope` function. The value of `a` | ||
| that gets logged on lines 24 and 26 is "outer" because that's the onlt value of `a` | ||
| that it has access to. Meanwhile "undefined" is logged because the function `testScope` | ||
| is referenced, however there is no return value within it, thus "undefined" is logged. |
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.
What about the local variable a defined within the testScope function? Run this in the console and tell me what you get.
fellow-submissions/devonte-duncan.md
Outdated
| console.log(a); | ||
| ``` | ||
| This above code will log: | ||
| "undefined" |
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.
We don't get undefined because the function hello does not log anything.
|
|
||
| sayItLoud(); | ||
| ``` | ||
| The variable `name` was declared and initialized within the global scope, and |
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.
great answer!
|
|
||
| let greeting = "Hello" | ||
| ``` | ||
| This logs an error, because `let` is a scoped variable, JavaScript cannot access it until |
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.
I'll take it!
| console.log(`Status for Problem Set: ${problemSet}`); | ||
| ``` | ||
|
|
||
| This code logs "Status for Problem Set: undefined". This is because the variable |
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.
Answer is correct but explanation has a critical flaw. problemSet is not initialized in the body of that function. Remember: function parameters become locally scoped variables in the body of the function. Thus, when we assign the parameter a value in the function, it does not impact the binding of the argument passed to that function.
|
|
||
| console.log(advice); | ||
| ``` | ||
| The variable `advice` is declared locally within the if statement, therefore when |
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.
yep
| console.log(advice); | ||
| ``` | ||
|
|
||
| This code works despite the variable `advice` being declared locally because |
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.
Half correct. Make this change.
Variables declared with var are function scoped, not block scoped. Thus, advice can be accessed outside of the block where it is declared.
No description provided.