转换货币的功能,代码通过8/9测试,不知道通过上次测试缺少什么?



这就是问题所在:让我们创建一个convertCurrency函数,该函数将接受参数,如以格里夫纳(’s(为单位的金额、exchangeRate、所需货币的name currencyName和返回字符串Give their{x}}{currencyName}}(s(。该函数必须检查员工是否正确填写了参数,并在金额和/或汇率包含负值或零的情况下返回消息"输入有效数据"。如果结果不是整数,请将值四舍五入到小数点后两位。

这是我写的代码:

function convertCurrency(amount, exchangeRate, currencyName) {
// write code here
const result = amount * exchangeRate;
if (amount && exchangeRate > 0) {
if (Number.isInteger(result)) {
return `Give them ${result} ${currencyName}('s)`;
} else {
return `Give them ${result.toFixed(2)} ${currencyName}('s)`;
}
} else if (amount || exchangeRate <= 0) {
return 'Enter valid data';
}
}

这就是我得到的错误:当金额=-500,汇率=0.14,currencyName="比索"时,函数"convertCurrency"应返回"输入有效数据">

您的行if (amount && exchangeRate > 0)在您的头脑中被解析为"数量>0和汇率>0〃;。但是它被JavaScript解析为";(金额(和(汇率>0(";。其被评价为";(-500(和(0.14>0(〃;,因此"-500和真";,因此";真";。

您需要实际放置amount > 0 && exchangeRate > 0

相关内容

最新更新