본문 바로가기
Java Script

Quotes

by _션샤인 2024. 8. 27.

6.0 Quotes

To make Random Quotes, create forntend by HTML

 

Randomness

 

console.log(quotes[10]

We need function that can give us number 9 to 0.

 

1) To get the number 0~9

Math.random() * 10

 

2) To remove float numbers

Math.round (반올림)

Math.ceil (올림)

(1.01 -> 2, 1.0 -> 1.0, 1.1->2)

Math.floor (내림)

 

 

console.log(quotes[Math.floor(Math.random() * quotes.length)]);

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

                                             random number between zero to one

 

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

                                                                           multifly that for length of array

 

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

 

 

6.1 Background

 

const image = ["0.jpg", "1.jpg", "2.jpg" ...] -> array the images

const chosenImage = images[Math.floor(Math.random() * images.length)];

console.log(chosenImage);

 

*Paste something created with Javascript in the HTML

goal : to create string like - > <img src = "img/0.jpg">

 

const bgImage = document.createElement("img")

 

bcImage.src=`img/${chosenImage}`;

document.body.appendChild(bgImage);

 -> appendChild is going to add an HTML to the body

 -> append : is at the end / prepend : is on the top

 

const images = [
    "0.jpg", "2.jpg","3.jpg","4.jpg","5.jpg"]

const chosenImage = images[Math.floor(Math.random() * images.length)];

console.log(chosenImage);

const bgImage = document.createElement("img");

bgImage.src = `img/${chosenImage}`;

document.body.appendChild(bgImage);

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'Java Script' 카테고리의 다른 글

To do list (2)  (1) 2024.08.29
To do list (1)  (0) 2024.08.28
Clock  (0) 2024.08.26
Login Form  (0) 2024.08.23
Javascript on the browser  (0) 2024.08.22