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
Adam Flanagan
8,022 PointsJavaScript (?) help needed
Okay, so I want to make my first app, I'm going to be using PhoneGap. Pretty simple, tip calculator based on your country. There will be an input that says 'insert country' then if statements saying eg if UK then take ten percent of inputted bill amount. My question is....what would the script be for say, for example taking ten percent of a variable??
Thanks,
Adam
1 Answer
Aaron Daub
Courses Plus Student 96 Pointsvariable *= percent/100.0
Adam Flanagan
8,022 PointsAdam Flanagan
8,022 PointsOkay thanks, but could you explain that bit by bit.
Aaron Daub
Courses Plus Student 96 PointsAaron Daub
Courses Plus Student 96 PointsI don't know what's vague.
x *= yis equivalent to
x = x * yso
variable = variable * percent/100.0You divide the percent (in this case 10) by 100.0 to get a fractional result (in this case 0.1).
variable = variable * 0.1You then multiply the resulting fraction by the current value stored in the variable and assign this result back to the original variable
After the above snippet is executed the variable ten will store the number 1
Adam Flanagan
8,022 PointsAdam Flanagan
8,022 PointsThanks, its not vague its just that my JavaScript knowledge is limited.