const btn = document.querySelector("#emoji");
const emojis = [
"๐",
"๐
",
"๐คฃ",
"๐",
"๐",
"๐ค",
"๐คจ",
"๐",
"๐",
"๐",
"๐",
"๐",
"๐คฅ",
"๐ด",
"๐ฅบ",
"๐ง",
"๐",
"๐ณ",
"๐",
"๐ฅด",
"๐ง",
"๐คจ",
"๐",
"๐ค",
"๐คญ",
"๐ฅฐ",
"๐ค",
"๐",
"๐ค",
"๐คช",
"๐ฅฒ",
"๐",
"๐",
"๐ฌ",
];
btn.addEventListener("mouseover", () => {
btn.innerText = emojis[Math.floor(Math.random() * emojis.length)];
});
JavaScript
const btn = document.querySelector("#emoji");
document= the webpagequerySelector()= finds an element
It finds
<div id="emoji">
and stores it in btn
Think of it like:
Web Page
โ
Find
<div id="emoji">
โ
btn
2.
const emojis = [
"๐",
"๐
",
"๐คฃ",
...
];
This is an array
It stores many emojis
Index
0 ๐
1 ๐
2 ๐คฃ
3 ๐
4 ๐
5 ๐ฅฐ
...
3.
btn.addEventListener("mouseover", () => {
This waits for an event
mouseover means:
"When the mouse enters this element"
Mouse
โ
๐
โ
Run JavaScript
4.
Math.random()
Generates a random decimal number between 0 (inclusive) and 1 (exclusive)
Example:
0.23
0.81
0.45
0.02
It changes every time
5.
Math.random() * emojis.length
Suppose there are 35 emojis
Then:
0.45 ร 35
=
15.75
or
0.92 ร 35
=
32.2
6.
Math.floor()
Removes the decimal part
Example:
15.75
โ
15
32.2
โ
32
So you get a valid array index
7.
emojis[Math.floor(Math.random() * emojis.length)]
This picks a random emoji from the array
Example:
Random number
โ
12
โ
emojis[12]
โ
๐คฅ
Next time:
Random
โ
25
โ
๐ฅฐ
8.
btn.innerText =
Changes the text inside the <div>
Initially:
๐
After hover:
๐ฅฐ
Again:
๐
Flow of the program
Page Opens
โ
โผ
Show ๐
โ
โผ
Mouse moves over emoji
โ
โผ
mouseover event fires
โ
โผ
Generate random number
โ
โผ
Pick random emoji
โ
โผ
Replace current emoji
โ
โผ
CSS hover effect
โ
โโโ scale(1.3)
โโโ grayscale(0)
Output:
https://raja000999-project-6ae1d0.gitlab.io/emoji.html
เฎเฏเฎณเฏเฎตเฎฟ เฎเฏเฎณเฏเฎเฏเฎเฎณเฏ???
Do you have any questions??? or Feel free to ask questions???
Top comments (0)