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) Constructor Functions and Prototypes Creating Multiple Instances with Constructors

How to keep a variable as an integer in a constructor function?

Here is the code:

function Calculator (x,y) { this.x = parseFloat(x); this.y = parseFloat(y); this.adding = function() {console.log(x+y)}; this.subtracting = function() {console.log(x-y)}; this.multiplying = function() {console.log(x*y)}; this.dividing = function() {console.log(x/y)}; }

It's a constructor function for a rudimentary calculator object. I parseFloated the x and y parameters in order to convert them to floats. Everything works fine when you create a new Calculator, like: var calc = new Calculator(x,y);

BUT.......if I change the code INSIDE each of the console.logs to this:

function Calculator (x,y) { this.x = x; this.y = y; this.adding = function() {console.log(" Add " + x+y)}; this.subtracting = function() {console.log(" Subtract " + x-y)}; this.multiplying = function() {console.log(" Multiply " + x*y)}; this.dividing = function() {console.log(" Divide " + x/y)}; }

THEN the parsefloating function seems to be irrelevant because the JavaScript interpreter will assume that X and Y are STRINGS. The adding function will add 2 strings and the subtracting function will deliver a NaN result.

This is the question: How can I build a constructor function that maintains the Floats inside console.logs?

2 Answers

Steven Parker
Steven Parker
229,644 Points

When you use the "+" symbol with a string, it's a concatenation operator. So if the next argument is a number, it is converted to a string and then tacked on. That's why adding returns all the values. But for subtracting, the "y" value is subtracted from the string made by the first concatenation, producing "NaN".

The solution is just to group the math operations to make sure they are performed first before any string conversions:

    console.log(" Add " + (x + y));
// ...
    console.log(" Subtract " + (x - y));
// etc...

Thank you very much. The problem is solved. :-) Thank you for your time. :-) Jorge

Phillip Kerman
PLUS
Phillip Kerman
Courses Plus Student 285 Points

I think all that's happening is that the + will create a string when EITHER (or both) its operands are strings. So, if do: console.log("someString " + myNumber);
You get a string.

Just put your number variables in parenthesis to make them evaluate (as numbers) first... or you can always put them them in the parentheses of Number()

Thank you very much. The problem is solved. :-) I appreciate the time you took to answer my question.