Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请修改代码能跳出死循环 #725

Open
lgwebdream opened this issue Jul 6, 2020 · 2 comments
Open

请修改代码能跳出死循环 #725

lgwebdream opened this issue Jul 6, 2020 · 2 comments
Labels
JavaScript teach_tag 编程题 teach_tag

Comments

@lgwebdream
Copy link
Owner

while (1) {
  switch ("yideng") {
    case "yideng":
    //禁止直接写一句break
  }
}
@lgwebdream lgwebdream added JavaScript teach_tag 编程题 teach_tag labels Jul 6, 2020
@lgwebdream
Copy link
Owner Author

扫描下方二维码,获取答案以及详细解析,同时可解锁800+道前端面试题。

@GolderBrother
Copy link

// 方法一 手动抛出错误
try {
  while (1) {
    switch ("yideng") {
      case "yideng":
        //禁止直接写一句break
        throw new Error("This is an unknown error~");
    }
  }
} catch (error) {
  console.log(error);
}

// 方法二 break+标签跳出循环
out: while (1) {
  switch ("yideng") {
    case "yideng":
      //禁止直接写一句break
      break out;
  }
}
console.log("out");

// 方法三:for循环+continue+退出标签
out2: for (i = 0; i < 1; i++) {
  while (1) {
    switch ("yideng") {
      case "yideng":
        //禁止直接写一句break
        continue out2;
    }
  }
}
console.log("out2");

// 方法四:直接return退出
~(() => {
  while (1) {
    switch ("yideng") {
      case "yideng":
        //禁止直接写一句break
        return;
    }
  }
})();
console.log("return");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
JavaScript teach_tag 编程题 teach_tag
Projects
None yet
Development

No branches or pull requests

2 participants