简体   繁体   中英

Making calulation with Plain Javascript


Since years I am learning with stack overflow, however this is my fist question.
I try to build a price-calculator with java script.

The idea is selecting a license-model (selector), using then a Base-Price (via input field), selecting the user-quantifier (via input field) which has a multiply factor linked. In the end the Base-Price will get multiplied and the final Price will be displayed.

I am quite comfortable that the math part is correct however there must be syntax error I am dismissing. I hope this is a valuable question, since I am getting stuck some time with this.

Would be great if someone has some input on this, thanks.

Here are my thoughts:

 // get input from inputfiels var Licence = parseInt(document.getElementById("Licence").value); var Quantity = parseInt(document.getElementById("Quantity").value); var Basis = parseInt(document.getElementById("Basis").value); // Desktop User numbers var User = [3, 8, 16, 36, 50]; // Web View numbers var View = [20, 50, 80, 200, 500]; // linked multiply factor (eg. 16 users = multiply factor x 3, or 50 views = multiply factor x 2) var Multiply = [1, 2, 3, 4, 5]; // Calcutation Desktop function Desktop_Kernel(Quantity, Basis) { var index; for (index of Multiply) { if (User[index - 1] == Quantity) { return Basis * Multiply[index - 1]; } } } // Calcutation Web function Web_Kernel(Quantity, Basis) { var index; for (index of Multiply) { if (View[index - 1] == Quantity) { return Basis * Multiply[index - 1]; } } } // Licece Model: Desktop or Web function Licence_Selector() { if (Licence == 1) { var Price = Desktop_Kernel(Quantity, Basis); } else if (Licence == 2) { var Price = Web_Kernel(Quantity, Basis); } document.getElementById("Price").value = Price; } // show output document.getElementById("Price").value = Desktop_Kernel(Quantity, Basis);
 <select id="Licence"> <option value="1">Desktop</option> <option value="2">Web</option> </select> <input type="number" id="Basis" placeholder="Basis Price" > <input type="number" id="Quantity" placeholder="Quantity"> <input type="button" value="Display Price" onclick="Licence_Selector()"> <input type="text" id="Price">

  • You need to read the inputs within your calculation function.
  • There's no need to have two kernel functions that only change one variable; instead pass that list in as an argument.
  • I'm not sure your calculations are correct; if Quantity is not listed within the User/View arrays, then it will always return undefined . Should that maybe be >= instead of == , to have a tier system? (I corrected it as such, not sure if that's what you meant.)

 // Desktop User numbers var User = [3, 8, 16, 36, 50]; // Web View numbers var View = [20, 50, 80, 200, 500]; // linked multiply factor (eg. 16 users = multiply factor x 3, or 50 views = multiply factor x 2) var Multiply = [1, 2, 3, 4, 5]; function Kernel(Numbers, Quantity, Basis) { var index; for (index of Multiply) { if (Numbers[index - 1] >= Quantity) { return Basis * Multiply[index - 1]; } } } // Licece Model: Desktop or Web function Recalculate() { // get input from inputfields var Licence = parseInt(document.getElementById("Licence").value); var Quantity = parseInt(document.getElementById("Quantity").value); var Basis = parseInt(document.getElementById("Basis").value); var Numbers = (Licence == 1? User: View); var Price = Kernel(Numbers, Quantity, Basis); document.getElementById("Price").value = Price; }
 <select id="Licence" value="1"> <option value="1">Desktop</option> <option value="2">Web</option> </select> <input type="number" id="Basis" placeholder="Basis Price" > <input type="number" id="Quantity" placeholder="Quantity"> <input type="button" value="Display Price" onclick="Recalculate()"> <input type="text" id="Price">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM