如何正确实现此 switch 语句?
How do I correctly implement this switch statement?
我是 Javascript 的新手,我无法让我的 switch 语句与每个函数或我的默认值一起正常工作。我尝试过的每一件事都解决了一个问题,但又造成了另一个问题。如何正确使用 switch 语句为我的案例获取正确的 return。
const f_to_c = (f = 32) => {
total = 5 / 9 * (f - 32)
result = (f + 'F = ' + total + 'C')
return result;
}
console.log(f_to_c());
const c_to_f = (y = 0) => {
total = 9 / 5 * (y) + 32
results = (y + 'C = ' + total + 'F')
return results;
}
console.log(c_to_f())
const convertTemp = (x = 'str', int = 0) => {
switch ('f', 'c') {
case 'c':
return c_to_f(int);
case 'f':
return f_to_c(int);
default:
return ('Not an accurate temperature degree type..')
}
};
console.log(convertTemp())
您需要多读一点如何创建 switch 语句,这里很好source
const convertTemp = (x = 'c', int = 0) => {
switch(x){
case 'c':
return c_to_f(int);
case 'f':
return f_to_c(int);
default:
return ('Not an accurate temperature degree type..')
}
};
我是 Javascript 的新手,我无法让我的 switch 语句与每个函数或我的默认值一起正常工作。我尝试过的每一件事都解决了一个问题,但又造成了另一个问题。如何正确使用 switch 语句为我的案例获取正确的 return。
const f_to_c = (f = 32) => {
total = 5 / 9 * (f - 32)
result = (f + 'F = ' + total + 'C')
return result;
}
console.log(f_to_c());
const c_to_f = (y = 0) => {
total = 9 / 5 * (y) + 32
results = (y + 'C = ' + total + 'F')
return results;
}
console.log(c_to_f())
const convertTemp = (x = 'str', int = 0) => {
switch ('f', 'c') {
case 'c':
return c_to_f(int);
case 'f':
return f_to_c(int);
default:
return ('Not an accurate temperature degree type..')
}
};
console.log(convertTemp())
您需要多读一点如何创建 switch 语句,这里很好source
const convertTemp = (x = 'c', int = 0) => {
switch(x){
case 'c':
return c_to_f(int);
case 'f':
return f_to_c(int);
default:
return ('Not an accurate temperature degree type..')
}
};