명언 보여주기는 기본적인 내용만으로 충분히 구현이 가능했던 것 같다.
Math.random 메서드로 난수를 만들어 랜덤한 인덱스를 불러오는 방법,
document.querySeletor로 HTML의 요소를 자바스크립트로 불러오는 방법만 알고있다면 쉽게 코드를 작성할 수 있을 것이다.
객체로 된 배열을 만든다.
const quotes = [
{quote: "I never dreamed about success, I worked for it.",
author: "Estee Lauder",},
{quote: "Do not try to be original, just try to be good.",
author: "Paul Rand",},]
HTML에서 글과 작가에 대한 내용을 담을 span요소 두개를 만든다.
document.querySelector(”#quote span:first-child”)
document.querySelector(”#quote span:first-child”)
쿼리셀렉터로 span요소를 가져온다.
명언이 랜덤으로 나올 수 있게 만들어진 배열의 인덱스에 Math.random으로 난수를 만든다.
quotes[Math.floor(Math.random() * quotes.length)];
쿼리셀렉터로 불러온 요소에 난수로 만들어진 배열의 인덱스가 저장된 변수를 textContent로 지정해준다.
체크리스트