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 Object-Oriented JavaScript (2015) Practice Project User Interface Code

Dmitri Antonov
PLUS
Dmitri Antonov
Courses Plus Student 6,010 Points

confusion with function parameters....

Hello to everyone.

While trying to understand the code presented in the video, got confused with function parameters.

in question.js we have isCorrectAnswer function with parameter (choice) : Question.prototype.isCorrectAnswer = function (choice) { return this.answer === choice; };

then we're using above function in quiz.js in order to create guess function with parameter (answer): Quiz.prototype.guess = function (answer) { if (this.getCurrentQuestion().isCorrectAnswer(answer)) { this.score++; } this.questionIndex++; };

After that we're using guess function in guesshandler of QuizUI:

guessHandler: function(id, guess) { var button = document.getElementById(id); button.onclick = function() { quiz.guess(guess); Quiz.UI.displayNext(); } },

I understand the reason why functions are created and what is their purpose, but a bit confused with parameters. Can someone explain step by step how they cooperate with each other?

thx

3 Answers

Parameters allow you to pass data into a function. Parameters can either be used to store, modify or be used to add logic to your function. So in your question you have the following...

Question.prototype.isCorrectAnswer = function (choice) { 
// if question answer = users choice - return boolean value
  return this.answer === choice; 
};

the isCorrectAnswer method returns a boolean value based on whether the users choice is correct or not.

Quiz.prototype.guess = function(answer) {
 // if current question is answered correctly
  if (this.getCurrentQuestion().isCorrectAnswer(answer)) {
    this.score++; // add to users score
  }
  this.questionIndex++; // move to next question
};

the guess function takes a users answer as a parameter to check if the answer they provide is correct for the current question. If the answer is correct, then points are added to the users score. Then moves on to the next question.

guessHandler: function(id, guess) {
  var button = document.getElementById(id);
  button.onclick = function() {
    quiz.guess(guess);
    Quiz.UI.displayNext();
  }
}

guessHandler function takes the button id and the users guess and passes the guess when the button is clicked and then shows the next question.

I hope this helps but feel free to ask me any more questions you may have.

A function performs a task. A parameter is what is passed into the task for whatever reason.

So, we might have a function that is called throwSomething. Maybe the function takes a parameter called "thing"

consider the following:

throwSomething : function(thing){
  if (thing != null) {
    console.log("You just threw a " + thing);
  } else {
    console.log("There was nothing to throw! you look silly trying to throw the nothingness that was not passed in.");
  }
}

so... throwSomething is the reusable function. The parameter is what is passed in when the function is called. Maybe we will pass in "ball"

  throwSomething("ball");

and "You just threw a ball" would be printed to the console. Or maybe "chair"

  throwSomething("chair");

and "You just threw a ball" would be printed to the console.

If nothing is passed in a la

  throwSomething();

you would get the else statement printed out.

This is a very simple function, but the parameter that this particular function allows for... tells the person calling the function that it might (likely) need something passed in to do things with. We used an ambiguous name for the parameter "thing" since it could be any number of items that could be thrown. And perhaps there would be a better variable name for that, but it makes sense to me.

Once again, functions are the actions and parameters of a function are the variables that the function may need.

hope that helps

Not a problem.... good luck in your javascript endeavors.