#2.13 Conditionals
prompt();
์ฌ์ฉ์์๊ฒ ์ฐฝ์ ๋์ ๊ฐ์ ๋ฐ์์ค์ง๋ง, ๊ฐ์ ๋ฐ์์ฌ ๋ ๊น์ง ์ฝ๋์ ์คํ์ ๋ฉ์ถ๋ ์ค๋๋ ๋ฐฉ์์ผ๋ก css๋ฅผ ๋ฐ๊ฟ ์๋ ์๋ค.
typeof ๋ผ๋ ํค์๋๋ variable์ type์ ๋ณผ์ ์๋ ํค์๋์ธ๋ฐ, prompt()์์๋ ์ซ์๋ฅผ ์ ๋ ฅ๋ฐ๊ณ ์ด๋ฅผ typeof๋ก ์ถ๋ ฅํด๋ string์ด๋ผ๊ณ ์ ๋ ฅ๋๋ค. prompt์ type์ string์ด default์ด๊ธฐ ๋๋ฌธ์ด๋ค.
parseInt();
์ซ์๋ก ๊ตฌ์ฑ๋ string type์ number type์ผ๋ก ๋ณํ์์ผ์ค๋ค.
console.log(typeof "15", typeof parseInt("15"));
// string number
const age = parseInt(prompt("Please write your age"));
console.log(typeof age);
// ์ด ๋ prompt์ ๋ช
๋ฐฑํ string๊ฐ์ ์
๋ ฅํ๋ฉด ์ฝ์์๋ age์ type์ด NaN์ด๋ผ๊ณ ํ ๊ฒ์ด๋ค.
// ๊ทธ๋ฌ๋ typeof age๋ number๋ก ์ถ๋ ฅ๋๋ค. NaN์ด number๋ก ์ทจ๊ธ๋๋ ๋ฏํ๋ค..
// console.log(isNan(age));๋ true๊ฐ ๋ ๊ฒ.
// ๋ง์ฝ prompt ์์ 123dfad ๊ฐ์ด ์ซ์์ ๋ฌธ์์ด์ ์
๋ ฅํ๊ณ
// console.log(age);๋ฅผ ํ๋ฉด 123์ด ์ถ๋ ฅ๋๋ค.
// ์ด๋ parseInt๊ฐ ์ซ์๊ฐ ์๋ ๊ธ์๋ฅผ ๋ง๋๊ฒ ๋๋ฉด
// ๊ทธ ๊ธ์ ์ดํ๋ ๋ฌด์ํ๊ณ ํด๋น ์ง์ ๊น์ง์ ์ ์๊ฐ์ ๋ฆฌํดํด์ฃผ๊ธฐ ๋๋ฌธ์ด๋ค.
#2.14 Conditionals part Two
const age = parseInt(prompt("How old are you?"));
if (isNaN(age)) {
console.log("Please write a number.");
} else if (age < 18) {
console.log("You are too young.");
} else {
console.log("You can drink.");
}
์ฐ์ฐ์ | ๋ด์ฉ |
&& | ์กฐ๊ฑด๋ฌธ ๋ ๋ค true (AND) |
|| | ์กฐ๊ฑด๋ฌธ ๋ ์ค ํ๋๋ง true (OR) |
== | ๋๋ฑ ์ฐ์ฐ์ (๊ฐ๋ง ๊ฐ์ผ๋ฉด true) |
=== | ์ผ์น ์ฐ์ฐ์ (๊ฐ๊ณผ ๊ฐ์ ์ข ๋ฅ(data type)๊ฐ ๋ชจ๋ ๊ฐ์์ง ๋น๊ตํ์ฌ ๊ฐ์ผ๋ฉด true) |
== vs ===
- ๋ฐฐ์ดํ : ๋ฐฐ์ด์ ๋ฐ์ดํฐ ๊ฐ์ด ๊ฐ๋๋ผ๋ ๋ฐฐ์ด์ ํ ๋นํ ๋ ๊ฐ ๋ณ์๋ ๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ฃผ์๋ฅผ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์, ์ฃผ์๊ฐ ๋ค๋ฅด๋ฏ๋ก ๋ ๋ฐฐ์ด์ ๊ฐ์ง ์๋ค.
let a = [1, 2, 3];
let b = [1. 2. 3];
console.log(a==b); // false
console.log(a===b); // false
let x = {}
let y = {}
console.log(x==y); // false
console.log(x===y); // false
๋๋ฑ ์ฐ์ฐ์(==)๋ณด๋ค๋ ์ผ์น ์ฐ์ฐ์(===)๋ฅผ ํจ์ฌ ๊ถ์ฅํ๋ค.
#3.6 CSS in Javascript
current color = h1์ ํ์ฌ color
newColor : ๋ณ๊ฒฝ๋ ์์ ๊ฐ์ ๋ด๋ let ๋ณ์
// #1. event๋ฅผ ์ ์ฉ์ํฌ element๋ฅผ ์ฐพ์๋ผ
const h1 = document.querySelector(".hello:first-child h1");
// #3. ๊ทธ event์ ๋ฐ์ํด๋ผ
function handleTitleClick() {
const currentColor = h1.style.color;
let newColor; // ์๋ฌด๊ฒ๋ ์๋ ๋น let ๋ณ์
if (currentColor === "blue") {
newColor = "tomato";
} else {
newColor = "blue";
}
h1.style.color = newColor;
}
// #2. event๋ฅผ listenํด๋ผ
h1.addEventListener("click", handleTitleClick);
#3.7 CSS in Javascript part Two
์์ ์ฝ๋๋ ์ ์๋ํ๋ ์ฝ๋์ง๋ง, style์ ์ ํฉํ ๋๊ตฌ๋ css์ด๊ธฐ ๋๋ฌธ์ ์ฌ๋งํ ์คํ์ผ ์์ฑ์ css์, javascript์๋ cs์์ ์ค์ ํ ๊ฐ์ element์ ์ ๋ฌ๋ง ์ํค๊ณ ์ถ๋ค. ๋๋ฌธ์ html, css, js ๋ชจ๋ ์ผ๋ถ ์์ ํ๋ค.
index.html
<!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>Momentum</title>
<!-- link:css ์ ์๋์์ฑ -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="hello">
<h1 class="title">Hello</h1>
</div>
<script src="app.js"></script>
</body>
</html>
style.css
body {
background-color: beige;
}
h1 {
color: cornflowerblue;
transition: color 0.5s ease-in-out;
}
.title {
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
.clicked {
color: tomato;
}
app.js
const h1 = document.querySelector(".hello:first-child h1");
function handleTitleClick() {
const clickedClass = "clicked";
if (h1.className === clickedClass) {
h1.className = "";
} else {
h1.className = clickedClass;
}
}
h1.addEventListener("click", handleTitleClick);
๊ทธ๋ฌ๋ ์ ์ฝ๋๋ ๋ง์ฝ h1 ํ๊ทธ๋ฅผ ํด๋ฆญํ์ ๋, h1 ํ๊ทธ์ ๋ฏธ๋ฆฌ ์ค์ ํด์ค title ํด๋์ค๊ฐ ์ฌ๋ผ์ง๊ณ clicked ํด๋์ค๊ฐ ์์ ๋์ ํด๋์ค๊ฐ ์์ ๋์ ์ํ๋ง ๋ฐ๋ณต๋ ๊ฒ์ด๋ค.
#3.8 CSS in Javascript part Three
์ด ๋ฒ๊ทธ๋ฅผ ํด๊ฒฐํด์ฃผ๋ ๊ฒ์ด classList์ด๋ค. className์ ๋ชจ๋ ํด๋์ค๋ฅผ ๊ต์ฒดํด๋ฒ๋ฆฌ์ง๋ง, classList์์๋ class๋ค์ ๋ชฉ๋ก์ผ๋ก ์์ ํ ์ ์๊ฒ๋ ํ์ฉํด์ค๋ค.
classList์๋ ๋ง์ function์ด ์๋๋ฐ, ๊ทธ ์ค contains() function์ ๋ช ์๋ class๊ฐ html element์ class์ ํฌํจ๋์ด ์๋์ง ์๋ ค์ค๋ค. add์ remove๋ classList์์ ๋ช ์๋ class๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์ ๊ฑฐํ ์ ์๋ค.
์ด๋ฅผ ํ๋์ ํ์ธํ๊ธฐ ์ํด style.css์์ title ํด๋์ค์ font-family: 'Times New Roman', Times, serif; ๋ฅผ ์ถ๊ฐํด์คฌ๋ค.
const h1 = document.querySelector(".hello:first-child h1");
function handleTitleClick() {
const clickedClass = "clicked";
if (h1.classList.contains(clickedClass)) {
h1.classList.remove(clickedClass);
} else {
h1.classList.add(clickedClass);
}
}
h1.addEventListener("click", handleTitleClick);
toggle()์ ํ ํฐ์ด ์กด์ฌํ๋ฉด ํ ํฐ์ ์ ๊ฑฐํ๊ณ , ์กด์ฌํ์ง ์์ผ๋ฉด ์ถ๊ฐํด์ค์ผ๋ก์จ ์์ ์ฝ๋๋ฅผ ๋์ฑ ๊ฐ๋ตํ๊ฒ ์งค ์ ์๋ค.
const h1 = document.querySelector(".hello:first-child h1");
function handleTitleClick() {
h1.classList.toggle("clicked");
}
h1.addEventListener("click", handleTitleClick);
'javascript > ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DAY 10 (0) | 2023.01.04 |
---|---|
DAY 8 (0) | 2023.01.03 |
DAY 4 (0) | 2022.12.30 |
DAY 3 (0) | 2022.12.28 |
DAY 2 (1) | 2022.12.28 |