-
Notifications
You must be signed in to change notification settings - Fork 0
/
greetings.js
32 lines (26 loc) · 933 Bytes
/
greetings.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const greetings=["Hi","Hello"];
const loginForm=document.querySelector("#login-form");
const loginInput=document.querySelector("#login-form input");
const greetingMsg=document.querySelector("#greeting")
const HIDDEN_CLASSNAME="hidden";
const USERNAME_KEY="userName";
function onLoginSubmit(event) {
event.preventDefault();
loginForm.classList.add(HIDDEN_CLASSNAME);
const userName=loginInput.value;
localStorage.setItem(USERNAME_KEY, userName);
paintGreetings(userName);
}
function paintGreetings(username) {
const index=Math.floor(Math.random()*2);
greetingMsg.innerHTML=`${greetings[index]} ${username}`;
greetingMsg.classList.remove(HIDDEN_CLASSNAME);
}
const savedUserName=localStorage.getItem(USERNAME_KEY)
if (savedUserName === null) {
loginForm.classList.remove(HIDDEN_CLASSNAME);
loginForm.addEventListener("submit", onLoginSubmit);
}
else {
paintGreetings(savedUserName);
}