ì´ë¤ íë¡ê·¸ëë° ì¸ì´ë ì½ëë ìì¬ ê²°ì ì ë´ë¦¬ê³ ì ë ¥ ë´ì©ì ë°ë¼ ìì ì ìíí´ì¼ í©ëë¤. ì를 ë¤ì´ ê²ììì íë ì´ì´ì ìëª ìì¹ê° 0ì´ë©´ ê²ìì´ ì¢ ë£ë©ëë¤. ë ì¨ ì±ììë ì침ì í´ê° ë¬ ê·¸ë¦¼ì ë³´ì¬ì£¼ê³ ë°¤ìë ë¬ê³¼ ë³ì ë³´ì¬ì¤ëë¤. ì´ ë¬¸ìììë JavaScriptìì ì¡°ê±´ë¬¸ì´ ìëíë ë°©ë²ì ì´í´ ë³´ê² ìµëë¤.
íìí ì¬ì ì§ì: 기본ì ì¸ ì»´í¨í° íì© ë¥ë ¥, HTML, CSS, Javascript 첫걸ì 목í: JavaScriptìì 조건문ì ì¬ì©ë²ì ì´í´í©ëë¤. ì¬ë¬ë¶ì í ì¡°ê±´ë³´ë¤ ì°ìì ìì ì ììµëë¤..!ì¬ë(ê³¼ ë물)ì íì ê·¸ë¤ì ì¶ì ìí¥ì 미ì¹ë ê²°ì ì, ìì ê²(ê³¼ì를 íë 먹ìê¹? ëê° ë¨¹ìê¹?)ë¶í° í° ê²(ê³ í¥ì 머물면ì ìë²ì§ì ëì¥ìì ì¼í´ì¼ í ê¹? ìëë©´ ì²ì²´ë¬¼ë¦¬íì ê³µë¶íë¬ ë¯¸êµì¼ë¡ ì íì ê°ê¹?)ê¹ì§ ë´ë¦½ëë¤.
조건문ì ì°ë¦¬ê°, ë°ëì ë´ë ¤ì ¸ì¼ íë ì íììë¶í° (ì를 ë¤ìë©´, "ì¿ í¤ í ê° ëë ë ê°"), ê·¸ ì íë¤ì ê²°ê³¼ê¹ì§ (ìë§ë "ì¿ í¤ í ê°ë¥¼ 먹ëë¤" ì ê²°ê³¼ë "ì¬ì í ë°°ê° ê³ íë¤" ì¼ì§ë ëª¨ë¥´ê³ , "ì¿ í¤ ë ê°ë¥¼ 먹ëë¤" ì ê²°ê³¼ë "ë°°ë¶ë¥´ë¤, ê·¸ë¬ë ìë§ê° ì¿ í¤ë¥¼ ë¤ ë¨¹ìë¤ê³ ë를 ì¼ë¨ì¹ ê²ì´ë¤" ì¼ì§ë ëª¨ë¦ ëë¤), ê·¸ë¬í ìì¬ ê²°ì ì JavaScriptìì ííí ì ìê² í©ëë¤.
if ... else 문ì¬ë¬ë¶ì´ JavaScriptìì ì¸ ë¨ì°ì½ ê°ì¥ ì¼ë°ì ì¸ ííì 조건문ì ì´í´ë´
ìë¤ â ë³ë³ì°®ì if ... else
문ì
ëë¤.
기본 if...else
문ë²ì ìì¬ ì½ë(pseudocode)ë¡ ë¤ìê³¼ ê°ì´ ë³´ì
ëë¤:
if (ì¡°ê±´) { ë§ì½ ì¡°ê±´(condition)ì´ ì°¸ì¼ ê²½ì° ì¤íí ì½ë } else { ëì ì¤íí ë¤ë¥¸ ì½ë }
ì를 ì´í´ë³´ë©´:
if
ë¤ì ê´í¸ê° ìµëë¤.true
ë false
를 리í´í©ëë¤.true
를 ë°ííë ê²½ì°ìë§ ì¤íë©ëë¤.else
.true
ê° ìë ê²½ì°ìë§ ì¤íë©ëë¤. â ëë ë¤ë¥¸ ë§ë¡ íìë©´, ì¡°ê±´ì´ false
ì¸ ê²½ì°ìë§ ì¤íë©ëë¤.ì´ ì½ëë ì¬ëì´ ì½ì ì ììµëë¤. â ì´ê²ì "ë§ì½ ì¡°ê±´ì´ true
ë©´, ì½ë A를 ì¤ííê³ , ìëë©´ ì½ë B를 ì¤ííë¤." ë¼ê³ ë§í©ëë¤.
ë°ëì else
ì ë ë²ì§¸ ì¤ê´í¸ë¥¼ í¬í¨íì§ ììë ëë¤ë ê²ì 주목í´ì¼ í©ëë¤. â ë¤ìì ëí ìë²½í ì½ëì
ëë¤:
if (ì¡°ê±´) { ë§ì½ ì¡°ê±´(condition)ì´ ì°¸ì¼ ê²½ì° ì¤íí ì½ë } ì¤íí ë¤ë¥¸ ì½ë
íì§ë§, ì¬ê¸°ì ì¡°ì¬í´ì¼ í ì â ìì ê²½ì°, ì½ëì ë ë²ì§¸ ë¸ë¡ì 조건문ì ìí´ì ì ì´ëì§ ììì, ì¡°ê±´ì´ true
ë false
를 리í´íë ê²ì ê´ê³ìì´ íì ëìí©ëë¤. ì´ê²ì´ ë°ëì ëì ê²ì ìëì§ë§, ì¬ë¬ë¶ì´ ìíë ê²ì´ ìë ì§ë 모ë¦
ëë¤. â ì¢
ì¢
ì¬ë¬ë¶ì ë ë¤ê° ìëë¼, ì½ëì í ë¸ë ëë ë¤ë¥¸ ë¸ëì ì¤íí기를 ìí©ëë¤.
ë§ì§ë§ì¼ë¡, ì¬ë¬ë¶ì ëëë¡ ë¤ìê³¼ ê°ì´ ì§§ì ì¤íì¼ë¡ ì¤ê´í¸ ìì´ ì°ì¬ì§ if...else
를 ë³´ìì ì§ë 모ë¦
ëë¤:
if (ì¡°ê±´) ë§ì½ ì¡°ê±´(condition)ì´ ì°¸ì¼ ê²½ì° ì¤íí ì½ë else ëì ì¤íí ë¤ë¥¸ ì½ë
ì´ê²ì ìë²½íê² ì í¨í ì½ëì´ì§ë§, ì¬ì©íë ê²ì ì¶ì²íì§ ììµëë¤ â ë§ì½ ì½ë ë¸ë¡ì ë²ì를 ì í기 ìí´ ì¤ê´í¸ë¥¼ ì¬ì©íê³ , ë¤ìì ì¤ê³¼ ë¤ì¬ì°ê¸°ë¥¼ ì¬ì©íë¤ë©´ ì½ë를 ì½ê³ 무ìì´ ì§íëëì§ ììë´ë ê²ì´ í¨ì¬ ì½ìµëë¤.
ì¤ì ìì문ë²ì ì ì´í´í기 ìí´ì ì¤ì ìì를 ììë´ ìë¤. ì´ë¨¸ëë ìë²ì§ê° ìì´ìê² ì§ìì¼ì ëìë¬ë¼ê³ ìì²íë¤ê³ ììí´ ë´ ìë¤. ë¶ëª¨ëê»ì "ì°ë¦¬ ì 기, ë§ì½ì ì¼í ê°ë 걸 ëì주면, ë¤ê° ìíë ì¥ëê°ì ì´ ì¬ì ê° ëëë¡ ì©ëì ë ì¤ê²." ë¼ê³ ë§ìíì¤ì§ë ëª¨ë¦ ëë¤. JavaScriptìì, ì°ë¦¬ë ì´ê²ì ë¤ìê³¼ ê°ì´ ííí ì ììµëë¤:
let shoppingDone = false;
let childsAllowance;
if (shoppingDone === true) {
childsAllowance = 10;
} else {
childsAllowance = 5;
}
ì ì½ëë íì false
를 리í´íë shoppingDone
ë³ì를 ê²°ê³¼ë¡ ë³ëë°, ì´ë ì°ë¦¬ì ë¶ìí ìì´ìê² ì¤ë§ì ìê²¨ì£¼ê² ì£ . ìì´ê° ë¶ëª¨ëê³¼ í¨ê» ì¼íì ê°ë¤ë©´ ë¶ëª¨ëì´ shoppingDone
ë³ì를 true
ë¡ ì¤ì íë ë©ì»¤ëì¦ì ì ê³µíë ê²ì ì°ë¦¬ìê² ë¬ë ¸ìµëë¤.
ì°¸ê³ : GitHubìì ìì를 ë ë³¼ ì ììµëë¤. ì´ ìì ì ìì± ë²ì on GitHub (also see it running live.)
else ifì§ë ììììë ë ê°ì§ ì í, ëë ê²°ê³¼ê° ììì£ â íì§ë§ ì°ë¦¬ê° ë ê°ì§ë³´ë¤ ë ë§ì ê²ì ìíë¤ë©´?
ì¶ê°ë¡ ì í/결과를 if...else
ì ì°ê²°íë ë°©ë²ì´ ììµëë¤. â else if
를 ì¬ì©í´ì ë§ì
ëë¤. ê° ì¶ê° ì íì if() { ... }
ì else { ... }
ì¬ì´ì ë£ì ì¶ê°ì ì¸ ë¸ë¡ì íìë¡ í©ëë¤. ê°ë¨í ë ì¨ ìë³´ ì´í리ì¼ì´ì
ì ì¼ë¶ê° ë ì ìë ë¤ìì ì¢ ë ê´ë ¨ë ìì를 íì¸íì¸ì:
<label for="weather">Select the weather type today: </label>
<select id="weather">
<option value="">--Make a choice--</option>
<option value="sunny">Sunny</option>
<option value="rainy">Rainy</option>
<option value="snowing">Snowing</option>
<option value="overcast">Overcast</option>
</select>
<p></p>
const select = document.querySelector("select");
const para = document.querySelector("p");
select.addEventListener("change", setWeather);
function setWeather() {
const choice = select.value;
if (choice === "sunny") {
para.textContent =
"It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.";
} else if (choice === "rainy") {
para.textContent =
"Rain is falling outside; take a rain coat and a brolly, and don't stay out for too long.";
} else if (choice === "snowing") {
para.textContent =
"The snow is coming down â it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.";
} else if (choice === "overcast") {
para.textContent =
"It isn't raining, but the sky is grey and gloomy; it could turn any minute, so take a rain coat just in case.";
} else {
para.textContent = "";
}
}
<select>
ì리먼í¸ë¥¼ ì¬ì©íì¬ ë¤ë¥¸ ë ì¨ ì íê³¼ ê°ë¨í 문ë¨ì ë§ë¤ ì ììµëë¤.<select>
ì <p>
ìì를 모ë ì ì¥íê³ ìê³ , ê°ì´ ë³í ë setWeather()
í¨ìê° ëìíëë¡ <select>
ììì ì´ë²¤í¸ 리ì¤ë를 ì¶ê°íìµëë¤.<select>
ìììì ì íë íì¬ ê°ì choice
ë¼ë ë³ìì 먼ì ì¤ì í©ëë¤. ê·¸ë° ë¤ì choice
ê°ì ë°ë¼ ë¬¸ë¨ ìì ë¤ë¥¸ í
ì¤í¸ë¥¼ íìí기 ìí´ ì¡°ê±´ë¬¸ì ì¬ì©í©ëë¤. if() {...} block
ìì í
ì¤í¸ë 첫 ë²ì§¸ë¥¼ ì ì¸íê³ , else if() {...}
ìì 모ë ì¡°ê±´ë¤ì´ ì´ë»ê² í
ì¤í¸ëëì§ì ì ìíì¸ì.else {...}
ìì, ê°ì¥ ë§ì§ë§ ì íì 기본ì ì¼ë¡ "ìµíì ìë¨" ìµì
ì
ëë¤ â true
ì¸ ì¡°ê±´ì´ ìì¼ë©´ ê·¸ ìì ì½ëê° ì¤íë©ëë¤. ì´ ê²½ì° ì무ê²ë ì íëì§ ìì¼ë©´, ì를 ë¤ì´, ì¬ì©ìê° ì²ìì íìí "--Make a choice--" íë ì´ì¤íë(placeholder) ìµì
ì ë¤ì ì ííê¸°ë¡ íë¤ë©´, 문ë¨ì í
ì¤í¸ë¥¼ ë¹ì°ë ìí ì í©ëë¤.ì°¸ê³ : ëí ì´ ìì 를 GitHubìì ì°¾ì ì ììµëë¤. (see it running live on there also.)
ë¹êµ ì°ì°ìì ëí ë©ëª¨ë¹êµ ì°ì°ìë ì°ë¦¬ì 조건문 ìì ì¡°ê±´ì í ì¤í¸íëë° ì¬ì©ë©ëë¤. ì°ë¦¬ë 먼ì ì´ì ì JavaScriptì 기본ì ì¸ ì°ì° - ì«ìì ì°ì°ì 문ììì ë¹êµ ì°ì°ì를 ë´¤ìµëë¤. ì°ë¦¬ì ì íë¤ì ë¤ìê³¼ ê°ìµëë¤:
===
ì !==
â í ê°ì´ ë¤ë¥¸ ê°ê³¼ ê°ê±°ë ë¤ë¥¸ì§ í
ì¤í¸íë¤.<
ì >
â í ê°ì´ ë¤ë¥¸ ê°ë³´ë¤ ììì§ í°ì§ í
ì¤í¸íë¤.<=
ì >=
â í ê°ì´ ë¤ë¥¸ ê°ë³´ë¤ ìê±°ë ê°ìì§, í¬ê±°ë ê°ìì§ í
ì¤í¸íë¤ì°¸ê³ : ì´ê²ë¤ì ëí 기ìµì ëì´ë¦¬ê¸°ë¥¼ ìíì ë¤ë©´ ì ë§í¬ì ìë ê²ë¤ì ë¤ì ë³´ì¸ì.
ëª ë²ì´ê³ ë¤ì ë§ë boolean(true
/false
)ê°ì í
ì¤í¸íë ê²ê³¼ ì¼ë°ì ì¸ í¨í´ì ëí í¹ë³í ì¸ê¸ì íê³ ì¶ììµëë¤. ì´ë í ê°ë¤ì´ë false
, undefined
, null
, 0
, NaN
ì´ë ë¹ ë¬¸ìì´(''
)ì´ ìë ê°ì 조건문ì¼ë¡ í
ì¤í¸ëìì ë, ì¤ì ë¡ë true
를 리í´íë¯ë¡, ë³ìê° ì°¸ì¸ì§ í¹ì ê°ì´ ì¡´ì¬íëì§ë¥¼ í
ì¤í¸í기 ìí´ ë³ì ì´ë¦ ê·¸ ì체를 ì¬ì©í ì ììµëë¤. ì를 ë¤ì´:
let cheese = "Cheddar";
if (cheese) {
console.log("Yay! Cheese available for making cheese on toast.");
} else {
console.log("No cheese on toast for you today.");
}
ê·¸ë¦¬ê³ , ë¶ëª¨ëì ìí´ ì§ìì¼ì íë ìì´ì ëí ì´ì ììë¡ ëìê°ìë©´, ë¤ìê³¼ ê°ì´ ìì±í ì ììµëë¤.
let shoppingDone = false;
let childsAllowance;
if (shoppingDone) {
// ëª
ìì ì¼ë¡ '=== true'를 ëª
ìí íìê° ììµëë¤
childsAllowance = 10;
} else {
childsAllowance = 5;
}
if ... else ì¤ì²©
if...else
문ì ë ë¤ë¥¸ 문 ìì (ì¤ì²©í기 ìíì¬) ë£ë ê²ì ìë²½íê² ê°ë¥í©ëë¤. ì를 ë¤ì´, ì¨ëê° ë¬´ìì¸ì§ì ë°ë¼ ì¶ê°ì ì¸ ì íì ìµì
ì ë³´ì¬ì£¼ê¸° ìí´ ì°ë¦¬ì ë ì¨ ìë³´ ì´í리ì¼ì´ì
ì ì
ë°ì´í¸ í ì ììµëë¤.
if (choice === "sunny") {
if (temperature < 86) {
para.textContent =
"It is " +
temperature +
" degrees outside â nice and sunny. Let's go out to the beach, or the park, and get an ice cream.";
} else if (temperature >= 86) {
para.textContent =
"It is " +
temperature +
" degrees outside â REALLY HOT! If you want to go outside, make sure to put some suncream on.";
}
}
ë¹ë¡ ì½ëê° ëª¨ë ëìíëë¼ë, ê° if...else
문ì ë¤ë¥¸ 문과 ìì í ë
립ì ì¼ë¡ ëìí©ëë¤.
ë§ì½ ì¤ì²©ë if...else
문ì ìì± ìì´ ë¤ìí ì¡°ê±´ì í
ì¤í¸í길 ìíë¤ë©´ ë
¼ë¦¬ ì°ì°ìê° ì¬ë¬ë¶ì ëìì¤ ì ììµëë¤. ì¡°ê±´ ë´ìì ì¬ì©ë ë, ì²ìì ë ê°ì§ë ë¤ìì í©ëë¤:
&&
â AND; ì ì²´ ííì(expression)ì´ true
를 리í´í기 ìí´ ë ê° í¹ì ê·¸ ì´ìì ííìì´ ê°ë³ì ì¼ë¡ true
ë¡ íê°ëëë¡ ê·¸ ìë¤ì ê°ì´ ì°ê²°í ì ìê² í©ëë¤.||
â OR; ì ì²´ ííìì´ true
를 리í´í기 ìí´ íë í¹ì ê·¸ ì´ìì ííìì´ ê°ë³ì ì¼ë¡ true
ë¡ íê°ëëë¡ ë ê° í¹ì ê·¸ ì´ìì ìë¤ì ê°ì´ ì°ê²°í ì ìê² í©ëë¤.AND ìì를 ìí´ì ìì ìì ì½ë를 ë¤ìê³¼ ê°ì´ ì¬ìì±í ì ììµëë¤.
if (choice === "sunny" && temperature < 86) {
para.textContent =
"It is " +
temperature +
" degrees outside â nice and sunny. Let's go out to the beach, or the park, and get an ice cream.";
} else if (choice === "sunny" && temperature >= 86) {
para.textContent =
"It is " +
temperature +
" degrees outside â REALLY HOT! If you want to go outside, make sure to put some suncream on.";
}
ì ìììì, 첫 ë²ì§¸ ì½ë ë¸ë¡ì choice === 'sunny'
ì temperature < 86
ê° ì¤ì§ true
를 리í´í´ì¼ë§ ì¤íë ê²ì
ëë¤.
ë¹ ë¥´ê² OR ìì를 ë´ ìë¤.
if (iceCreamVanOutside || houseStatus === "on fire") {
console.log("You should leave the house quickly.");
} else {
console.log("Probably should just stay in then.");
}
ë
¼ë¦¬ ì°ì°ìì ë§ì§ë§ ì íì¸ !
ì°ì°ìë¡ ííëë NOTì ííìì ë¶ì (negate)í기 ìí´ ì¬ì©ë ì ììµëë¤. ì OR ììì í¨ê» ë´
ìë¤.
if (!(iceCreamVanOutside || houseStatus === "on fire")) {
console.log("Probably should just stay in then.");
} else {
console.log("You should leave the house quickly.");
}
ì ìììì, OR ë¬¸ì´ true
를 리í´íë¤ë©´, NOT ì°ì°ìë ì ì²´ ííìì´ false
를 리í´íëë¡ ë¶ì í ê²ì
ëë¤.
ì´ë¤ 구조ë ì§, ì¬ë¬ë¶ì´ ìíë ë§í¼ ë§ì ë ¼ë¦¬ 문(statement)ì ê²°í©í ì ììµëë¤. ë¤ì ììë ì¤ì§ ë OR 문 모ëê° true를 리í´íë©´ ë´ë¶ì ì½ë를 ì¤ííëë°, ì´ë ì ì²´ì AND ë¬¸ì´ true를 리í´í ê²ìì ì미í©ëë¤.
if ((x === 5 || y > 3 || z <= 10) && (loggedIn || userName === "Steve")) {
// run the code
}
조건문ìì ë
¼ë¦¬ì OR ì°ì°ì를 ì¬ì©í ëì ì¼ë°ì ì¸ ì¤ìë ì¬ë¬ë¶ì´ ê²ì¬íë ê°ì ë³ì를 í ë² ëª
ìí기(state)를 ìëíê³ , ê·¸ë¦¬ê³ true를 리í´í ì ìë ê°ë¤ì 목ë¡ì, ||
(OR) ì°ì°ìì ìí´ ë¶ë¦¬íë©° ì ê³µíë ê²ì
ëë¤. ì를 ë¤ìë©´:
if (x === 5 || 7 || 10 || 20) {
// run my code
}
ì´ ê²½ì°ì if(...)
ë´ë¶ ì¡°ê±´ì 7(ëë ë¤ë¥¸ 0ì´ ìë ê°)ì´ íì trueë¡ íê°ëë¯ë¡, íì trueë¡ íê°ë ê²ì
ëë¤. ì´ ì¡°ê±´ì ì¤ì ë¡ë "ë§ì½ xê° 5ì ê°ê±°ë, 7ì´ trueë©´ â ì´ê²ì íì trueë¤"ë¼ê³ ë¶ëª
íê² ë§íê³ ììµëë¤. ì´ê²ì ë
¼ë¦¬ì ì¼ë¡ ì°ë¦¬ê° ìíë ê²ì´ ìëëë¤! ì´ë¥¼ ëìíê² í기 ìí´ ì°ë¦¬ë ê° OR ì°ì°ìì ì 측ìì ìì í í
ì¤í¸ë¥¼ ëª
ìí´ì¼ í©ëë¤.
if (x === 5 || x === 7 || x === 10 || x === 20) {
// run my code
}
switch 문
if...else
문ì 조건문 ì½ëê° ì ê°ë¥íê² íë ì¼ì íì§ë§, ë¨ì ì´ ìì§ë ììµëë¤. ê·¸ 문ì ë ê°ì§ ì íì ê°ì§ê³ ìê³ , ê°ê°ì ì¤íë í©ë¦¬ì ì¸ ìì ì½ëê° íìíê³ , ë°/ëë ê·¸ ì¡°ê±´ì´ ë³µì¡í (ì를 ë¤ìë©´, ë¤ìì ë
¼ë¦¬ ì°ì°ì) ê²½ì°ì ì£¼ë¡ ì ì©í©ëë¤. ì¬ë¬ë¶ì´ ë¨ì§ ì´ë¤ ê°ì ì íì ë³ì를 ì¤ì íê±°ë ì¡°ê±´ì ë¬ë¦° í¹ì í 문(statement)ì ì¶ë ¥í기를 ìíë ê²½ì°ì, ê·¸ 구문(syntax)ì ë¤ì ë²ê±°ë¡ì¸ ì ìëë°, í¹í ì¬ë¬ë¶ì´ ë¤ìì ì íì ê°ì§ê³ ìë ê²½ì°ì ê·¸ë ìµëë¤.
ê·¸ë¬í ê²½ì°ì switch
문ì ì¬ë¬ë¶ì ì¹êµ¬ì
ëë¤. ì´ë ì
ë ¥ì¼ë¡ íëì ííì/ê°ì ë°ê³ , ê°ê³¼ ì¼ì¹íë íë를 ì°¾ì ëê¹ì§ ì¬ë¬ í목ì ì´í´ë³´ê³ ê·¸ì ë§ë ì½ë를 ì¤íí©ëë¤. ì¬ë¬ë¶ìê² ìì´ëì´ë¥¼ 주기 ìí´, ì¬ê¸° ëªëª ë§ì ìì¬ ì½ëê° ììµëë¤:
switch (expression) { case choice1: run this code break; case choice2: run this code instead break; // ìíë ë§í¼ ë§ì case를 í¬í¨íììì¤ default: actually, just run this code }
ì¬ê¸°ìì:
switch
.case
ë ì½ë¡ ì´ ë¤ì ìµëë¤.break
문ì ë¤ì ì¸ë¯¸ì½ë¡ ì´ ìµëë¤. ì´ì ì ì íì´ ííìì´ë ê°ê³¼ ì¼ì¹íë¤ë©´ ë¸ë¼ì°ì ë í´ë¹ ì½ë ë¸ë¡ìì ì¤íì ë©ì¶ê³ , switch 문 ìëì ìë ì½ëë¡ ì´ëí©ëë¤.default
ë ê·¸ ë¤ì ì íì´ ìë¤ë ê²ì ì ì¸íê³ ì¼ì´ì¤ë¤ ì¤ íëì ìì í ê°ì ì½ë í¨í´ì¸ë° (3ë²â5ë²), ì´ ë¸ë¡ ì´íìë ì¤íí ì´ë¤ ì½ëë ìì¼ë¯ë¡ break
ë¬¸ì´ íìíì§ ììµëë¤. ì¼ì¹íë íëª©ì´ ìì¼ë©´ ì¤íëë 기본 ìµì
ì
ëë¤.A switch exampleì°¸ê³ :
default
를 ë°ëì í¬í¨íì§ ììë ë©ëë¤ â ë§ì½ ííìì´ ë¯¸ì§ì ê°ê³¼ ê°ê² ëì´ë²ë¦´ ì ìë ê²½ì°ê° ìë¤ë©´ ì¬ë¬ë¶ì ìì íê² ì´ê²ì ìëµí ì ììµëë¤. ê·¸ë¬ë, ê·¸ ê²½ì°ê° ìë¤ë©´, ì¬ë¬ë¶ì 미ì§ì ê²½ì°ë¥¼ ë¤ë£¨ê¸° ìí´ ì´ê²ì í¬í¨í íìê° ììµëë¤.
ì¤ì ìì 를 í´ ë´ ìë¤. switch문ì ëì ì¬ì©í´ ì¼ê¸°ìë³´ ì í리ì¼ì´ì ì ì¬ìì±í ê²ì ëë¤.
<label for="weather">Select the weather type today: </label>
<select id="weather">
<option value="">--Make a choice--</option>
<option value="sunny">Sunny</option>
<option value="rainy">Rainy</option>
<option value="snowing">Snowing</option>
<option value="overcast">Overcast</option>
</select>
<p></p>
const select = document.querySelector("select");
const para = document.querySelector("p");
select.addEventListener("change", setWeather);
function setWeather() {
const choice = select.value;
switch (choice) {
case "sunny":
para.textContent =
"It is nice and sunny outside today. Wear shorts! Go to the beach, or the park, and get an ice cream.";
break;
case "rainy":
para.textContent =
"Rain is falling outside; take a rain coat and a brolly, and don't stay out for too long.";
break;
case "snowing":
para.textContent =
"The snow is coming down â it is freezing! Best to stay in with a cup of hot chocolate, or go build a snowman.";
break;
case "overcast":
para.textContent =
"It isn't raining, but the sky is grey and gloomy; it could turn any minute, so take a rain coat just in case.";
break;
default:
para.textContent = "";
}
}
ì°¸ê³ : ëí ì´ ìì 를 GitHubìì ì°¾ì ì ììµëë¤. (see it running live on there also.)
ì¼íì°ì°ìë¤ë¥¸ ìì ë¡ ë¤ì´ê°ê¸° ì ì ìê°íê³ ì¶ì ë§ì§ë§ êµ¬ë¬¸ì´ ììµëë¤. ì¼í(ì¡°ê±´)ì°ì°ì(ternary or conditional operator)ë ì¡°ê±´ì í
ì¤í¸íê³ ë§ì½ ì¡°ê±´ì´ true
ë¼ë©´ íëì ê°/ííìì 리í´íê³ , ë§ì½ false
ë¼ë©´ ë¤ë¥¸ ê°/ííìì 리í´íë 구문ì
ëë¤ â ì´ê²ì ì´ë¤ ìí©ì ì ì©í ì ìì¼ë©°, ë§ì½ true
/false
ì¡°ê±´ ì¬ì´ë¥¼ íµí´ ì íëë ë ì íì ê°ì§ê³ ìë¤ë©´ if...else
ë¸ë¡ë³´ë¤ ì½ë를 í¨ì¬ ì ê² ì¬ì©í ì ììµëë¤. ì´ ìì¬ ì½ëë ìëì ê°ìµëë¤:
( condition ) ? run this code : run this code instead
ê·¸ë¬ë©´ ê°ë¨í ì를 ë´ ìë¤:
let greeting = isBirthday
? "Happy birthday Mrs. Smith â we hope you have a great day!"
: "Good morning Mrs. Smith.";
isBirthday
ë¼ë ë³ìëª
ì´ ì¬ê¸° ììµëë¤ â ë§ì½ ì´ê²ì´ true
ë¼ë©´, ì°ë¦¬ë ê²ì¤í¸ìê² ìì¼ ì¶í ë©ìì§ë¥¼ ë³´ë
ëë¤; ë§ì½ ìëë¼ë©´, ì°ë¦¬ë ê·¸ë
ìê² ì¼ë°ì ì¸ ì¸ì¬ë¥¼ ë³´ë
ëë¤.
ì¼íì°ì°ìë¡ ë¨ì§ ë³ì ê°ë§ì ì í íìê° ììµëë¤; 무ìì´ë ì§ ì¢ìíë í¨ìë ì½ë를 ì¤íí ì ììµëë¤. ì´ ìì ë ì¼íì°ì°ì를 ì¬ì©íì¬ ì¬ì´í¸ì ì¤íì¼ë§ì´ ì ì©ëë ê°ë¨í í ë§ ì í기를 ë³´ì¬ì¤ëë¤.
<label for="theme">Select theme: </label>
<select id="theme">
<option value="white">White</option>
<option value="black">Black</option>
</select>
<h1>This is my website</h1>
const select = document.querySelector("select");
const html = document.querySelector("html");
document.body.style.padding = "10px";
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}
select.onchange = function () {
select.value === "black"
? update("black", "white")
: update("white", "black");
};
ì¬ê¸°ìë í
ë§(ê²ì ëë íì)를 ê³ ë¥´ê¸° ìí '<select>
' ììê° ìê³ , ì¬ê¸°ì ëíì¬ ì¹ì¬ì´í¸ ì 목ì ë³´ì¬ì£¼ë ê°ë¨í '<h1>
" ììê° ììµëë¤. ì°ë¦¬ë ëí update()
ë¼ë í¨ì를 ê°ì§ê³ ìëë°, ì´ í¨ìë ë ììì 매ê°ë³ì(parameter)(ì
ë ¥)ë¡ ì·¨í©ëë¤. ì¹ì¬ì´í¸ì ë°°ê²½ìì´ ì²« ë²ì§¸ ì ê³µë ììì¼ë¡ ì§ì ëê³ , í
ì¤í¸ ììì´ ë ë²ì§¸ ì ê³µë ììì¼ë¡ ì í´ì§ëë¤.
ëì¼ë¡, ì°ë¦¬ë ëí ì¼íì°ì°ì를 í¬í¨íê³ ìë í¨ìì ì¤íì ì ê³µíë 'onchange' ì´ë²¤í¸ 리ì¤ë를 ê°ì§ê³ ììµëë¤. ì´ê²ì í
ì¤í¸ ì¡°ê±´ì¼ë¡ ììí©ëë¤ â select.value === 'black'
. ë§ì½ ì´ê²ì´ true
를 리í´íë©´, ì°ë¦¬ë ê²ì ê³¼ íìì 매ê°ë³ì를 ì§ëê³ ìë update()
í¨ì를 ì¤ííëë°, ì´ë ë°°ê²½ìì ê²ì ì¼ë¡ í
ì¤í¸ ìì íìì¼ë¡ ëë 결과를 ì미í©ëë¤. ë§ì½ ì´ê²ì´ false
를 리í´íë©´, ì°ë¦¬ë íìê³¼ ê²ì ì 매ê°ë³ì를 ì§ëê³ ìë update()
í¨ì를 ì¤ííëë°, ì´ë ì¬ì´í¸ì ììì´ ë°ì ëë¤ë ê²ì ì미í©ëë¤.
ì°¸ê³ : ëí ì´ ìì 를 GitHubìì ì°¾ì ì ììµëë¤. (see it running live on there also.)
ì§ì í´ë³´ê¸°: ê°ë¨í ë¬ë ¥ ë§ë¤ê¸°ì´ ìì ììë ê°ë¨í ë¬ë ¥ ì´í리ì¼ì´ì ì ë§ë¤ì´ ë³¼ ê²ì ëë¤. ì½ëìë ë¤ìê³¼ ê°ì ê²ë¤ì´ ë¤ì´ ììµëë¤.
<select>
ìì.<select>
ë©ë´ìì ì íë ê°ì´ ë³ê²½ëìì ë를 íì§íë onchange
ì´ë²¤í¸ í¸ë¤ë¬.<h1>
ììì ì¬ë°ë¥¸ ë¬ì íìíê³ ë¬ë ¥ì ìì±íë createCalendar()
í¨ì.onchange
í¸ë¤ë¬ í¨ìë´ì 조건문ì ìì±í´ì¼ í©ëë¤. ìì¹ë // ADD CONDITIONAL HERE
주ì ë°ë¡ ìëì
ëë¤. 조건문ì ë¤ìì ë§ì¡±í´ì¼ í©ëë¤:
choice
ë³ì ìì ì ì¥ëì´ ììµëë¤. ì´ê²ì ê°ì´ ë³ê²½ë ì´íì <select>
ìì ê°ì¼ ê²ì
ëë¤. ì를 ë¤ìë©´ "January")days
ë³ì를 ì íë ë¬ì ì¼ìì ëì¼íê² ì¤ì í기. ì´ê²ì í기 ìí´ìë 1ë
ì ê° ë¬ì ì¼ì를 ì°¾ìë´ì¼ë§ í ê²ì
ëë¤. ì´ ìì ì 목ì ì ìí´ ì¤ë
ì 무ìí ì ììµëë¤.íí¸:
ë§ì½ ì¤ì를 íëë¼ë 'Reset' ë²í¼ì¼ë¡ ì´ê¸°í í ì ììµëë¤. ì ë§ë¡ í´ëµì ëª¨ë¥´ê² ë¤ë©´, "Show solution" ì¼ë¡ í´ê²° ë°©ë²ì íì¸íì¸ì.
<h2>Live output</h2>
<div class="output" style="height: 500px;overflow: auto;">
<label for="month">Select month: </label>
<select id="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<h1></h1>
<ul></ul>
</div>
<h2>Editable code</h2>
<p class="a11y-label">
Press Esc to move focus away from the code area (Tab inserts a tab character).
</p>
<textarea id="code" class="playable-code" style="height: 400px;width: 95%">
const select = document.querySelector('select');
const list = document.querySelector('ul');
const h1 = document.querySelector('h1');
select.onchange = function() {
const choice = select.value;
// ADD CONDITIONAL HERE
createCalendar(days, choice);
}
function createCalendar(days, choice) {
list.innerHTML = '';
h1.textContent = choice;
for (let i = 1; i <= days; i++) {
const listItem = document.createElement('li');
listItem.textContent = i;
list.appendChild(listItem);
}
}
createCalendar(31,'January');
</textarea>
<div class="playable-buttons">
<input id="reset" type="button" value="Reset" />
<input id="solution" type="button" value="Show solution" />
</div>
.output * {
box-sizing: border-box;
}
.output ul {
padding-left: 0;
}
.output li {
display: block;
float: left;
width: 25%;
border: 2px solid white;
padding: 5px;
height: 40px;
background-color: #4a2db6;
color: white;
}
html {
font-family: sans-serif;
}
h2 {
font-size: 16px;
}
.a11y-label {
margin: 0;
text-align: right;
font-size: 0.7rem;
width: 98%;
}
body {
margin: 10px;
background: #f5f9fa;
}
const textarea = document.getElementById("code");
const reset = document.getElementById("reset");
const solution = document.getElementById("solution");
let code = textarea.value;
let userEntry = textarea.value;
function updateCode() {
eval(textarea.value);
}
reset.addEventListener("click", function () {
textarea.value = code;
userEntry = textarea.value;
solutionEntry = jsSolution;
solution.value = "Show solution";
updateCode();
});
solution.addEventListener("click", function () {
if (solution.value === "Show solution") {
textarea.value = solutionEntry;
solution.value = "Hide solution";
} else {
textarea.value = userEntry;
solution.value = "Show solution";
}
updateCode();
});
const jsSolution =
"const select = document.querySelector('select');\nconst list = document.querySelector('ul');\nconst h1 = document.querySelector('h1');\n\nselect.onchange = function() {\n const choice = select.value;\n let days = 31;\n if(choice === 'February') {\n days = 28;\n } else if(choice === 'April' || choice === 'June' || choice === 'September'|| choice === 'November') {\n days = 30;\n }\n\n createCalendar(days, choice);\n}\n\nfunction createCalendar(days, choice) {\n list.innerHTML = '';\n h1.textContent = choice;\n for(let i = 1; i <= days; i++) {\n const listItem = document.createElement('li');\n listItem.textContent = i;\n list.appendChild(listItem);\n }\n }\n\ncreateCalendar(31,'January');";
let solutionEntry = jsSolution;
textarea.addEventListener("input", updateCode);
window.addEventListener("load", updateCode);
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = function (e) {
if (e.keyCode === 9) {
e.preventDefault();
insertAtCaret("\t");
}
if (e.keyCode === 27) {
textarea.blur();
}
};
function insertAtCaret(text) {
const scrollPos = textarea.scrollTop;
let caretPos = textarea.selectionStart;
const front = textarea.value.substring(0, caretPos);
const back = textarea.value.substring(
textarea.selectionEnd,
textarea.value.length,
);
textarea.value = front + text + back;
caretPos = caretPos + text.length;
textarea.selectionStart = caretPos;
textarea.selectionEnd = caretPos;
textarea.focus();
textarea.scrollTop = scrollPos;
}
// Update the saved userCode every time the user updates the text area code
textarea.onkeyup = function () {
// We only want to save the state when the user code is being shown,
// not the solution, so that solution is not saved over the user code
if (solution.value === "Show solution") {
userEntry = textarea.value;
} else {
solutionEntry = textarea.value;
}
updateCode();
};
ì§ì í´ë³´ê¸°: ìê¹ ê³ ë¥´ê¸°
ì´ ìì ììë ììì ë´¤ë ì¼íì°ì°ì ìì ì ì¼íì°ì°ì를 ê°ë¨í ì¹ì¬ì´í¸ì ë ë§ì ì íì ì ì©í ì ìê² íë switch문ì¼ë¡ ì íí ê²ì
ëë¤. <select>
ì ë³´ì¸ì â ì´ë²ìë ì´ê²ì´ ë ê°ì í
ë§ ìµì
ì ê°ì§ê³ ìì§ ìê³ , ë¤ì¯ ê°ë¥¼ ê°ì§ê³ ìë ê²ì ë³´ì¤ ì ìì ê²ì
ëë¤. ì¬ë¬ë¶ì // ADD SWITCH STATEMENT
주ì ë°ë¡ ë°ì ì¶ê°í íìê° ììµëë¤:
choice
ë³ì를 ì´ê²ì ì
ë ¥ ííìì¼ë¡ ë°ìì¼ í©ëë¤.update()
í¨ìë ì¤íëì´ì¼ë§ íê³ , ë ê°ì ììì, 첫ë²ì§¸ë ë°°ê²½ìì¼ë¡, ëë²ì§¸ë í
ì¤í¸ ììì¼ë¡ ì ë¬í´ì¼ë§ í©ëë¤. ììê°ì 문ìì´ì´ë¯ë¡, ë°ì´íë¡ ê°ì¸ì ¸ì¼ í íìê° ìë¤ë ê²ì 기ìµíì¸ì.ë§ì½ ì¤ì를 íëë¼ë 'Reset' ë²í¼ì¼ë¡ ì´ê¸°í í ì ììµëë¤. ì ë§ë¡ í´ëµì ëª¨ë¥´ê² ë¤ë©´, "Show solution" ì¼ë¡ í´ê²° ë°©ë²ì íì¸íì¸ì.
<h2>Live output</h2>
<div class="output" style="height: 300px;">
<label for="theme">Select theme: </label>
<select id="theme">
<option value="white">White</option>
<option value="black">Black</option>
<option value="purple">Purple</option>
<option value="yellow">Yellow</option>
<option value="psychedelic">Psychedelic</option>
</select>
<h1>This is my website</h1>
</div>
<h2>Editable code</h2>
<p class="a11y-label">
Press Esc to move focus away from the code area (Tab inserts a tab character).
</p>
<textarea id="code" class="playable-code" style="height: 450px;width: 95%">
const select = document.querySelector('select');
const html = document.querySelector('.output');
select.onchange = function() {
const choice = select.value;
// ADD SWITCH STATEMENT
}
function update(bgColor, textColor) {
html.style.backgroundColor = bgColor;
html.style.color = textColor;
}</textarea
>
<div class="playable-buttons">
<input id="reset" type="button" value="Reset" />
<input id="solution" type="button" value="Show solution" />
</div>
html {
font-family: sans-serif;
}
h2 {
font-size: 16px;
}
.a11y-label {
margin: 0;
text-align: right;
font-size: 0.7rem;
width: 98%;
}
body {
margin: 10px;
background: #f5f9fa;
}
const textarea = document.getElementById("code");
const reset = document.getElementById("reset");
const solution = document.getElementById("solution");
let code = textarea.value;
let userEntry = textarea.value;
function updateCode() {
eval(textarea.value);
}
reset.addEventListener("click", function () {
textarea.value = code;
userEntry = textarea.value;
solutionEntry = jsSolution;
solution.value = "Show solution";
updateCode();
});
solution.addEventListener("click", function () {
if (solution.value === "Show solution") {
textarea.value = solutionEntry;
solution.value = "Hide solution";
} else {
textarea.value = userEntry;
solution.value = "Show solution";
}
updateCode();
});
const jsSolution =
"const select = document.querySelector('select');\nconst html = document.querySelector('.output');\n\nselect.onchange = function() {\n const choice = select.value;\n\n switch(choice) {\n case 'black':\n update('black','white');\n break;\n case 'white':\n update('white','black');\n break;\n case 'purple':\n update('purple','white');\n break;\n case 'yellow':\n update('yellow','darkgray');\n break;\n case 'psychedelic':\n update('lime','purple');\n break;\n }\n}\n\nfunction update(bgColor, textColor) {\n html.style.backgroundColor = bgColor;\n html.style.color = textColor;\n}";
let solutionEntry = jsSolution;
textarea.addEventListener("input", updateCode);
window.addEventListener("load", updateCode);
// stop tab key tabbing out of textarea and
// make it write a tab at the caret position instead
textarea.onkeydown = function (e) {
if (e.keyCode === 9) {
e.preventDefault();
insertAtCaret("\t");
}
if (e.keyCode === 27) {
textarea.blur();
}
};
function insertAtCaret(text) {
const scrollPos = textarea.scrollTop;
let caretPos = textarea.selectionStart;
const front = textarea.value.substring(0, caretPos);
const back = textarea.value.substring(
textarea.selectionEnd,
textarea.value.length,
);
textarea.value = front + text + back;
caretPos = caretPos + text.length;
textarea.selectionStart = caretPos;
textarea.selectionEnd = caretPos;
textarea.focus();
textarea.scrollTop = scrollPos;
}
// Update the saved userCode every time the user updates the text area code
textarea.onkeyup = function () {
// We only want to save the state when the user code is being shown,
// not the solution, so that solution is not saved over the user code
if (solution.value === "Show solution") {
userEntry = textarea.value;
} else {
solutionEntry = textarea.value;
}
updateCode();
};
ì¤ë ¥ì íê°í´ ë³´ì¸ì!
ì´ ë¬¸ì를 ëê¹ì§ ì½ì¼ì ¨ì§ë§, ì¤ìí ê²ë¤ì ì¬ì í 기ìµíê³ ê³ì ê°ì? ë¤ì 문ì를 ì½ê¸° ì ì ì´ ë¬¸ìì ë´ì©ì ì íìµíê³ ì´í´íì ¨ëì§ íì¸íì¤ ì ììµëë¤ â ì¤ë ¥ì íê°í´ ë³´ì¸ì: 조건문.
ê²°ë¡ê·¸ë¦¬ê³ ì´ê²ì´ ì¬ë¬ë¶ì´ ì§ê¸ JavaScriptìì ììì¼ í 조건문 구조ì ëí 모ë ê²ì ëë¤! ì ë ì¬ë¬ë¶ì´ ì´ ê°ë ë¤ì ì´í´íê³ ì´ ìì ë¤ì ì½ê² íµê³¼íì ê²ì´ë¼ê³ íì í©ëë¤; ë§ì½ ëë ì§ ì´í´íì§ ëª»í ê² ìë¤ë©´, ìì ë¡ê² ì´ ë¬¸ì를 ë¤ì ì½ê±°ë, 문ìí기ìì ëìì ìì²í´ ë³´ì¸ì.
ê°ì´ 보기RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4