Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialPeter Retvari
Full Stack JavaScript Techdegree Student 8,392 PointsSolved in a different way to put Newteachers object to the Teachers array
Hi Folks,
I solved in a different way to put Newteachers object to the Teachers array
function addNewTeachers() {
for (let i = 0; i< newTeachers.length; i+=1) {
teachers.push(newTeachers[i]);
}
}
However, this Hint a little confusing for me. It's pretty obvious that you have to define your variable before you use it (so I solved that part as well), but it also true for var keyword. If I declare the variable with var after I call the function I got the error message that undefined length doesn't exist or something.
'Hint: Unlike var variables, let and const variables cannot be accessed before they are declared. '
So all I'm saying is it possible with var keyword the declare it after the function?
Because mine is not working if you put the var after the functions (now it's ok, but just because it's located before the function):
3 Answers
Steven Parker
231,248 PointsThe difference in how declaration is handled is because of a process known as hoisting. This affects only the declaration. so any assignment of value still occurs when the statement is executed.
So what you're seeing in your code is because the assignment has not happened yet.
Steven Parker
231,248 PointsYour function needs to take "newTeachers" as an argument:
function addNewTeachers(newTeachers) { // <-- added "newTeachers"
When you say, "If I declare the variable with var after I call the function I got the error message..." I'm not sure what variable you are referring to. Your sample code doesn't show anything after the function.
And then when you say, "now it's ok, but just because it's located before the function" The sample code also doesn't show anything before the function.
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 PointsHi Steven, thanks for your quick help. You're so fast :) Yes, you're right about the function, if we have to use an argument it should look like this:
function addNewTeachers(newteachers) {
for (let i = 0; i< newteachers.length; i+=1) {
teachers.push(newteachers[i]);
}
}
function printTreehouseSummary() {
// TODO: fix this function so that it prints the correct number of courses and teachers
for (let i = 0; i < teachers.length; i++) {
console.log(`${teachers[i].name} teaches ${teachers[i].topicArea}`);
}
console.log(`Treehouse has ${i} JavaScript courses, and ${teachers.length} Javascript teachers`);
}
addNewTeachers(newTeachers);
printTreehouseSummary();
All I wanted to say Yesterday about the "hint", that I don't understand clearly what's the difference between the var, let, const keywords. She says: "'Hint: Unlike var variables, let and const variables cannot be accessed before they are declared. '" So it means that var variables can be accessed before they are declared? If yes why my code not working here: https://w.trhou.se/mrhgfwxvqu ?
Omar Faruque
Full Stack JavaScript Techdegree Graduate 21,802 PointsThe workspace has hoisting issues. Though JavaScript puts all the variables and functions into memory before the execution context, but the variables get stored as undefined. In order to make your code work, put the newTeachers array before the function call which passes the array as an argument.
Steven Parker
231,248 PointsThe functionality of "hoisting" is part of the language (not specific to the workspace). Variables declared with "var" are hoisted, but ones declared with "let" or "const" are not.
Omar Faruque
Full Stack JavaScript Techdegree Graduate 21,802 PointsActually I meant that the instructor have put the newTeachers variable which contains an array after the function which is causing issues related to hoisting. The console shows me an error every time, doesn't matter whether am I using let or var.
Steven Parker
231,248 PointsThis sounds like a somewhat different issue, and would be worth starting a new question for it. Be sure to include your whole code for analysis.
Peter Retvari
Full Stack JavaScript Techdegree Student 8,392 PointsPeter Retvari
Full Stack JavaScript Techdegree Student 8,392 PointsThank you, Steven ?