* 랜덤함수
Math.random() = 0~1 사이에 정수들이 생성된다.
ㄴ Math.random() * 10 = 0~10 정수들을 생성한다.
(결과 값 : 1.0250 or 9.540 ... 소숫점까지 생성)
Math.round() = 반올림
ㄴ Math.round(1.1) === 1, Math.round(1.6) === 2
Math.ceil() = 올림
Math.floor() = 내림
* 딱 떨어지는 숫자를 만들기
Math.floor(Math.random()*10); = 0~9까지 랜덤 생성
| const quotes = [ { quote : "Be yourself; everyone else is already taken.", author : "Oscar Wilde" }, { quote : "If you tell the truth, you don't have to remember anything.", author : "Mark Twain" }, { quote : "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.", author : "Albert Einstein" }, { quote : "So many books, so little time.", author : "Frank Zappa" }, { quote : "A room without books is like a body without a soul.", author : "Marcus Tullius Cicero" }, { quote : "Be the change that you wish to see in the world.", author : "Mahatma Gandhi" }, { quote : "You only live once, but if you do it right, once is enough.", author : "Mae West" }, { quote : "To live is the rarest thing in the world. Most people exist, that is all.", author : "Oscar Wilde" }, { quote : "Without music, life would be a mistake.", author : "Friedrich Nietzsche" }, { quote : "Live as if you were to die tomorrow. Learn as if you were to live forever.", author : " Mahatma Gandhi" }, ] // 명언 리스트 const quote = document.querySelector("#quote span:first-child"); // html 1번 <span> 접근 const author = document.querySelector("#quote span:last-child"); // html 2번 <span> 접근 const todaysQuote = quotes[Math.floor(Math.random()*quotes.length)]; quote.innerText = todaysQuote.quote; author.innerText = todaysQuote.author; |
const todaysQuote = quotes[Math.floor(Math.random()*quotes.length)];
배열이 총 10개 0~9까지 이므로 Math.random() * quotes.length을 해줌
quotes.length 는 배열의 갯수를 구하는 객체
quote.innerText = todaysQuote.quote;
author.innerText = todaysQuote.author;
quote 1번 <span>에 접근해서 todaysQuote의 quote해당하는 부분을 가져다 써라
quthor 작동원리 동일
반응형
'프론트엔드 > javascript' 카테고리의 다른 글
| javascript (12) - TodoList #1 (0) | 2023.04.12 |
|---|---|
| javascript (11) - 백그라운드 랜덤 사진 (0) | 2023.04.11 |
| javascript (10) - intervals, timeout, padStart, padEnd (0) | 2023.04.11 |
| javascript (8) - Login 만들기 #3 (0) | 2023.04.05 |
| javascript (8) - Login 만들기 #2 (0) | 2023.04.04 |