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

JavaScript (?) 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

variable *= percent/100.0

Okay thanks, but could you explain that bit by bit.

I don't know what's vague.

x *= y

is equivalent to

x = x * y

so

variable = variable * percent/100.0

You divide the percent (in this case 10) by 100.0 to get a fractional result (in this case 0.1).

variable = variable * 0.1

You then multiply the resulting fraction by the current value stored in the variable and assign this result back to the original variable

var ten = 10;

ten *= 10/100.0;

After the above snippet is executed the variable ten will store the number 1

Thanks, its not vague its just that my JavaScript knowledge is limited.