【问题标题】:Switch Statement: Applying Multiple Arguments in One OutputSwitch 语句:在一个输出中应用多个参数
【发布时间】:2023-01-11 11:58:23
【问题描述】:

我想弄清楚如何将两个不同的条件应用于 switch 语句中的一个输出。例如,我有一个函数可以根据第二个参数获得的参数来设置字符串样式。第一个和第二个输出没问题,因为它只有一个样式参数,但第三个输出有。我找不到将大写和反向样式应用于字符串的方法。我试图循环一个 switch 语句。我想知道是否有任何好的解决方案。

function caseStyle(string, style) {
  function toUpper(string) {
    string = string.toUpperCase();
    return string;
  }

  function toReversed(string) {
    string = string.split("").reverse().join("");
    return string;
  }

  switch (style) {
    case "upper":
      string = toUpper(string);
      break;
    case "reversed":
      string = toReversed(string);
      break;
  }
  return string;
}

console.log(caseStyle("hello", "upper")); //output: HELLO
console.log(caseStyle("hello", "reversed")); //output: olleh
console.log(caseStyle("hello", ["upper", "reversed"])); // expected output: OLLEH

【问题讨论】:

  • 你需要介绍新的案例。 case "reversedUpper": return toUpper(toReversed(string))
  • 你需要检查style是否是一个数组,并相应地处理
  • 检查样式是否为数组。如果不是像style = [style] 那样使它成为单个元素数组,然后在测试每个元素的样式数组上执行 for 循环。这将允许您拥有任意数量的样式

标签: javascript


【解决方案1】:

你需要检查style是否是一个数组,并相应地处理

或者,强制它成为一个数组 style = [style].flat() 然后迭代该数组

.flat() 将在传入数组的情况下展平数组

如下

function caseStyle(string, style) {
  style = [style].flat();
  function toUpper(string) {
    string = string.toUpperCase();
    return string;
  }

  function toReversed(string) {
    string = string.split("").reverse().join("");
    return string;
  }
  style.forEach(style => {
    switch (style) {
      case "upper":
        string = toUpper(string);
        break;
      case "reversed":
        string = toReversed(string);
        break;
    }
  });
  return string;
}

console.log(caseStyle("hello", "upper")); //output: HELLO
console.log(caseStyle("hello", "reversed")); //output: olleh
console.log(caseStyle("hello", ["upper", "reversed"])); // expected output: OLLEH

【讨论】:

    【解决方案2】:

    您可以通过以下方式使用switch (true)

    function caseStyle(string, style) {
          function toUpper(string) {
            string = string.toUpperCase();
            return string;
          }
    
      function toReversed(string) {
        string = string.split("").reverse().join("");
        return string;
      }
    
      switch (true) {
        case style === "upper":
          string = toUpper(string);
          break;
        case style === "reversed":
          string = toReversed(string);
          break;
        case style.includes("upper") && style.includes("reversed"):
          string = toUpper(toReversed(string))
          break;
        default:
           break;
      }
      return string;
    }
    
    console.log(caseStyle("hello", "upper"));//output: HELLO
    console.log(caseStyle("hello", "reversed"));//output: olleh
    console.log(caseStyle("hello", ["upper", "reversed"]));// expected output: OLLEH

    【讨论】:

    • 当然可以,但是如果您想添加更多“功能”怎么办……那么您的案例陈述将变得笨拙
    • 这不是你要的
    【解决方案3】:

    为什么不运行一个简单的 for-of-loop 呢?您还需要检查输入是否不是数组,如果不是,则将其转换为数组。

    function caseStyle(string, styles) {
      function toUpper(string) {
        string = string.toUpperCase();
        return string;
      }
    
      function toReversed(string) {
        string = string.split("").reverse().join("");
        return string;
      }
      
      if (!(styles instanceof Array)) styles = [styles];
      for (const style of styles) {
        switch (style) {
          case "upper":
            string = toUpper(string);
            break;
          case "reversed":
            string = toReversed(string);
            break;
        }
      }
      return string;
    }
    
    console.log(caseStyle("hello", "upper")); //output: HELLO
    console.log(caseStyle("hello", "reversed")); //output: olleh
    console.log(caseStyle("hello", ["upper", "reversed"])); // expected output: OLLEH

    【讨论】:

    • 你运行代码了吗?
    • @JaromandaX 抱歉,我有一个小错误。我现在修好了。谢谢!
    • 还有 Array.isArray 函数 - 但我更喜欢我的解决方案 :p 但这说明在 javascript 中有很多方法可以做简单的事情
    【解决方案4】:

    我可能不会整体采用这种方法,但这可能适合您当前的方案;特别是如果您控制传递给这些函数的输入“样式”。

    function caseStyle(string, style) {
      function toUpper(string) {
        string = string.toUpperCase();
        return string;
      }
    
      function toReversed(string) {
        string = string.split("").reverse().join("");
        return string;
      }
    
      switch (style.toString()) {
        case "upper":
          string = toUpper(string);
          break;
        case "reversed":
          string = toReversed(string);
          break;
        case "upper,reversed":
          string = toUpper(toReversed(string));
          break;
    
    }
      return string;
    }
    
    console.log(caseStyle("hello", "upper")); //output: HELLO
    console.log(caseStyle("hello", "reversed")); //output: olleh
    console.log(caseStyle("hello", ["upper", "reversed"])); // expected output: OLLEH

    【讨论】:

      猜你喜欢
      • 2018-09-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-07
      相关资源
      最近更新 更多