-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Music Maven- My music Quiz Bot #288
Open
Anna2024WebDev
wants to merge
19
commits into
Technigo:main
Choose a base branch
from
Anna2024WebDev:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d0bf1ea
intital commit
Anna2024WebDev 8fa1f7c
Due to difficulties I commit a large update of everything from update…
Anna2024WebDev afed082
Added media queries, changed colours and added a div for the option-b…
Anna2024WebDev fc6c40f
added more questions, added score function and fixed styling and colours
Anna2024WebDev 9b7fd82
updated readme
Anna2024WebDev ab28d8c
updated CSS padding buttons
Anna2024WebDev 140c1f5
changed font and tried to remove horizontal scroll
Anna2024WebDev 94151b2
increased the size of the bot and user img
Anna2024WebDev 270a963
changed bubble padding
Anna2024WebDev 94515bc
small fixes
Anna2024WebDev 849e272
small fixes in CSS
Anna2024WebDev 1d832d7
fix
Anna2024WebDev 927f505
fixes to CSS max-widthand overflow
Anna2024WebDev 462c407
fix
Anna2024WebDev d5d491e
removed extras
Anna2024WebDev 875af73
try to fix responsiveness again
Anna2024WebDev 598cf95
added flex-grow to the chat section
Anna2024WebDev 04baca4
fix
Anna2024WebDev a734b5f
fixed bug in JS code, answers revealed when double clicking on buttons
Anna2024WebDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
# Project Name | ||
|
||
Replace this readme with your own information about your project. | ||
|
||
Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
The second project of the Javascript sprint was to create a chat bot using functions, DOM selectors and conditionals. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
I had a hard time getting started with this. I understand the overall logic but found it difficult to put everything in place. The most difficult part was to put together the ask question function and getting it to check correct answer. I needed a lot of help from chat gpt for that, also with the final score function. I also found it hard with the order of things and getting each step to work. | ||
|
||
## View it live | ||
I also watched the videos about DOMs and functions again to try to understand the whole concept and putting it together but I must say that I still am somewhat confused and need to read more about this. | ||
|
||
Have you deployed your project somewhere? Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about. | ||
If I had more time I would add a function to restart the quiz. I would also add some sounds since it is a music quiz, that could be fun. | ||
|
||
## View it live | ||
https://musicmaven.netlify.app/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,67 @@ | ||
// DOM selectors (variables that point to selected DOM elements) goes here 👇 | ||
const chat = document.getElementById('chat') | ||
const chat = document.getElementById("chat") | ||
const inputWrapper = document.getElementById("input-wrapper") | ||
const form = document.getElementById("name-form") | ||
const nameInput = document.getElementById("name-input") | ||
|
||
// Functions goes here 👇 | ||
let currentQuestionNumber = 1 // Start with Question 1 | ||
let score= 0 | ||
|
||
// Question data | ||
const Question1 = { | ||
question: "Who released a single in 1999 called Genie In a Bottle?", | ||
options: ["Britney Spears", "Christina Aguilera", "Robyn", "Corona"], | ||
correctAnswer: "Christina Aguilera" | ||
} | ||
|
||
const Question2 = { | ||
question: "Which song has the lyrics: 'It´s like ten thousand spoons when all you need is a knife'?", | ||
options: ["Torn", "Ironic", "Killing me softly", "You learn"], | ||
correctAnswer: "Ironic" | ||
} | ||
|
||
const Question3 = { | ||
question: "Who wrote the song 'I will always love you'?", | ||
options: ["Whitney Houston", "Celine Dion", "Dolly Parton", "Mariah Carey"], | ||
correctAnswer: "Dolly Parton" | ||
} | ||
|
||
const Question4= { | ||
question: "Which artist has not competed in the Eurovision Song Contest?", | ||
options: ["Celine Dion", "Julio Iglesias", "Bonnie Tyler", "Vanessa Paradis"], | ||
correctAnswer: "Vanessa Paradis" | ||
} | ||
|
||
const Question5= { | ||
question: "Who is the composer of 'Moonlight Sonata'?", | ||
options: ["Bach", "Beethoven", "Mozart", "Hendel"], | ||
correctAnswer: "Beethoven" | ||
} | ||
|
||
const Question6= { | ||
question: "What band was Kurt Cobain the lead singer of?", | ||
options: ["Pearl Jam", "Nirvana", "Soundgarden", "Hole"], | ||
correctAnswer: "Nirvana" | ||
} | ||
|
||
const Question7= { | ||
question: "Which artist had a 2019 hit with the song 'Bad Guy'?", | ||
options: ["Billie Eilish", "Ariana Grande", "Selena Gomez", "Lizzo"], | ||
correctAnswer: "Billie Eilish" | ||
} | ||
|
||
const Question8= { | ||
question: "Who won the first season of American Idol?", | ||
options: ["Jennifer Hudson", "Adam Lambert", "Kelly Clarkson", "Carrie Underwood"], | ||
correctAnswer: "Kelly Clarkson" | ||
} | ||
|
||
|
||
// Array of questions | ||
const questions = [Question1, Question2, Question3, Question4, Question5, Question6, Question7, Question8] | ||
|
||
// A function that will add a chat bubble in the correct place based on who the sender is | ||
const showMessage = (message, sender) => { | ||
// The if statement checks if the sender is the user and if that's the case it inserts | ||
// an HTML section inside the chat with the posted message from the user | ||
if (sender === 'user') { | ||
chat.innerHTML += ` | ||
<section class="user-msg"> | ||
|
@@ -16,8 +71,6 @@ const showMessage = (message, sender) => { | |
<img src="assets/user.png" alt="User" /> | ||
</section> | ||
` | ||
// The else if statement checks if the sender is the bot and if that's the case it inserts | ||
// an HTML section inside the chat with the posted message from the bot | ||
} else if (sender === 'bot') { | ||
chat.innerHTML += ` | ||
<section class="bot-msg"> | ||
|
@@ -28,26 +81,160 @@ const showMessage = (message, sender) => { | |
</section> | ||
` | ||
} | ||
|
||
// This little thing makes the chat scroll to the last message when there are too many to | ||
// be shown in the chat box | ||
chat.scrollTop = chat.scrollHeight | ||
} | ||
// Scroll only after new content has been added | ||
setTimeout(() => { | ||
chat.scrollTop = chat.scrollHeight; | ||
}, 100); | ||
}; | ||
|
||
// A function to start the conversation | ||
const greetUser = () => { | ||
// Here we call the function showMessage, that we declared earlier with the argument: | ||
// "Hello there, what's your name?" for message, and the argument "bot" for sender | ||
showMessage("Hello there, what's your name?", 'bot') | ||
// Just to check it out, change 'bot' to 'user' here 👆 and see what happens | ||
|
||
// Show the name input field | ||
inputWrapper.innerHTML = ` | ||
<input id="name-input" placeholder="Enter name" /> | ||
<button id="name-submit">Submit</button> | ||
` | ||
|
||
document.getElementById('name-submit').addEventListener('click', () => { | ||
const userName = document.getElementById('name-input').value | ||
if (userName) { | ||
showMessage(userName, 'user') | ||
setTimeout(() => { | ||
showMessage(`Nice to meet you, ${userName}! Are you ready for a quiz?`, 'bot') | ||
setTimeout(showYesNoButtons, 900) | ||
}, 700) | ||
} | ||
}) | ||
} | ||
|
||
|
||
// Show Yes/No buttons for quiz confirmation | ||
const showYesNoButtons = () => { | ||
inputWrapper.innerHTML = ` | ||
<button id="yes">Yes</button> | ||
<button id="no">No</button> | ||
` | ||
|
||
document.getElementById("yes").addEventListener('click', () => { | ||
showMessage("Yes", "user") | ||
inputWrapper.innerHTML = '' // Remove Yes/No buttons | ||
setTimeout(() => askQuestion(), 1000) // Start the quiz | ||
}) | ||
|
||
document.getElementById('no').addEventListener('click', () => { | ||
showMessage("No", "user") | ||
setTimeout(() => { | ||
showMessage("Ok, see you another time!", 'bot') | ||
inputWrapper.innerHTML = '' // Clear buttons | ||
}, 700) | ||
}) | ||
} | ||
|
||
// Eventlisteners goes here 👇 | ||
|
||
// Here we invoke the first function to get the chatbot to ask the first question when | ||
// the website is loaded. Normally we invoke functions like this: greeting() | ||
// To add a little delay to it, we can wrap it in a setTimeout (a built in JavaScript function): | ||
// and pass along two arguments: | ||
// 1.) the function we want to delay, and 2.) the delay in milliseconds | ||
// This means the greeting function will be called one second after the website is loaded. | ||
setTimeout(greetUser, 1000) | ||
|
||
// Ask the current question and show answer options | ||
const askQuestion = () => { | ||
const currentQuestion = questions[currentQuestionNumber - 1] | ||
|
||
if (currentQuestion) { | ||
showMessage(currentQuestion.question, 'bot') | ||
|
||
// Show options as buttons. Each button has a unique id (option-0, option-1, option-2, option-3, for example for question nr 1-option 0 is Britney Spears, option1 is Christina Aguilera etc. ${currentQuestion.options[0]}, ${currentQuestion.options[1]}, etc., are placeholders that get replaced with the actual options for the current question. | ||
|
||
inputWrapper.innerHTML = ` | ||
<div class="option-buttons"> | ||
<button id="option-0">${currentQuestion.options[0]}</button> | ||
<button id="option-1">${currentQuestion.options[1]}</button> | ||
<button id="option-2">${currentQuestion.options[2]}</button> | ||
<button id="option-3">${currentQuestion.options[3]}</button> | ||
</div> | ||
` | ||
|
||
// Add event listeners to the buttons | ||
currentQuestion.options.forEach((option, index) => { | ||
document.getElementById(`option-${index}`).addEventListener('click', () => { | ||
showMessage(option, 'user') | ||
checkAnswer(option) // Check the selected answer | ||
}) | ||
}) | ||
} else { | ||
// If there are no more questions, end the quiz | ||
showMessage("Quiz finished! Thanks for playing.", 'bot') | ||
inputWrapper.innerHTML = '' // Clear buttons after the quiz ends | ||
} | ||
} | ||
|
||
// Check the user's answer and move to the next question | ||
const checkAnswer = (selectedOption) => { | ||
setTimeout(() => { | ||
const currentQuestion = questions[currentQuestionNumber - 1] | ||
|
||
if (selectedOption === currentQuestion.correctAnswer) { | ||
showMessage("Correct!", 'bot') | ||
score++ // Increment the score for a correct answer | ||
} else { | ||
showMessage(`Wrong! The correct answer is ${currentQuestion.correctAnswer}.`, 'bot') | ||
} | ||
}, 900) | ||
|
||
|
||
// After answering, move to the next question if there are more questions | ||
setTimeout(() => { | ||
inputWrapper.innerHTML = '' // Clear buttons after selecting an answer | ||
currentQuestionNumber++ // Increment the question number | ||
|
||
if (currentQuestionNumber <= questions.length) { | ||
askQuestion() // Ask the next question | ||
} else { | ||
showFinalScore() // Show the final score at the end | ||
|
||
} | ||
}, 900) // Timeout | ||
} | ||
|
||
// Function to display the final score | ||
const showFinalScore = () => { | ||
setTimeout(() => { | ||
showMessage(`Quiz finished! You got ${score} out of ${questions.length} correct. Thanks for playing!`, 'bot') | ||
inputWrapper.innerHTML = '' // Clear buttons after the quiz ends | ||
}, 900) // Timeout | ||
} | ||
|
||
// Start the conversation | ||
setTimeout(greetUser, 900) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
Comment on lines
+213
to
+246
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ending with one empty line is enough 😉 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let score = 0
*