์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
- canvas image
- ์ฝ๋ฉ์ผ๊ธฐ
- react
- ๋ ธ๋ง๋ ์ฝ๋
- HTML ํ์ผ๊ตฌ์กฐ
- next.js
- tailwindcss
- negative margin
- typescript
- ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ
- tanstack-query
- ์ํ์ฝ๋ฉ
- axios-mock-adapte
- queryprovider
- ๋ ธ๋ง๋์ฝ๋
- JAVA Script
- ํ๋ก ํธ์๋
- ๋ฐ๋๋ผ JS๋ก ๊ทธ๋ฆผํ ๋ง๋ค๊ธฐ
- javascript
- ์ฝ๋ฉ๋ ํ
- CSS
- HTML
- clone coding
- canvas js
- ์น ๋ธ๋ผ์ฐ์ ๋ ๋๋ง
- jest
- JS
- canvas
- ์ฝ์ฝ์ํก ํด๋ก ์ฝ๋ฉ
- ์น ๋ธ๋ผ์ฐ์ ๊ตฌ์กฐ
- Today
- Total
Coding Archive
๋ ธ๋ง๋ ์ฝ๋ - ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ(7) ๋ณธ๋ฌธ
๋ ธ๋ง๋ ์ฝ๋ - ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ(7)
์ฝ๋ฑ์ด 2022. 6. 11. 18:39#6 QUOTES AND BACKGROUND
0. Quotes
const quotes = [
{
quote:
"Life is a tragedy when seen in close-up, but a comedy in long-shot.",
author: "Charlie Chaplin",
},
{
quote: "Sometimes even to live is an act of courage.",
author: "Seneca",
},
{
quote: "What we dwell on is who we become.",
author: "Oprah Winfrey",
},
{
quote: "The secret of getting ahead is getting started.",
author: "Mark Twain",
},
{
quote:
"Great things are done by a series of small things brought together.",
author: "Vincent Van Gogh",
},
{
quote: "The way to get started is to quit talking and begin doing.",
author: "Walt Disney",
},
{
quote: "The future starts today, not tomorrow.",
author: "Saint pope John Paul II",
},
{
quote: "To be happy, we must not be too concerned with others.",
author: "Albert Camus",
},
{
quote: "Life is not fair, get used to it.",
author: "Bill Gates",
},
{
quote: "Only i can change my life. NO one can do it for me.",
author: "Carol Burnett",
},
];
const quote = document.querySelector("#quote span:first-child");
const author = document.querySelector("#quote span:last-child");
const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)];
quote.innerText = todaysQuote.quote;
author.innerText = todaysQuote.author;
1. const quotes = [ ] ;
๋ช
์ธ์ ๋ด์ ๋ณ์๋ฅผ ์์ฑํ๋ค.
2. {quote: "" ,
author: "" ,
}
๋ณ์ ์์ ๊ฐ์ฒด(object) ํํ๋ก ๋ช
์ธ๊ณผ ์๊ฐ๋ฅผ ์ ์ฅํ๋ค.
3. const quote ์ const author๋ก HTML ์์(element)๋ฅผ ๊ฐ์ ธ์จ๋ค.
(* ์ฒซ ๋ฒ์งธ span ์ ์ ํํ ๋ → #quote span:first-child๋ก ์ ์ด์ค)
4. ๋๋ค์ผ๋ก quotes ์์ ์๋ ๊ฐ์ฒด๋ค์ ์ ํํด์ผ ํ๋ค.
quotes์ ์๋ ๊ฐ์ฒด๋ค์ quotes[0], quotes[1] ์์ผ๋ก ๋ถ๋ฌ์ฌ ์ ์์ผ๋ฉฐ,
์ด๋ ๋๋ค์ผ๋ก ์ซ์๋ค์ ์ง์ ํด์ฃผ๊ณ ์ถ์ผ๋ฉด math.random() ํจ์ ์ฌ์ฉํ๋ค.
todaysQuote๋ผ๋ ๋ณ์ ๋ง๋ค๊ณ , ๊ทธ ์์ quotes[Math.floor(Math.random()*quotes.length)];๋ฅผ ๋ฃ์ด์ค๋ค.
๊ฐ์ฒด ์๊ฐ ์๋๋ผ quotes.length๋ฅผ ๋ฃ์ด์ฃผ๋ฉด quotes๋ณ์ ์์ ๊ฐ์ฒด๋ค์ด ์ถ๊ฐ๋๊ฑฐ๋ ์ญ์ ๋ผ๋ ์ฝ๋๋ฅผ ๋ฐ๊ฟ ํ์๊ฐ ์๋ค.
(* [1,2,3,4,5] ์ด๋ ๊ฒ ์๊ธด array๊ฐ ์์ ๋, Array.length๋ฅผ ์ฌ์ฉํ๋ฉด Array์ ๊ธธ์ด๋ฅผ ๋ฐํํด์ค)
5. quote์ author์ innerText๋ฅผ ๋ณ๊ฒฝํ๋ค.
todaysQuote.quote์ผ๋ก ๋ช
์ธ์ todaysQuote.author๋ก ์๊ฐ๋ฅผ ๊ฐ์ ธ์จ ํ quote์ author ๋ณ์์ ๋ฃ์ด์ค๋ค.
+) Q
const todayQuote = quotes[Math.floor(Math.random() * quotes.length)]; ์ฝ๋์์ array์ object๊ฐ ๋ฌด์์๋ก ์ ํ์ด ๋๋๋ฐ,
quote.innerText = todaysQuote.quote;
author.innerText = todaysQuote.author;
๊ทธ๋ฌ๋ฉด ์ด ์ฝ๋์์ ์์๋ ์๋์ todayQuote์ ์ํด์ ๋ค๋ฅธ ๊ฐ์ฒด๊ฐ ๋์ฌ ์๋ ์๋ ๊ฑด์ง? ๊ทธ๋ ๊ทธ๋ ๋ฌด์์๋ก ์ ํ์ด ๋๋ค๊ณ ํ๋๋ฐ quote๋ author๊ฐ ํญ์ ์ผ์น๋๋ ๊ฒ ์ด๋ป๊ฒ ๊ฐ๋ฅํ ๊ฑด์ง?
-
๋ฌด์์๋ก ์ ํ๋๋ค๋ ๊ฒ์ ๊ทธ ์ธ๋ฑ์ค ์ซ์๊ฐ ๋๋ค์ผ๋ก ๋์จ๋ค๋ ๊ฒ์ด๋ค.
randomness๋ก ์ธํด ์ซ์๊ฐ 0~9 ์ฌ์ด ์ ์๊ฐ ๋์ค๊ฒ ๋๋๋ฐ, ๊ทธ๋ผ ๊ฐ ์ธ๋ฑ์ค์ ํด๋นํ๋ object๋ค์ด ์๋ค.
๋ฐ๋ผ์ quote๋ author๋ ํญ์ ์ผ์น๋ ์๋ฐ์ ์๋ค.
Math.floor() : ๋ด๋ฆผ
Math.ceil(3.6) // => 3
Math.ceil() : ์ฌ๋ฆผ
Math.ceil(1.1) // => 2
Math.round() : ๋ฐ์ฌ๋ฆผ
Math.ceil(6.2) // => 6
Math.ceil(4.5) // => 5
Math.random() : 0๊ณผ 1 ์ฌ์ด์ ๋ฌด์์์ ๊ฐ์ ๋ฐํ
Math.random() * 10 // => 0์์ 9์ฌ์ด์ ์ซ์๋ค์ ์ป์ ์ ์์ (0๋ถํฐ ์
)
1. Background
const images = ["0.jpg", "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg"];
const chosenImage = images[Math.floor(Math.random() * images.length)];
const bgImage = document.createElement("img");
bgImage.src = `img/${chosenImage}`;
document.body.appendChild(bgImage);
js์์ html์ ํ๊ทธ๋ฅผ ์์ฑ
- createElement() : ํ๊ทธ๋ฅผ ์์ฑํ๋ค. (์์ง html์ ์๋ ์ํ → js์์๋ง ๋ณผ ์ ์๋ ์ํ)
- appendChild : ๋ถ๋ชจ ๋ ธ๋์ ์์ ๋ ธ๋ ์ถ๊ฐํ๋ค. (html์์ ์์ฑ๋๋ ๊ฑธ ๋ณผ ์ ์์)
2. Recap
. Math ๊ฐ์ฒด ๊ธฐ๋ฅ
Math.random() 0๋ถํฐ 1 ์ฌ์ด ๋ฌด์์์ ๊ฐ ๋ฐํ
Math.floor() ๋ด๋ฆผ
Math.ceil() ์ฌ๋ฆผ
Math.round() ๋ฐ์ฌ๋ฆผ
JS์์ html ์์๋ฅผ ์์ฑ
createElement(" ")
ex)
document.createElement("img") ์ผ ๊ฒฝ์ฐ html ๋ด์ img ํ๊ทธ๋ฅผ ์์ฑ
appendChild() → ํจ์ ์์ ๊ฒฝ๋ก์ ์ ์ํ ๊ฐ์ ๊ฐ์ฅ ๋ค์ ๊ธฐ์
prepend() → ๋ฐ๋๋ก ์์์ ๊ธฐ์
'๐ฅ๏ธ Clone Coding' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ ธ๋ง๋ ์ฝ๋ - ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ(9) (0) | 2022.06.14 |
---|---|
๋ ธ๋ง๋ ์ฝ๋ - ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ(8) (0) | 2022.06.13 |
๋ ธ๋ง๋ ์ฝ๋ - ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ(6) (0) | 2022.06.10 |
๋ ธ๋ง๋ ์ฝ๋ - ๋ฐ๋๋ผ JS๋ก ํฌ๋กฌ ์ฑ ๋ง๋ค๊ธฐ(5) (0) | 2022.06.09 |
๋ ธ๋ง๋ ์ฝ๋ - ์ฝ์ฝ์ํก ํด๋ก ์ฝ๋ฉ ์ฐ์ต(7) (0) | 2022.05.20 |