javascript - 遇到第一个 IF 时如何停止并且在遇到第一个 IF 后没有两个欢迎警报

var guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];

var name = prompt("What is your name?");

if (name === "TC") {
  alert("Welcome " + name + " to the party!");
  return;
} else {
  var firstChar = name.slice(0, 1);
  var upperCaseChar = firstChar.toUpperCase();
  var lastChar = name.slice(1, name.length);
  var lowerCaseChar = lastChar.toLowerCase();
  var completeChar = upperCaseChar + lowerCaseChar;
  var name = completeChar;
}

if (guestlist.includes(name)) {
  alert("Welcome " + name + " to the party!");
} else {
  alert("Sorry, " + name + ". Unfortunately, you are not on the list.")
}

我刚开始javascript。所以,我想看看如何让第一个 IF 停止。感谢你们!

回答1

尝试将您的代码更改为此。

const guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];

const name = prompt("What is your name?");

const firstChar = name.slice(0, 1).toUpperCase();
const lastChar = name.slice(1, name.length).toLowerCase();
const completeChar = firstChar + lastChar;

if (name === "TC") {
  alert("Welcome " + name + " to the party!");
} else if(guestlist.includes(completeChar)){
  alert("Welcome " + completeChar + " to the party!");
}else{
  alert("Sorry, " + completeChar + ". Unfortunately, you are not on the list.")
}

现在让我们一点一点地回顾一下为什么以及如何改变它:

  1. 根据新的 ECMA 编码标准将所有 var 更改为 const。

  2. 您可以堆叠方法,而不是:

    var firstChar = name.slice(0, 1); var upperCaseChar = firstChar.toUpperCase();

您可以像这样简单地在一行中执行此操作: const firstChar = name.slice(0, 1).toUpperCase();

  1. 去查看 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else,以便更好地了解如何编写包含多个条件的 if...else 语句。

回答2

只需将您的其他 if 语句放在您的第一个 if 的 else 语句中,并且在“if”的末尾,您不需要使用 return,它是无用的(除非它有更多代码并且您的代码在某个必须中断的函数中返回点停止执行)

var guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];
    
    var name = prompt("What is your name?");
    
    if (name === "TC") {
      alert("Welcome " + name + " to the party!");
      //return; no need for this
    } 
    else {
      var firstChar = name.slice(0,1);
    var upperCaseChar = firstChar.toUpperCase();
    var lastChar = name.slice(1,name.length);
    var lowerCaseChar = lastChar.toLowerCase();
    var completeChar = upperCaseChar + lowerCaseChar;
    var name = completeChar;
    
    if (guestlist.includes(name)) {
      alert("Welcome " + name + " to the party!");
    } 
    else {
        alert("Sorry, " + name + ". Unfortunately, you are not on the list.")
    }
    }

回答3

您可以使用停止功能在第一个 IF 中停止!

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>

</html>
<script>
  var guestlist = ["Angela", "Jack", "Thu", "Amy", "TC"];

  var name = prompt("What is your name?");

  if (name === "TC") {
    alert("Welcome " + name + " to the party!");
    stop();
  } else {
    var firstChar = name.slice(0, 1);
    var upperCaseChar = firstChar.toUpperCase();
    var lastChar = name.slice(1, name.length);
    var lowerCaseChar = lastChar.toLowerCase();
    var completeChar = upperCaseChar + lowerCaseChar;
    var name = completeChar;
  }

  if (guestlist.includes(name)) {
    alert("Welcome " + name + " to the party!");
  } else {
    alert("Sorry, " + name + ". Unfortunately, you are not on the list.");
  }
</script>

</body>

相似文章

c# - Recursion 映射问题

我有一个自引用的第n个深度结构。我想把map这个结构变成一个字符串列表。但我似乎无法成功。publicclassSPFolderStructure{publicstringName{get;set;}...

最新文章