-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (115 loc) · 4 KB
/
script.js
File metadata and controls
133 lines (115 loc) · 4 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// DOM selectors
const chat = document.getElementById('chat');
const nameInput = document.getElementById('name-input');
const form = document.getElementById('name-form');
const buttonContainer = document.getElementById('button-container');
// Conversation state
let userName = '';
// Sound effects
const sendSound = new Audio('assets/message-sent.mp3');
const receiveSound = new Audio('assets/new-notification.mp3');
// Functions
// Play send sound
const playSendSound = () => {
sendSound.play();
};
// Play receive sound
const playReceiveSound = () => {
receiveSound.play();
};
// Final message
const goodbyeMessage = () => {
showMessage(`Félicitations, ${userName}! You've completed the French learning session. Au revoir!`, 'bot');
};
// Handle user's answer to French questions
const handleFrenchAnswer = (answer, correctAnswer, nextQuestion) => {
showMessage(answer, 'user');
playSendSound();
buttonContainer.style.display = 'none'; // Hide buttons
setTimeout(() => {
if (answer === correctAnswer) {
showMessage(`Excellent! "${answer}" is correct. 🥐`, 'bot');
} else {
showMessage(`Not quite.. The correct answer is "${correctAnswer}".`, 'bot');
}
playReceiveSound();
setTimeout(() => nextQuestion(), 1000);
}, 1000);
};
// Create buttons for French questions
const createFrenchButtons = (options, correctAnswer, nextQuestion) => {
form.style.display = 'none'; // Hide form
buttonContainer.innerHTML = options.map(option =>
`<button type="button" class="button-class">${option}</button>`
).join('');
buttonContainer.style.display = 'flex'; // Show buttons
// Add event listeners to buttons
buttonContainer.querySelectorAll('button').forEach(button => {
button.addEventListener('click', () => handleFrenchAnswer(button.textContent, correctAnswer, nextQuestion));
});
};
// Ask how to say "Yes" in French
const askYesInFrench = () => {
showMessage("How do you say 'Yes' in French?", 'bot');
playReceiveSound();
createFrenchButtons(["Non", "Oui", "Peut-être"], "Oui", goodbyeMessage);
};
// Ask how to say "Thank you" in French
const askThankYouInFrench = () => {
showMessage("How do you say 'Thank you' in French?", 'bot');
playReceiveSound();
createFrenchButtons(["S'il vous plaît", "Merci", "Oui"], "Merci", askYesInFrench);
};
// Ask how to say "Hello" in French
const askHelloInFrench = () => {
showMessage("How do you say 'Hello' in French?", 'bot');
playReceiveSound();
createFrenchButtons(["Bonjour", "Merci", "Au revoir"], "Bonjour", askThankYouInFrench);
};
// Start French lesson
const startFrenchLesson = (name) => {
userName = name;
showMessage(`Enchanté, ${name}! 👩🏻🎨🍷 Let's learn some French.`, 'bot');
playReceiveSound();
setTimeout(askHelloInFrench, 1000);
};
// Handle the name input
const handleNameInput = (event) => {
event.preventDefault();
const name = nameInput.value.trim();
if (name === '') {
showMessage('', 'user');
playSendSound();
setTimeout(() => {
showMessage(`You forgot to type your name`, 'bot');
playReceiveSound();
}, 1000);
} else {
showMessage(name, 'user');
playSendSound();
nameInput.value = ''; // Empty textfield
setTimeout(() => startFrenchLesson(name), 1000);
}
};
// Event listener for the form
form.addEventListener('submit', handleNameInput);
// Function that shows different chat bubbles depending on 'user' or 'bot'
const showMessage = (message, sender) => {
const messageHTML = `
<section class="${sender}-msg">
${sender === 'bot' ? '<img src="assets/bot.png" alt="Bot" />' : ''}
<div class="bubble ${sender}-bubble">
<p>${message}</p>
</div>
${sender === 'user' ? '<img src="assets/user.png" alt="User" />' : ''}
</section>
`;
chat.innerHTML += messageHTML;
chat.scrollTop = chat.scrollHeight;
};
// Greeting message
const greetUser = () => {
showMessage(`Bonjour! I'm the French Learning Bot. 🇫🇷🥖 What's your name?`, 'bot');
};
// Start by greeting the user
setTimeout(greetUser, 1000);