💻 JavaScript Snippet to Store Friends' Names in an Array
Let's understand how to store and access friend names in an array and create an interactive question around it.
📦 Array of Friends
const friends = ["Amit", "Sonu", "Monu", "Gonu", "Salu"];
This array stores the names of friends as string elements. Now, we’ll use this array to generate a small quiz.
❓ Quiz Format Snippet
const question = {
prompt: "Which friend is not included in the array?",
options: ["Amit", "Sonu", "Monu", "Gonu", "Saluu"],
correctAnswer: "Saluu"
};
Notice that "Saluu" is spelled with an extra 'u' and is not present in the original array.
🧠 Try This!
Which friend is not in the array?
🛠️ Explanation
The JavaScript object question
defines a multiple-choice quiz. It contains:
- prompt: The question to display
- options: Array of answer choices
- correctAnswer: The correct answer to check against
➡️ Why "Saluu" is correct?
Because it’s not in the original array: ["Amit", "Sonu", "Monu", "Gonu", "Salu"]
. The extra "u" makes it invalid.
🧪 Practice Tip:
Use this method to dynamically create quizzes from any dataset. This helps in building learning tools, interview practice sets, and student worksheets.
🔗 Related Posts
© All Rights Reserved | Posted on STEMFact Blog | Updated: July 2025
No comments:
Post a Comment