# Percentage Calculator Function

Something I seem to need a lot.

function calculate(form, id) { a = form.q1.value; b = form.q2.value; switch(id) { case 1: total = (a / 100) * b; //What is a percent of b? break; case 2: total = (a / b) * 100; //a is what percent of b? break; case 3: total = (b - a) / a * 100; //What is the percentage increase/decrease from a to b? break; case 4: total = (a / b) * 100; //a is b percent of what? break; } form.total.value = total; }