简体   繁体   English

使用普通 Javascript 进行计算

[英]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.我尝试使用 java 脚本构建价格计算器。

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.您需要阅读计算 function 中的输入。
  • There's no need to have two kernel functions that only change one variable;不需要两个只改变一个变量的 kernel 函数; 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 .如果 Quantity 未在用户/视图 arrays 中列出,则它将始终返回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">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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