2017-10-07 74 views
1

新手学生编码器,打破JavaScript代码插入功能

这里和我开发一个程序,可以提醒,一旦你的钱,你给它会计算小费金额类型节目,和税收来获得用户拥有的总金额。我有基本代码下来,并将其分成功能,但当我把一个数字显示为未识别。

这里是我的代码:

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(); 

+0

你的函数的所有期望得到的参数,但是当你打电话给他们,你不传递任何参数。另外,除了最后一个函数返回的值,你忽略了。所以当你最后调用'finalCost()'函数时,你不会传递任何参数,所以'foodCharge','taxAmount'等都是未定义的。请注意,您已经过度使用了'parseFloat()' - 您需要的唯一地方就是该值是一个字符串,即用户输入的值。顺便说一句,我已经在你的问题中格式化了代码,使它更具可读性(更改空白,不更改实际代码)。 – nnnnnn

+0

请编辑您的帖子,以便更详细地描述问题并指出您的问题是什么,而不是仅仅说出“我找不到问题”,我建议您阅读SO发布指南 – shafeen

+0

@shafeen - 为什么你会编辑删除演示片段?此外,OP还说明了问题所在,即结果显示为“未定义” - 运行已删除的代码段时很明显。 – nnnnnn

回答

0

你只需要当你从字符串解析浮点数parseFloat功能。您不需要解析常规数学运算的结果(如果两个数字都不是字符串)。当您将函数作为参数传递给时,警报()必须用括号括起来,否则您将引用传递给函数。

如果我正确理解你的问题,这里是你的程序:

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(); 
0

可以调节功能内finalCost被调用。注意,parseFloat()只需要在foodCharge功能

const TAXRATE = .095 
 
const TIPRATE = .2 
 

 
function foodCharge() { 
 
    return parseFloat(prompt("please enter the amount")); 
 
} 
 

 
function taxAmount(charge, tax) { 
 
    return charge * tax; 
 
} 
 

 
function subAmount(charge, tax) { 
 
    return charge + tax; 
 
} 
 

 
function tipAmount(tip, sub) { 
 
    return tip * sub; 
 
} 
 

 
function grandTotal(charge, tax, tip) { 
 
    return charge + tax + tip; 
 
} 
 

 
function finalCost() { 
 

 
    let _foodCharge = foodCharge(); 
 
    let _taxAmount = taxAmount(_foodCharge, TAXRATE); 
 
    let _subAmount = subAmount(_foodCharge, _taxAmount); 
 
    let _tipAmount = tipAmount(TIPRATE, _subAmount); 
 
    let _grandTotal = grandTotal(_foodCharge, _taxAmount, _tipAmount); 
 

 
    alert("Meal cost: " + _foodCharge + " \nTax: " + _taxAmount + " \nTip: " + 
 
    _tipAmount + " \nGrand total: " + _grandTotal); 
 
} 
 

 
finalCost();

+0

确定要尝试并分解您的代码(以便将来更好地理解这一点)....您所做的是创建具有所有原始名称的函数,然后在括号中添加它自己的变量ex。收费和税收等,然后为了将参数传递给finalCharge函数,您将分配的费用和税赋分配给它们各自受尊重的函数ex.let tax = taxAmount(charge,TAXRATE);这就是你如何在函数中传递参数 –

+0

@NathanCenter是的。为避免变量具有相同标识符的问题,带有'finalCost'的变量以下划线开头和下划线。 – guest271314