-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
66 lines (57 loc) · 1.59 KB
/
main.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
let userName;
const nameDisplay = document.getElementById('name');
const answers = [
'You wish!',
`Who
really cares
?`,
`You bet your ass
!`,
'How about no?',
`
You bore me`,
'Buy me a drink 1st',
'No with a but',
'Yes with an if',
];
const submitBtn = document.getElementById('submit-btn');
const questionDisp = document.getElementById('question-display');
const form = document.querySelector('form');
const answerDisp = document.querySelector('.textbox');
const triangle = document.querySelector('.triangle');
const getName = () => {
userName = prompt('What is your Name');
if (userName === '') {
prompt('Please enter a valid name');
}
};
const getRandomWord = () => {
return answers[Math.floor(Math.random() * answers.length)];
};
const ani = () => {
triangle.classList.add('triangle-ani');
answerDisp.classList.add('text-ani');
};
const removeAni = () => {
triangle.classList.remove('triangle-ani');
answerDisp.classList.remove('text-ani');
};
const handleQuestion = (question) => {
if (question[question.length - 1] !== '?' || question.length <= 1) {
alert('Ask a real question please');
} else {
questionDisp.textContent = `You asked: ${question}`;
question
? (answerDisp.textContent = getRandomWord())
: (answerDisp.textContent = '');
ani();
setTimeout(removeAni, 3000);
}
};
form.addEventListener('submit', (event) => {
event.preventDefault();
const question = document.getElementById('question').value;
handleQuestion(question);
});
getName();
nameDisplay.textContent = `Hi ${userName}, please ask a question below`;