ellatronic / portfolio

Portfolio I created from Hackbright Academy's Front-End Development Course
http://projects.ellatronic.com/portfolio/
0 stars 0 forks source link

Week 7 Assignment #42

Open ellatronic opened 9 years ago

ellatronic commented 9 years ago

@DiyahM @Keenahn @LijieZhou

I did the homework assignment where the last time I did a GitHub commit is displayed on my portfolio page. I'm a little confused on when to use var for declaring variables or not: https://github.com/ellatronic/portfolio/blob/gh-pages/js/home.js If you could please take a look and see if I did it right or not because I remember we did an in-class exercise and the var was causing problems because of local vs global variables.

But the GitHub commit display seems to work on my page.

Thanks.

DiyahM commented 9 years ago

Hi @ellatronic,

You should always use var when declaring a variable. You are correct in that the in class problem was causing problems due to scope of local vs global. Local variables are defined within functions. If you use the same name for both a local and global variable, js will assume you are referencing the local variable. In the in-class example, we declared a global variable called test (for example, i don't recall the actually name) and did not assign it a value. Then in a function most people declared (used the var keyword) another variable called test and assigned it a value. This variable and its value is only available within this function, since we used 'var', javascript assumed we wanted our own local copy. Once the function is finished the copy of test and its value does not exist, but we still have our global variable 'test' which has no value.

So, the problem occurred when most people assigned a value to that local variable 'test'. Then they tried to access the value in another function, but could only access the global variable which did not have a value.

If you need to explain this class today, I can.

If you'll like to practice scope more, check out codeacademy's tutorial on scope. https://www.codecademy.com/courses/javascript-intro/4/2

-Diyah

DiyahM commented 9 years ago

Oh, one more comment - In your home.js file, you declare a variable called 'time' in your for loop. (line 12). This is an example of a local variable that is scoped to the for loop, and should only be available within the for loop. However, you reference 'time' again on line 28. This should throw a 'time' is not defined error, since time should no longer exist. If it is working, its most likely js being forgiving. Time should be scoped to the function 'getGithubData' and declared in between lines 2 and 3. You can still assign its value on line 12.