【问题标题】:How do I exit out of my switch statement?如何退出我的 switch 语句?
【发布时间】:2020-09-13 13:57:54
【问题描述】:

我的尝试:

const find = function (map, type) {

let location = [];

for(var i = 0; i < map.length; i++){
    for(var j = 0; j < map[i].length; j++){
        switch(true) {
            case (type === 'gold' && map[i][j] === 'R' ):
                location.push(j,i);
                break;
            case (type === 'silver' && (map[i][j] === 'S' || map[i][j] === 'R')):
                location.push(j,i);
                break;
            case (type === 'bronze' && (map[i][j] === 'S' || map[i][j] === 'R' || map[i][j] === 'M' ) ):
                location.push(j,i);
                break;
            default:
                return 'false';
                break;
        }
    }

}

return location;
};
console.log(find(
    [
        // COLUMNS ARE X
        // 0    1    2    3    4    5
        ['s', 'R', 's', 'S', 'n', 'M'], // 0 ROWS ARE Y
        ['s', 'M', 's', 'S', 'r', 'M'], // 1
        ['s', 'M', 's', 'R', 'r', 'm'], // 2
        ['S', 'r', 's', 'm', 'r', 'M'], // 3
        ['S', 'r', 's', 'm', 'r', 'M'], // 4
        ['S', 'r', 'S', 'M', 'M', 'S']  // 5
    ],
    'gold'
));

我基本上是遍历一个表示矿物地图的二维数组,并通过将索引记录到数组位置并返回它来返回与我的 switch 语句中的条件相匹配的第一块土地的坐标。

我有两个主要问题:

如何编码,以便在找到与我的案例条件匹配的第一块土地后立即终止循环?现在 JS 将继续循环遍历整个 2D 数组,推入更多我不希望的坐标。

如果没有找到,我希望 JS 简单地返回字符串 False。但是,即使 case 语句的计算结果为 True,这似乎也在发生。只是想知道我是否有某种错字?

【问题讨论】:

    标签: javascript arrays switch-statement


    【解决方案1】:

    布尔值不是 switch 语句的理想用例 -

    switch(true) - 参数应该接受一个变量。您正在传递一个常量变量。变量应与 case 语句匹配。

    switch(bool)的正确用法,确实应该是

    var myBool = getValue();
    switch(myBool) {
      case true:
        console.log('true');
        break;
      case false:
        console.log('false');
        break;
    

    对于逻辑,我建议使用if/else

                if (type === 'gold' && map[i][j] === 'R' ){
                  location.push(j,i);
                } else if (type === 'silver' && (map[i][j] === 'S' || map[i][j] === 'R')){
                  location.push(j,i);
                } else if (type === 'bronze' && (map[i][j] === 'S' || map[i][j] === 'R' || map[i][j] === 'M' ) ) {
                  location.push(j,i);
                }
    

    【讨论】:

      【解决方案2】:

      您可以使用检查返回索引数组。

      const find = function(map, type) {
          for (var i = 0; i < map.length; i++) {
              for (var j = 0; j < map[i].length; j++) {
                  if (type === 'gold' && map[i][j] === 'R' ||
                      type === 'silver' && (map[i][j] === 'S' || map[i][j] === 'R') ||
                      type === 'bronze' && (map[i][j] === 'S' || map[i][j] === 'R' || map[i][j] === 'M')
                  ) {
                      return [j, i];
                  }
              }
          }
          return 'False';
      };
      

      【讨论】:

        【解决方案3】:

        你的函数第一个命中default 它只是返回false 所以你想改变它。这里主要使用您的逻辑进行重构,并对 switch 案例进行了一些更改:

        const find = (mapArr, type) => {
        for (let i = 0; i < mapArr.length; i++) {
            for (let j = 0; j < mapArr[i].length; j++) {
                switch (type) {
                    case 'gold': {
                        if (mapArr[i][j] === 'R') return [j, i];
                    }
                    case 'silver': {
                        if (mapArr[i][j] === 'S') {
                            return [j, i];
                        }
                    }   
                    case 'bronze': {
                        if (mapArr[i][j] === 'M') {
                            return [j, i];
                        }
                    }
                    default: continue;
                }
            }
        
        }
        };
        find(
            [
                // COLUMNS ARE X
                // 0    1    2    3    4    5
                ['s', 'R', 's', 'S', 'n', 'M'], // 0 ROWS ARE Y
                ['s', 'M', 's', 'S', 'r', 'M'], // 1
                ['s', 'M', 's', 'R', 'r', 'm'], // 2
                ['S', 'r', 's', 'm', 'r', 'M'], // 3
                ['S', 'r', 's', 'm', 'r', 'M'], // 4
                ['S', 'r', 'S', 'M', 'M', 'S']  // 5
            ],
            'bronze'
        )
        

        【讨论】:

          【解决方案4】:

          如果使用switch case 没有找到任何内容,这将返回false

          find = function (map, type) {
          
            let location = [];
            let notFound = 0;
            for(var i = 0; i < map.length; i++){
              for(var j = 0; j < map[i].length; j++){  
          
                  switch(true) {
                    case (type === 'gold' && map[i][j] === 'R' ):
                      location.push(j,i);
                      break;
          
                    case (type === 'silver' && (map[i][j] === 'S' || map[i][j] === 'R')):
                      location.push(j,i);
                      break;
          
                    case (type === 'bronze' && (map[i][j] === 'S' || map[i][j] === 'R' || map[i][j] === 'M' ) ):
                      location.push(j,i);
                      break;
                    default:
                      notFound++;
                  }
                  if(location.length == i + 1) break;
                }
          
              }
            if(notFound == map.length) return false;
            return location;
          };
          
          console.log(find(
            [
              // COLUMNS ARE X
              // 0    1    2    3    4    5
              ['s', 'R', 's', 'S', 'n', 'M'], // 0 ROWS ARE Y
              ['s', 'M', 's', 'S', 'r', 'M'], // 1
              ['s', 'M', 's', 'R', 'r', 'm'], // 2
              ['S', 'r', 's', 'm', 'r', 'M'], // 3
              ['S', 'r', 's', 'm', 'r', 'M'], // 4
              ['S', 'r', 'S', 'M', 'M', 'S']  // 5
            ],
            'gold'
          ));
          

          【讨论】:

            猜你喜欢
            • 2013-10-21
            • 1970-01-01
            • 2020-04-13
            • 1970-01-01
            • 1970-01-01
            • 2020-06-10
            • 2013-06-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多