将JavaScript代码分解为函数
新手学生程序员 在这里,我正在开发一个程序,可以提醒程序,一旦你输入你给的钱的数量,它将计算小费,并征税以获得用户拥有的总金额。我把基本代码记下来,并把它分成几个函数,但当我输入一个数字时,它显示为未识别的 这是我的密码:将JavaScript代码分解为函数,javascript,helpers,Javascript,Helpers,新手学生程序员 在这里,我正在开发一个程序,可以提醒程序,一旦你输入你给的钱的数量,它将计算小费,并征税以获得用户拥有的总金额。我把基本代码记下来,并把它分成几个函数,但当我输入一个数字时,它显示为未识别的 这是我的密码: const TAXRATE=.095 const TIPRATE=.2 function foodCharge (foodCharge) { return parseFloat(prompt("please enter the amount")); } foodChar
const TAXRATE=.095
const TIPRATE=.2
function foodCharge (foodCharge) {
return parseFloat(prompt("please enter the amount"));
}
foodCharge ();
function taxAmount (foodCharge,TAXRATE) {
return parseFloat(foodCharge*TAXRATE);
}
taxAmount();
function subAmount (foodCharge,taxAmount) {
return parseFloat(foodCharge+taxAmount);
}
subAmount ();
function tipAmount (TAXRATE,subAmount) {
return parseFloat (TAXRATE*subAmount);
}
tipAmount ();
function grandTotal (foodCharge, taxAmount, tipAmount) {
return grandTotal=parseFloat(foodCharge+taxAmount+tipAmount)
}
grandTotal ();
function finalCost(foodCharge,taxAmount, tipAmount, grandTotal ) {
alert ("Meal cost: "+ foodCharge + " \nTax: " + taxAmount + " \nTip: " +
tipAmount +" \nGrand total: " + grandTotal);
}
finalCost();
您可以调整要在
finalCost
中调用的函数。注意,parseFloat()
仅在foodCharge
功能中是必需的
const TAXRATE=.095
常数TIPRATE=.2
函数foodCharge(){
返回(提示(“请输入金额”);
}
功能税金额(费用、税款){
退货费*税金;
}
功能子安装(费用、税费){
退货费+税金;
}
功能tipAmount(tip,sub){
返回提示*sub;
}
功能总计(费用、税金、小费){
退货费+税金+小费;
}
函数finalCost(){
设_foodCharge=foodCharge();
let _taxAmount=taxAmount(_foodCharge,TAXRATE);
让_subAmount=subAmount(_foodCharge,_taxAmount);
设_tipAmount=tipAmount(TIPRATE,_submount);
让grandTotal=grandTotal(foodCharge,taxAmount,tipAmount);
警报(“餐费:”+\u食品费+”\nTax:“+\u税额+”\nTip:”+
_tipAmount+“\n总计:”+\u grandTotal);
}
最终成本()
您可以调整要在finalCost
中调用的函数。注意,parseFloat()
仅在foodCharge
功能中是必需的
const TAXRATE=.095
常数TIPRATE=.2
函数foodCharge(){
返回(提示(“请输入金额”);
}
功能税金额(费用、税款){
退货费*税金;
}
功能子安装(费用、税费){
退货费+税金;
}
功能tipAmount(tip,sub){
返回提示*sub;
}
功能总计(费用、税金、小费){
退货费+税金+小费;
}
函数finalCost(){
设_foodCharge=foodCharge();
let _taxAmount=taxAmount(_foodCharge,TAXRATE);
让_subAmount=subAmount(_foodCharge,_taxAmount);
设_tipAmount=tipAmount(TIPRATE,_submount);
让grandTotal=grandTotal(foodCharge,taxAmount,tipAmount);
警报(“餐费:”+\u食品费+”\nTax:“+\u税额+”\nTip:”+
_tipAmount+“\n总计:”+\u grandTotal);
}
最终成本()代码>仅当您从字符串解析浮点数时,才需要parseFloat函数。您不需要解析常规数学运算的结果(如果两个数字都不是字符串)。将函数作为参数传递给alert()时,必须使用括号()对其进行传递,否则将引用传递给函数
如果我正确理解您的问题,以下是您的计划:
const TAXRATE=0.095
const TIPRATE=0.2
function foodCharge (foodCharge) {
return parseFloat(prompt("please enter the amount"));
}
var charge = foodCharge ();
function taxAmount (charge, rate) {
return charge*rate;
}
var tax = taxAmount(charge, TAXRATE);
function subAmount (charge,tax) {
return charge+tax;
}
var amount = subAmount (charge,tax);
function tipAmount (rate,amount) {
return rate*amount;
}
var tip = tipAmount(TAXRATE,amount);
function grandTotal () {
return charge+tax+tip;
}
function finalCost() {
alert ("Meal cost: "+ charge + " \nTax: " + tax + " \nTip: " + amount +" \nGrand total: " + grandTotal());
}
finalCost();
仅当您从字符串解析浮点数时,才需要parseFloat函数。您不需要解析常规数学运算的结果(如果两个数字都不是字符串)。将函数作为参数传递给alert()时,必须使用括号()对其进行传递,否则将引用传递给函数
如果我正确理解您的问题,以下是您的计划:
const TAXRATE=0.095
const TIPRATE=0.2
function foodCharge (foodCharge) {
return parseFloat(prompt("please enter the amount"));
}
var charge = foodCharge ();
function taxAmount (charge, rate) {
return charge*rate;
}
var tax = taxAmount(charge, TAXRATE);
function subAmount (charge,tax) {
return charge+tax;
}
var amount = subAmount (charge,tax);
function tipAmount (rate,amount) {
return rate*amount;
}
var tip = tipAmount(TAXRATE,amount);
function grandTotal () {
return charge+tax+tip;
}
function finalCost() {
alert ("Meal cost: "+ charge + " \nTax: " + tax + " \nTip: " + amount +" \nGrand total: " + grandTotal());
}
finalCost();
所有函数都希望接收参数,但在调用它们时,不会传递任何参数。此外,除最后一个函数外,所有函数都返回您忽略的值。因此,当您在最后调用finalCost()
函数时,不会传递任何参数,因此foodCharge
,taxantium
等都是未定义的。请注意,您过度使用了parseFloat()
——您唯一需要的地方是当值是字符串时,即用户输入的值。顺便说一句,我已经格式化了你问题中的代码,使其更具可读性(更改为空白,不更改实际代码)。请编辑你的帖子主题,以便更详细地描述问题,并指出你的具体问题,而不是只说“我无法解决问题”,我恳请你阅读这篇文章guidelines@shafeen-为什么要编辑以删除演示片段?此外,OP确实说明了问题所在,即结果显示为“未定义”-这在运行您删除的代码段时很明显。@nnnnn-是这样吗?当我编辑的时候,我没有看到任何其他的编辑,不知道发生了什么,我的bad@shafeen-哦,好吧,如果两个人同时编辑,有时StackOverflow会感到困惑,所以可能就是这样。很抱歉责怪您。您的所有函数都希望接收参数,但当您调用它们时,您不会传递任何参数。此外,除最后一个函数外,所有函数都返回您忽略的值。因此,当您在最后调用finalCost()
函数时,不会传递任何参数,因此foodCharge
,taxantium
等都是未定义的。请注意,您过度使用了parseFloat()
——您唯一需要的地方是当值是字符串时,即用户输入的值。顺便说一句,我已经格式化了你问题中的代码,使其更具可读性(更改为空白,不更改实际代码)。请编辑你的帖子主题,以便更详细地描述问题,并指出你的具体问题,而不是只说“我无法解决问题”,我恳请你阅读这篇文章guidelines@shafeen-为什么要编辑以删除演示片段?此外,OP确实说明了问题所在,即结果显示为“未定义”-这在运行您删除的代码段时很明显。@nnnnn-是这样吗?当我编辑的时候,我没有看到任何其他的编辑,不知道发生了什么,我的bad@shafeen-哦,好吧,如果两个人同时编辑,有时StackOverflow会感到困惑,所以可能就是这样。很抱歉责怪了您。请尝试分解您的代码(以便更好地理解此代码以备将来使用)。。。。您所做的是使用所有的源代码创建函数