Reputation: 19
So I only really know your basic HTML and CSS. I can manipulate stuff that is there and figure it out, but I'm not a developer and am very limited on what I can write from scratch.
I'm trying to create an interactive quiz using javascript prompt functions and if statements. I've got the code to run but there are a few things I need to do to finish that i can't figure out. I have searched and had no luck receiving [uncomplicated] solutions to my issue.
PS. I know it might not be good practice to have just one huge script like this, but if possible help me with what I have. Thanks in advance for any help, here is my function: https://gist.github.com/anonymous/dcde24cf97df24fe5ca4
Upvotes: 0
Views: 6525
Reputation: 1
This is my answer. It's years late but this is how you can create a quiz using only JavaScript with prompt() and alert(). This was an answer for an assignment I was doing and I was also looking for help but everyone was saying to use HTML, this was my final stop and I figure out how to do it myself. Very simple.
let question1 = prompt('Who sings shining bright like a diamond?').toLowerCase()
console.log(question1)
let answer1 = 'rihanna'
let affirm1 = 'You are correct!'
let rebuttal1 = 'You are wrong. It is Rihanna'
if (question1 == answer1) {
alert(affirm1)
console.log(affirm1)
} else {
alert(rebuttal1)
console.log(rebuttal1)
}
let question2 = prompt('Who sings "Gods Plan" ?').toLowerCase()
console.log(question2)
let answer2 = 'drake'
let affirm2 = 'You got it right!'
let rebuttal2 = 'Nope! The answer is Drake.'
if (question2 == answer2) {
alert(affirm2)
console.log(affirm2)
} else {
alert(rebuttal2)
console.log(rebuttal2)
}
let question3 = prompt('Who sings "Anaconda" ?').toLowerCase()
console.log(question3)
let answer3 = 'nicki minaj'
let affirm3 = 'Yup! You got it!'
let rebuttal3 = "Wow! You really didn't know it was Nicki Minaj"
if (question3 == answer3) {
alert(affirm3)
console.log(affirm3)
} else {
alert(rebuttal3)
console.log(rebuttal3)
}
Upvotes: 0
Reputation: 1
I like to use this code, because it's nice and simple. anyways, here's the code:
var q; function w(e,r){while(q!=r){q=prompt(e);};}; w("question", "answer");
If you just add a ;
and then w("whatever your question is", "whatever your answer is")
at the end, it will add another question. but DO NOT forget the comma between the question and answer.
But if you want to choose what you want it to do, then you use this:
var x=prompt("what is 1+1?"); if(x=="2"){alert("yep!")} else{alert("no.")};
Upvotes: 0
Reputation: 1347
Your question is very broad as there are always a million ways to design something, but I'll give you the five-minute answer.
Use a data structure to hold your question / answer combinations, like an array of objects. An array makes sense if you want to keep the user along a "path". You simply iterate through the array to ask questions.
var questions = [{
question: 'Are you happy?',
answer: 'yes',
affirm: 'Yay! You got it right!',
rebuttal: 'Nope, you are definitely happy.'
},
{
question: 'Are you mad?',
answer: 'no',
affirm: 'Good job!',
rebuttal: 'Not right.'
}];
for (var i = 0, l = questions.length; i < l; i++) {
answer = prompt(questions[i].question);
// I do not support manipulating a loop counter mid-loop, but it's for an example.
if (answer !== questions[i].answer) {
alert(questions[i].rebuttal);
i--;
} else {
alert(questions[i].affirm);
}
}
With your method, which I have to be honest, is pretty primitive, you'd use have to write the choices in the text of the prompt. For multiple answers, you'd have to force the user to delimit their answers somehow (with commas, for example). But honestly, I would abandon the prompt/alert method altogether, and use HTML elements to achieve your goals (assuming this is in a web browser).
It seems other users have addressed your syntax errors.
If many of these concepts are foreign to you, I recommend studying up at Codecademy, as they have excellent courses on Javascript and HTML.
Upvotes: 1
Reputation: 20199
In your question, the curly braces not match. see the where the function braces finished. It is on third question.
Try
function welcome() {
confirm("Welcome! You have chosen to play. You will be presented with a series of questions...");
confirm("If you answer a questions incorrectly, you cannot advance to the next...");
var retVal = prompt("Do you want to continue?");
if (retVal == "yes") {
alert("Good, question 1...");
} else {
alert("Well you're boring");
}
//Question 1
var retVal = prompt("The term jitterbug originally was a slang term for a person who was real heavy on jittersauce. What does jittersauce refer to?");
if (retVal == "alcohol") {
alert("Not bad, next question...");
} else {
alert("FALSE. Please try again. HINT: it makes parties fun...");
}
//Question 2
var retVal = prompt("What early American Broadway show helped popularize swing dance styles, ranging from the Charleston to the Foxtrot?");
if (retVal == "Ziegfeld Follies") {
alert("Dang, pretty impressive. NEXT");
} else {
alert("NOPE. HINT: starts with a Z...");
}
//Question 3
var retVal = prompt("What ballroom style is inspired by the traditional Spanish bullfight?");
if (retVal == "paso doble") {
alert("Very good... your Googling skills are excellent.");
} else {
alert("WRONG. HINT: it's in Spanish...");
}
//Question 4
var retVal = prompt("Ballroom dancing gets its name from what Latin term?");
if (retVal == "ballare") {
alert("More like BALLER... HAH, ok next.");
} else {
alert("Really? Come on. HINT: it's got the word ball in it...");
}
//Question 5
var retVal = prompt("What Latin dance style is the official dance of the Dominican Republic?");
if (retVal == "Merengue") {
alert("Tastes like lemons, time for the final question");
} else {
alert("INCORRECT. HINT: starts with an M and rhymes with a fruit pie...");
}
//Question 6
var retVal = prompt("What Latin dance style is a combination of the Mambo and the Rumba?");
if (retVal == "cha cha") {
alert("YOU WIN YOU WIN YOU WIN... click OK to claim your prize");
} else {
alert("DUR NO. HINT: this is a text message service that will answer any question you text them...");
}
//Prize
var retVal = prompt("This is your prize. Will you accept?");
if (retVal == "yes") {
alert("Good. Comence prizing.");
} else {
alert("Why you no want prize?");
}
}
Upvotes: 0