【问题标题】:How do I correctly implement this switch statement?如何正确实现此 switch 语句?
【发布时间】:2021-06-12 01:56:15
【问题描述】:

我是 Javascript 新手,我无法让我的 switch 语句与每个函数或我的默认值一起正常工作。我尝试过的每一件事都解决了一个问题,但又产生了另一个问题。如何正确使用我的 switch 语句为我的案例获得正确的回报。

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

【问题讨论】:

  • 究竟出了什么问题,“不正确”的输出是什么,您预计会发生什么?
  • ('f', 'c') 这是什么意思?..

标签: javascript function switch-statement return


【解决方案1】:

你需要多读一点如何创建 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..')
    }
};

【讨论】:

    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2021-06-22
    • 1970-01-01
    • 1970-01-01
    • 2019-10-08
    • 2019-04-07
    • 1970-01-01
    • 2013-09-03
    相关资源
    最近更新 更多