So last time we left off with a great explanation of what everything in our Project constructor class does and how to use it. However, we also left off with not all the methods being fully operational. Now that its been a good amount time for what we’ve covered so far to sink in, we’ll now go onto what we did to fix these problems. Let’s get coding!

*Side Note: I’m not going to refer back to the code in the previous post too much. I’m just going to highlight and explain the changes that were made that stand out the most. 

 

The Code

 

Below you’ll find the fully functioning constructor class ready to be used in your console. Take a second to play with it for yourself, come back to the next section when you finish.

That’s where we begin explaining why it works now!

 

function Project(projName,endingAmount,totalHours,days,rate){
	this.projName = projName;
	this.endingAmount = endingAmount;
	this.totalHours = totalHours;
	this.days = days;
	this.rate = rate;
	var hoursToday = 0,
		hoursLeft,
		amountLeft;
	this.hoursUsed = function(hours){
		hoursToday += hours;
		hoursLeft = totalHours - hoursToday;
		this.totalHours = hoursLeft;
		console.log("You've worked " + hours + "today");
		console.log("You've worked " + hoursToday + " total.");
		console.log("You have " + hoursLeft + " remaining.");
	}
	this.hoursRemain = function(hours){
		console.log(this.totalHours);
	}
	this.amountLeft = function(){
		amountLeft = this.endingAmount - (hoursToday * this.rate);
		if(amountLeft > 0){
			console.log(amountLeft);
		}
		else{
			console.log("You workd overtime for the amount of " + Math.abs(amountLeft));
		}
	}
}

 

Variable Scope

 

In the version of this code in the last part, our variables were declared inside the function and object parameters were the only ones declared globally throughout the constructor. When I use the term global it is in regards to where the variables are being declared in our constructor class.

A variable declared outside any method or constructor is described as being globally accessible. The means that this variable can be called, modified, or reset from anywhere else in your code file. As an example, take a glance at the small code snippet below.


var name = "Jamal",
    city = "Atlatna";

var update = function(name,city){
    this.name = name;
    this.city = city;
}

 

The above code is setting two variables, name and city. These same variables are then being modified with the update function. This is possible because the variables were declared globally, outside of any function or constructor.

Now that what one of the problems I noticed in the code in the prior post in these series. To correct that, we’ve made all the variables necessary to be accessed in multiple places globally accesible. Take another chance look below.


this.projName = projName;
this.endingAmount = endingAmount;
this.totalHours = totalHours;
this.days = days;
this.rate = rate;
var hoursToday = 0,
    hoursLeft,
    amountLeft;

Proper Variable Usage

 

When you’re working with a programming language typos are not your friend! Previously, our code was calling the hoursLeft variable in the console log messages. There is nothing wrong with that, that’s what we want, but whenever that method was called we got an error. That was because in the log message the variable being called was hoursleft, not hoursLeft.

Another example of misuse can be found in the amountLeft method. The code for that method is written correctly, and shouldn’t have any errors reported for it. Unfortunately should and what will happen don’t work well together. Before with the the old cold we would get the output of NaN with that method. Why is that?

That happened because in the equation being used for the amountLeft variable there was a misuse of a variable, rate. Before the equation was calling the undeclared rate variable, whereas what it wanted was the this.rate variable.

 

What Now?

 

Everything in our constructor class is working perfectly for what it needs to do, yay! After all that, you’re probably wondering what could be coming next. Well, next time we are going to refactor and expand on the functionality of this constructor class. That’s not for a while though, until then just be happy and play with what we’ve gotten to so far :)!!