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 trial

JavaScript Introducing the Practice

Hamzah Iqbal
seal-mask
.a{fill-rule:evenodd;}techdegree
Hamzah Iqbal
Full Stack JavaScript Techdegree Student 11,145 Points

What is missing in the code?

So i think, it has all it needs. But I keep getting an error, on the addnewTeacher function. What is missing? 2nd question, why do I need add an argument in the function, 3rd question, what is the difference between newTeachers and teachers? Why is there two array objects?

const teachers = [
    {
        name: 'Ashley',
        topicArea: 'Javascript'
    }
];


const courses = ['Introducing JavaScript',
                'JavaScript Basics',
                'JavaScript Loops, Arrays and Objects',
                'Getting Started with ES2015',
                'JavaScript and the DOM',
                'DOM Scripting By Example'];


var i = courses.length;



function addNewTeachers(newTeachers) {
  for (let j = 0; j < teachers.lenght; j++) {
  teachers.push()
  }
    // TODO: write a function that adds new teachers to the teachers array 

}


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();



let newTeachers = [
    {
        name: 'James',
        topicArea: 'Javascript'
    },
    {
        name: 'Treasure',
        topicArea: 'Javascript'
    }
];

1 Answer

Isaiah Duncan
Isaiah Duncan
3,241 Points

I've made a lot of comments in the code that explains what I changed about your code and why. :)

const teachers = [
    {
        name: 'Ashley',
        topicArea: 'Javascript'
    }
];


const courses = ['Introducing JavaScript',
                'JavaScript Basics',
                'JavaScript Loops, Arrays and Objects',
                'Getting Started with ES2015',
                'JavaScript and the DOM',
                'DOM Scripting By Example'];


var i = courses.length;

/*

CHANGE 1: 
    Moved the 'newTeachers' object array higher in the code.
    You must declare 'let' variables BEFORE its implementation.

*/
let newTeachers = [
    {
        name: 'James',
        topicArea: 'Javascript'
    },
    {
        name: 'Treasure',
        topicArea: 'Javascript'
    }
];

/*

CHANGE 2: 
    Changed the name of the 'addNewTeachers' argument so that you know
    when the argument is being utilized vs an outside 
    variable. Keeps things less confusing.

CHANGE 3:
    Changed what you had 'teachers.lenght' to 'newTeachers.length'.
    What you had originally, had a spelling error in "length" as well as
    it was referencing the wrong array. Using 'teachers.length' would 
    always return '1' (because only one teacher is in the list at the start) 
    so it would only add one teacher.

    Secondly, 'newTeachers' is the object array you want to reference
    because it has the two new teachers you want to add to the main
    array of teachers. So we want to get the length of THIS array so
    we get the right amount of additions to the main list (which is 2).

CHANGE 4:
    Added 'addTeacher[j]' within the argument of the 'push' array method.
    Originally, you weren't pushing anything to new main teachers list as
    the push method was empty.

    What I've added within the push method will pull the object from the 
    'newTeachers' array according to the value that 'j' is set to and add 
    it to the main 'teachers' array. And this will only increment by 2 
    because we are getting the length of the 'newTeachers' array, which is 
    only 2 (because there are two objects in the array).

*/

// TODO: write a function that adds new teachers to the teachers array 
function addNewTeachers(addTeacher) {
  for (let j = 0; j < newTeachers.length; j++) {
    teachers.push(addTeacher[j])
  }
}

// TODO: fix this function so that it prints the correct number of courses and   teachers
function printTreehouseSummary() {
  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();