税收计算器功能 Javascript 代码无效
TaxCalculator function Javascript code is not working
const taxCalculator (price, state) => {
let state = 'NY' || 'NJ';
if (state === 'NY'){
return price * 1.04;
} else {
return price * 1.06625;
}
}
// 我被要求定义一个名为 taxCalculator 的函数,它接受一个项目的价格和一个状态。 taxCalculator 应该产生税后金额。如果纽约 - 4% 的销售税和新泽西州是 6.625% 的销售税。
谢谢!
那是错误的,你的 "state" 变量是你函数的一个参数。请尝试:
function taxtaxCalculator(price, state) {
let resp
switch (state) {
case 'NY':
resp = price * 1.04
break;
case 'NJ':
resp = price * 1.06625
break;
default:
//Another case not defined in the question.
resp = price * 1
break;
}
return resp
}
此外,如果您想支持 NY 和 NJ 以外的其他情况,您可以在内部使用 switch 函数来比较 "state" 变量值。
已修复您的代码,试试这个:
const taxCalculator = (price, state) => {
if (state === 'NY' ) {
return price * 1.04;
} else {
return price * 1.06625;
}
}
console.log(税收计算器(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) => {
let state = 'NY' || 'NJ';
if (state === 'NY'){
return price * 1.04;
} else {
return price * 1.06625;
}
}
// 我被要求定义一个名为 taxCalculator 的函数,它接受一个项目的价格和一个状态。 taxCalculator 应该产生税后金额。如果纽约 - 4% 的销售税和新泽西州是 6.625% 的销售税。
谢谢!
那是错误的,你的 "state" 变量是你函数的一个参数。请尝试:
function taxtaxCalculator(price, state) {
let resp
switch (state) {
case 'NY':
resp = price * 1.04
break;
case 'NJ':
resp = price * 1.06625
break;
default:
//Another case not defined in the question.
resp = price * 1
break;
}
return resp
}
此外,如果您想支持 NY 和 NJ 以外的其他情况,您可以在内部使用 switch 函数来比较 "state" 变量值。
已修复您的代码,试试这个:
const taxCalculator = (price, state) => {
if (state === 'NY' ) {
return price * 1.04;
} else {
return price * 1.06625;
}
}
console.log(税收计算器(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'));