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
victor ollervidez
14,934 PointsNeed help with parseInt
Hi I'm making a small Js project and I am having trouble getting the string from an input text box and parsing it to an integer. example:
//user input would be 5'6 for example
var userHeight = document.getElementById("height");
submitButton.onclick = function(){
console.log(parseInt(userHeight.value, 10));
}
So whats happening is that every time I submit I only get 5 in the console.
Any help is appreciated :D
2 Answers
Brent Cralley
18,541 PointsIt is possible(Anything is!), but it is going to involve a lot of work-arounds. Also, if not done properly, it could create many security risks in your form if you allow the user to enter data that isn't numeric when you need only numeric data.<br> <br> How does a form like this look to you?<br> Enter height:<br> Feet: ____<br> Inches:____<br> <br>
var userHeightFt = document.getElementById("HeightFt");
var userHeightIn = document.getElementById("HeightIn");
submitButton.onclick = function(){
console.log("You are " + userHeightFt + "feet and " + userHeightIn + "inches tall!"));
}
Now, it's been a while since I've used JS, so hopefully there aren't any syntax errors there. :-P The reasons I want to go with this format are:<br> <br> It makes it much easier to do conversions if you ever think you'll have to do it in the future. Need to find how many inches tall this person is? userHeightIn + (userHeightFt * 12) And there you are!<br> <br> Need to find how many cm tall the person is? (userHeightIn + (userHeightFt * 12)) * 2.54(or whatever the conversion is...), and there it is! <br> It requires much less code up front, and I think actually makes it easier for the user to understand. Instead of needing to have a label on the front end telling the user "Make sure to enter the value for feet first, then an apostrophie ('), and then the value for inches. Do it exactly like this, or we'll have an error!" <br> <br> I hope you don't feel like I'm belittling your ways. Again, if you really do want to do it that way, we have the power to with the programming languages at our disposal. I hope you can understand where I'm coming from and why I chose this logic.
Happy coding!
Brent Cralley
18,541 PointsDoes the user input " 5'6 " with the single quote between the feet and inches? If so, this could be your problem. The easiest(and possibly most practical) work-around to this would be to have one field for the height in feet, and one for the height in inches. The "feet" measurement unit uses a base-12 system(12 inches = 1 foot), so defining it as a base-ten unit as you did in your code will further confuse things. BUT if your program requires this type of input for some reason, we can work on that. :-)
victor ollervidez
14,934 PointsHey Brent! Yes the user inputs 5'6 with the apostrophe between the feet and inches. Reason I did it like that is because I want it to be simple for the user. Just felt like having two separate input boxes would clutter the form up. I have looked every where online for a solution but haven't found one starting to think it's not possible?
Thank You
victor ollervidez
14,934 Pointsvictor ollervidez
14,934 PointsNow this makes a lot more sense! Thank You!! :D