Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (a1234):

So I have this code to create a quiz in prompt boxes. However, in this code, there is only one response when the user enters a wrong answer: "Incorrect." How can I set different responses to a user's wrong answers in different questions (like Incorrect2, Incorrect3, etc?).

OpenStudy (rsmith6559):

Depending on how complicated you want to get: make each question an object. Technically, JS objects only have to be a collection of attributes like answer, prompt, incorrect1, incorrect2 and incorrect3. Have the else clause call a function that can decide which of the four responses is appropriate. You'd probably have to pass the question object around. If you want to get fancy, the evaluation function could be a method of the question object. If you really want to go overboard, each question object could have a specific evaluation function.

OpenStudy (cruffo):

If you prompt right after the check, could you just keep a counter for incorrect (like you are doing for correct). So instead of }else{ alert("Incorrect."); } do something like }else { numIncorrect++; alert("Incorrect" + numIncorrect);

OpenStudy (theeric):

Hi! If you want to change the code as little as possible, keep going with that function. If you add a parameter for some text that you want to display on incorrect, then you can do something like ``` check(prompt('What is the value of x in 4x = 20?'), 'Sorry, 4x = 20 => x = 20/4 => x = 5'); ``` What do you think of that? I trust you can modify the 'check' function, but others can help if you ask! Anyway.. I like to use objects, too. But that was mentioned. If you keep going with just the function, then you can have another parameter for correct responses, too. You can even get flexible by having sets of responses by having array parameters. That way the user can click a button to indicate if they want the answer given to them if they get it wrong; or maybe they prefer not to know the answer so that they can figure it out on their own. Side note: In the switch you have different alerts! Some say they're out of 7, some out of 5. I assume you're in the middle of a code change, but just wanted to give you a heads up. Also, you can make that feature more flexible by creating the string dynamically, like ``` alert( ['You got ', correct, '/7 answers correct'].join('') ); ``` Saves you from typing more 7's or 5's. Granted, it looks like you want to say 'one answer correct,' but you can say that conditionally when `correct == 0`.

OpenStudy (nox99):

Hi you need add some metadata in the code. What's means this ?? Include a new array with some values for any question and response. Wrong[0] = "nice try"; .... With this array set a different responses values for any input.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!