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 trialNazmul Haque Sajol
13,594 PointsInteger & Floating Conflict
What if, I'm not sure about the visitor input type in a form, where i have 2 variables to add together and return to our visitor . One input would be a integer and otherone floating. How should i handle this situation ?
2 Answers
Riley Hilliard
Courses Plus Student 17,771 PointsI am not entirely sure the question, however I think you are asking if you can add a number and a floating number? The answer to that is yes:
var x = 1;
var y = 1.001;
console.log(x + y); // returns 2.001
You also might be asking about what if the input comes into the JavaScript as a string. You can parse a string into a number like so:
var x = "1";
var y = "1.001";
console.log(x + y); // returns "11.001"
console.log(parseInt(x, 10) + parseFloat(y, 10)); // returns 2.001
Iain Simmons
Treehouse Moderator 32,305 PointsOr just use parseFloat
to parse the input if you're not sure if it will be an integer or float, since it can handle integers no problem.