Coding Archive

๋…ธ๋งˆ๋“œ ์ฝ”๋” - ๋ฐ”๋‹๋ผ JS๋กœ ํฌ๋กฌ ์•ฑ ๋งŒ๋“ค๊ธฐ(7) ๋ณธ๋ฌธ

๐Ÿ–ฅ๏ธ Clone Coding

๋…ธ๋งˆ๋“œ ์ฝ”๋” - ๋ฐ”๋‹๋ผ 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์˜ ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑ

  1. createElement() : ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. (์•„์ง html์— ์—†๋Š” ์ƒํƒœ → js์—์„œ๋งŒ ๋ณผ ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ)
  2. 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() → ๋ฐ˜๋Œ€๋กœ ์•ž์—์„œ ๊ธฐ์ž…

 

Comments