JavaScript Essentials 1
Overview and Objectives
In this project, you will create a Simple Interactive Quiz Application using JavaScript. This application will:
- Prompt the user for their name.
- Present a series of quiz questions to the user using
prompt()
. - Use loops and conditional statements to iterate through questions and evaluate responses.
- Use functions to encapsulate quiz logic (e.g., to ask a question, validate input, and calculate the score).
- Handle invalid input gracefully using error handling techniques.
- Output the final score and a personalized message using
alert()
orconsole.log()
.
This project integrates all the key concepts from the module and prepares you for more advanced topics.
Tasks and Instructions
Task 1: Setup and Greeting
-
Create Files:
- Create a file named
quizApp.js
.
- Create a file named
-
User Greeting:
- Prompt the user to enter their name and store it in a variable.
- Display a welcome message that includes the user’s name.
Task 2: Quiz Questions and Logic
-
Define Quiz Data:
- Create an array of at least 5 quiz questions.
- Each quiz question can be an object with properties like
question
andanswer
. For example:const quizQuestions = [ { question: "What is the file extension for JavaScript files?", answer: ".js" }, { question: "Which keyword is used to declare a block-scoped variable?", answer: "let" }, // Add more questions... ];
-
Function Implementation:
- Write a function
askQuestion(questionObj)
that:- Uses
prompt()
to display the question to the user. - Captures the user’s answer and compares it (case-insensitive) to the correct answer.
- Returns
true
if the answer is correct andfalse
otherwise.
- Uses
- Write another function
runQuiz()
that:- Iterates through the
quizQuestions
array using a loop. - Calls
askQuestion()
for each question. - Maintains a score counter that increments for each correct answer.
- Returns or displays the total score at the end.
- Iterates through the
- Write a function
Task 3: Error Handling and Input Validation
- Input Validation:
- Ensure that when using
prompt()
, if the user enters an empty value, the program prompts again or treats it as incorrect.
- Ensure that when using
- Exception Handling:
- Use
try...catch
around parts of the code where errors might occur (e.g., converting input) to prevent the application from crashing. - Display a user-friendly error message if an unexpected error occurs.
- Use
Task 4: Final Output
- Display Score:
- After all questions have been answered, display the final score along with a personalized message (e.g., “Great job, [Name]! You scored X out of Y.”).
- Enhancement:
- Allow the user to retake the quiz by asking if they want to play again, and repeat the process if they agree.
Submission Criteria
The following should be included within a GitHub repository, and submitted via Canvas:
-
Files/Links:
- Submit the
quizApp.js
file. - If applicable, provide a link to an online editor project (e.g., CodePen, JSFiddle).
- Submit the
-
Evidence of Output:
- Provide screenshots or a text log demonstrating the quiz execution (showing the greeting, question prompts, and final score output).
-
Error-Free Code:
- The code should execute without runtime errors and meet the functionality described above.
Grading Rubric
Criteria | Points | Description |
---|---|---|
User Greeting and Setup | 15 pts | Correctly prompts for the user’s name and displays a personalized welcome message. |
Quiz Data Structure | 10 pts | Defines an array of quiz questions as objects with at least two properties (question and answer ). |
Function Implementation (askQuestion) | 15 pts | Correctly implements a function to prompt the user, capture their answer, compare it (case-insensitive) to the correct answer, and return a boolean. |
Quiz Loop and Score Calculation | 15 pts | Uses a loop to iterate through the questions, calls askQuestion() for each, and maintains an accurate score counter. |
Input Validation and Error Handling | 15 pts | Implements validation to handle empty or invalid input and uses try…catch to gracefully handle potential errors. |
Final Output and User Feedback | 10 pts | Displays the final score with a personalized message; output is clear and informative. |
Code Organization and Modularity | 10 pts | Code is modular with clearly defined functions, uses descriptive variable names, and includes appropriate comments explaining key sections. |
Enhancements | 10 pts | Additional features (e.g., allowing replay, extra error handling) are implemented and function correctly. Awarded only if fully functional. |
Total Points: 100 points
Partial credit will be awarded for submissions that demonstrate a clear understanding of the project requirements even if some elements are incomplete or contain minor errors. For example, if the quiz functionality works correctly for most questions but lacks comprehensive error handling, or if the code structure is functional but not fully modularized, points will be deducted proportionally. Credit will be given for correct implementation of key components such as user greeting, question prompting, and score calculation, even if optional enhancements are missing or only partially implemented.