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 停止。謝謝你們!
uj5u.com熱心網友回復:
嘗試將您的代碼更改為此。
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.")
}
現在讓我們一點一點地回顧一下為什么以及如何改變它:
根據新的 ECMA 編碼標準將所有var更改為const 。
您可以堆疊方法,而不是:
var firstChar = name.slice(0, 1);var upperCaseChar = firstChar.toUpperCase();
您可以像這樣簡單地在一行中執行此操作:const firstChar = name.slice(0, 1).toUpperCase();
- 去查看The MDN WEBDOCS for If...else statements and control,以便更好地理解如何撰寫
if...else包含多個條件的陳述句。
uj5u.com熱心網友回復:
只需將您的其他 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.")
}
}
uj5u.com熱心網友回復:
您可以使用停止功能在第一個 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477324.html
標籤:javascript if 语句
上一篇:使用python變數從使用sqlalchemy的訪問表中讀取特定行
下一篇:在下一行標記有結果和沒有結果的行
