자바스크립트 - 랜덤하게 문장을 불러온다.

Jin Yun·2023년 9월 18일
0

중요 키워드

Math.floor()

const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)];

Math.floor()는 소수점 숫자을 제거하고 주어진 숫자와 같거나 작은 정수 중에서 가장 큰 수를 반환합니다.

Math.random()

const todaysQuote = quotes[Math.floor(Math.random() * quotes.length)];

Math.random()은 보통 length과 함께 자주 사용되는데 배열의 index 숫자 만큼 무작위로 숫자를 보내줍니다.

랜덤 문장 불러오기

const quotes = [
    {
      quote: "The way to get started is to quit talking and begin doing.",
      author: "Walt Disney",
    },
    {
      quote: "Life is what happens when you're busy making other plans.",
      author: "John Lennon",
    },
    {
      quote:
        "The world is a book and those who do not travel read only one page.",
      author: "Saint Augustine",
    },
    {
      quote: "Life is either a daring adventure or nothing at all.",
      author: "Helen Keller",
    },
    {
      quote: "To Travel is to Live",
      author: "Hans Christian Andersen",
    },
    {
      quote: "Only a life lived for others is a life worthwhile.",
      author: "Albert Einstein",
    },
    {
      quote: "You only live once, but if you do it right, once is enough.",
      author: "Mae West",
    },
    {
      quote: "Never go on trips with anyone you do ntot love.",
      author: "Hemmingway",
    },
    {
      quote: "We wander for distraction, but we travel for fulfilment.",
      author: "Hilaire Belloc",
    },
    {
      quote: "Travel expands the mind and fills the gap.",
      author: "Sheda Savage",
    },
  ];

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;
profile
호주 개발자

0개의 댓글

관련 채용 정보