Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Introducing the Practice

Tomasz Grodzki
Tomasz Grodzki
7,705 Points

The function addNewTeachers() works without any parameters. So, why should I create any parameters in this function?

I deleted a parameter newTeachers from addNewTeachers() function and it works the same like with this parameter. So why should I bother about adding any parameter?

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


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'];


const i = courses.length;



function addNewTeachers() {

  for ( let i = 0; i < newTeachers.length; i++ )
    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();
printTreehouseSummary();

1 Answer

Steven Parker
Steven Parker
218,832 Points

The advantage of the parameter would be that when you had several arrays of teachers to add, you could call it once for each one, passing it as the argument.

It would also be useful in situations where the array to be added is not in the global scope, so the function cannot access it directly.

Tomasz Grodzki
Tomasz Grodzki
7,705 Points

Oh I see. So in this case I can leave the function without this parameter and it isn't mistake? Or maybe are there some rules which says that I should anyway put a parameter in parentheses?

Steven Parker
Steven Parker
218,832 Points

As you already discovered, the parameter isn't necessary to make this particular case work. But you should have one because that's what the assignment specifically asked for.