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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops `do ... while` Loops

highlandcow
highlandcow
7,352 Points

describing the code - looking for feedback

I want to ensure I'm getting the javascript lingo down properly.

I took a stab authoring a comment after each line of code for the number guessing game program in this video (up to the do-while loop). Would someone mind providing feedback on my comments? I want to make sure I use the right terms when reading/describing code. Thank you!

var randomNumber = getRandomNumber(10);
/* Create variable "randomNumber". Set the value of "randomNumber" by calling function "getRandomNumber()" while passing the parameter "10" to the function. 
(I struggled a bit describing how the function and its parameter are used to assign a value to the variable. 
I also might be mixing up parameter and argument?) */

var guess; // Create variable "guess".

var guessCount = 0; //Create variable "guessCount". Set the value of guessCount to 0.  

var correctGuess = false; //Create variable correctGuess. Set the value of correctGuess to false. 

function getRandomNumber ( upper ) {
  var num = Math.floor(Math.random() * upper) + 1; 
  return num;
} 
/* Declare function "getRandomNumber" with parameter "upper". 
Create var "num". Set value of "num" by calling the Math.random function and multiplying by the upper parameter. 
Call the Mat.floor function. 
Add 1. (I suspect I messed this one up entirely.) 
Return the num variable.  */

2 Answers

Hi highlandcow,

What a great question. The truth is, IMHO, commenting should be simple English that explains what is the overall goal/objective. Your method name and variables should be self documenting, meaning, their name should explain their specific purpose. Confused? No problem, let me answer using the example code you posted; below the code is my explanation.

/* Guessing game where the user has to guess the system generated number. Game ends when user guesses the correct number. Method returns the number of guesses made by the user. */ NOTE: This above comment would be placed on the method running all of this code

// Retrieve a random number between 0 and 10 var randomNumber = getRandomNumber(10);

/* Capture user guesses. This information is needed once the user completes the game. */ var guess; // Create variable "guess". var noOfUserGuessCnt = 0; var isUserGuessCorrect = false;

/* Return a random number, from 0 up to the value of highestNumber */ function getRandomNumber ( highestNumber ) { var num = Math.floor(Math.random() * highestNumber) + 1; return num; } NOTE: The getRandomNumber method shouldn't be here. It should be in it's own block of code, which serves a specific function (i.e. that is, it returns a number in the desired range).

When I read code comments, even my own, I want to understand what's "going on." I know that var {variableName} means I've created a new object. This fact is not important; what is important is WHY did I create that variable. For example, noOfUserGuessCnt; it's easier to understand that this is the number of guesses made by the user. The real question is why is that important? The comment above all the "set up variables" explains it; this information is needed when the game is completed (i.e. set up variables are guess, noOfUserGuessCnt, and isUserGuessCorrect).

There is no wrong answer, to include what you posted. The goal is that when you, or someone else, reviews the code, he/she understands what's going on. I will say that I would encourage responsible use of comments. Putting too much comments is almost as bad as having none at all. The code naming scheme is the most important lesson to take from this exercise. You'll find that your code is much easier to read, if your variable and method names are self explanatory, assisted by easy-to-understand comments. The exercise of good commenting skills will come easier with the more code you write.

HTH

John, "WHY did I create that variable. For example," that is a fantastic concept that I had not considered. I love simple and straight forward, and your comment IS that.

This is my way of commenting <blockquote>

var randomNumber = getRandomNumber(10);
/* created a variable called 'randomNumber' and assigned its value to the function call 'getRandomNumber' and passed in 10 as the parameter */

var guess; // Created the variable "guess".

var guessCount = 0; //Created a  variable called "guessCount" and assigned its value to 0.  

var correctGuess = false; //Created a variable called 'correctGuess' and set its value to false.

function getRandomNumber ( upper ) {
  var num = Math.floor(Math.random() * upper) + 1; 
  return num;
} 
/* Created a function called 'getRandomNumber' and it takes in a value as its parameter. Inside the function we created a variable called num and assigned its value to a random number between 1 and the number we specify when calling the function. After generating the random number we return it.  */

</blockquote> If i managed to help, mark my answer as the best