Reputation: 21
im new to programming and i keep having all questions comeup,i want the math.random to pick 1 random question and ask it and log answer to the console but i keep getting prompted all the questions
var langquestion = prompt('what is the best language?');
var progquestion = prompt('how many years have you been programming?');
var drivequestion = prompt('does veronica drive good?');
var ran = Math.random() * 10;
if (ran < 3) {
if (langquestion === 'javascript') {
console.log('correct');
} else {
console.log('wrong');
}
} else if (ran < 6 && ran > 3) {
if (progquestion < 2) {
console.log('keep working hard');
} else if (progquestion > 2 && progquestion < 5) {
console.log('your a programmer');
} else {
console.log('your the man');
}
} else {
if (drivequestion === 'yes'){
console.log('correct');
} else {
console.log('wrong');
}
};
;
Upvotes: 0
Views: 209
Reputation: 25634
Your problem is that you prompt
multiple times in a row, on the first 3 lines of your code.
Since your questions have different logics to handle the answer, you could structure your code like this:
var questions = [
{
text: 'What is the best language?',
handler: function(answer) {
return answer === 'javascript' ? 'Correct' : 'Wrong';
}
},
{
text: 'How many years have you been programming?',
handler: function(answer) {
if (answer < 2) return 'Keep working hard';
else if (answer < 5) return 'You\'re a programmer';
else return 'You\'re the man';
}
},
{
text: 'Does veronica drive well?',
handler: function(answer) {
return answer === 'yes' ? 'Correct' : 'Wrong';
}
}
];
var rand = Math.floor(Math.random() * questions.length),
question = questions[rand],
answer = prompt(question.text);
console.log(question.handler(answer));
Upvotes: 1