M.O.S. Studios Posted March 24, 2009 Share Posted March 24, 2009 Hey guys, I know this question shows how much of a newb in JavaScript I am. However I’m hopping you can help me out. I have this code in a function: if (myform.taxes_cost.value == 1)var tax1 = subTotal * 0.05; if (myform.taxes_cost.value == 1)var tax2 = (subTotal+parseFloat(tax1)) * 0.075; if (myform.taxes_cost.value == 1)var tax3 = (subTotal+parseFloat(tax1)+parseFloat(tax2)) * 0.03; As you can see I have the same condition in all three of those statements. Is their any way to make the one if statement that will define all those var's? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/150947-solved-combine-3-if-statements-into-one/ Share on other sites More sharing options...
Maq Posted March 24, 2009 Share Posted March 24, 2009 This is a very basic concept in most languages... if (myform.taxes_cost.value == 1) { var tax1 = subTotal * 0.05; var tax2 = (subTotal+parseFloat(tax1)) * 0.075; var tax3 = (subTotal+parseFloat(tax1)+parseFloat(tax2)) * 0.03; } Quote Link to comment https://forums.phpfreaks.com/topic/150947-solved-combine-3-if-statements-into-one/#findComment-793056 Share on other sites More sharing options...
Floydian Posted March 24, 2009 Share Posted March 24, 2009 By the way, it's recommend that you always enclose IF blocks in curlies --- { and } There are documented cases where javascript gets confused when there aren't { and } because of two possible syntactically correct interpretations. Quote Link to comment https://forums.phpfreaks.com/topic/150947-solved-combine-3-if-statements-into-one/#findComment-793182 Share on other sites More sharing options...
M.O.S. Studios Posted March 25, 2009 Author Share Posted March 25, 2009 Awesome works 100%, and thanks for the advice. Quote Link to comment https://forums.phpfreaks.com/topic/150947-solved-combine-3-if-statements-into-one/#findComment-793601 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.