const taxCalculator (price, state) => {
let state = 'NY' || 'NJ';
if (state === 'NY'){
return price * 1.04;
} else {
return price * 1.06625;
}
}
//我被要求定义一个名为taxCalculator的函数,该函数接受商品和州的价格。 taxCalculator应该产生税后金额。如果NY-4%的营业税和NJ是6.625%的营业税。
谢谢!
固定您的代码,请尝试以下操作:
const taxCalculator = (price, state) => {
if (state === 'NY' ) {
return price * 1.04;
} else {
return price * 1.06625;
}
}
console.log(taxCalculator(100,'NY'));
缺少第一个=
|var, const, let| identifier = (@param1, ...@paramN) => {...
...
return result;
}
const taxCalculator = (price, state) => {...
[state
已经声明为第二个参数-删除let
以使其有效-删除整个表达式,因为它没有用:
let state = 'NY' || 'NJ';
if/else
语句是否已经分类,如果state
为'NY'
。上面的表达式甚至没有真正定义,修改等。state
完全没有。
注意:演示中评论了详细信息
/*
= Two ternaries:
- if state is 'NY' [?] then tax is 1.04
[:] if state is 'NJ' [?] then tax is 1.06625
[:] otherwise tax is nothing
- if tax is nothing [?] then return a message
[:] otherwise return the product of price and tax as a
String prefixed by '$' and truncated to 2 decicimals.
*/
const taxCalculator = (price, state) => {
let tax = state === 'NY' ? 1.04 : state === 'NJ' ? 1.06625 : null;
return tax === null ? `${state} not recognized` : '$' + (price * tax).toFixed(2);
}
console.log(taxCalculator(5.29, 'NJ'));
console.log(taxCalculator(5.29, 'CA'));
console.log(taxCalculator(5.29, 'NY'));
这是错误的,您的“状态”变量是函数的参数。请尝试:
const taxCalculator(price,state)=>{
return (price * (state==='NY'?1.04:1.06625))
}
此外,如果要支持NY和NJ以外的其他情况,可以在内部使用switch函数来比较“ state”变量值。