1

I am new to JavaScript and decided to create my own JS quiz.

Here's what I have so far:

<script type="text/javascript">
    score = 0;
    var questions = [['whats 2 + 2' , '4'] , ['whats 3 * 3' , 9] , ['whats 2 * 7' , 14] ];

    function askQ(ans){
        var answer =prompt (questions[i] , '');
        if(answer == questions[1]){
            score++;
            alert('Yahooo , ur right');
        } else{
            alert('Brush up ur GK');
        }
    }

    for (var i = 0; i < questions.length ; i++) {
        askQ(questions[i]);
    };
    </script>

The problem is when the question "prompted" to the users screen, the answer is also displayed simultaneousl , eg . for the 1st question this is what appears:

"what is 2+2,4" .. now you see , "4" is the answer

I am sure in order for the 4 not to display I need to do something differently, so I went through a few online snippets of code and it was either too complex for me to understand.

Here is a jsfiddle.

1
  • 2
    questions[i] is an array consisting of two elements. you want to display questions[i][0] in the prompt, and compare the answer to questions[i][1]. Commented Aug 12, 2014 at 17:21

2 Answers 2

2

It should be:

<script type="text/javascript">
  score = 0;
var questions = [['whats 2 + 2' , '4'] , ['whats 3 * 3' , 9] , ['whats 2 * 7' , 14] ];

function askQ(ans){
    var answer =prompt (ans[0] , '');
    if(answer == ans[1]){
        score++;
        alert('Yahooo , ur right');
    } else{
        alert('Brush up ur GK');
    }
}

for (var i = 0; i < questions.length ; i++) {
    askQ(questions[i]);
};
</script>

Here are the main differences: Instead of

var answer =prompt (questions[0] , '');

I did

var answer =prompt (ans[0] , '');

Because you have to actually use the passed argument ans with its first value.

Instead of

if(answer == questions[1]){

I did

if(answer == ans[1]){

For the same reason as above but with its second value.

Sign up to request clarification or add additional context in comments.

1 Comment

What is different between this and OP's code? "Teach a man to fish..."
1

Note you're passing in ans into the function (which conveniently contains the question and answer pair), but you're not actually using that anywhere.

The line:

var answer =prompt (questions[i] , '');

Should probably be:

var answer = prompt(ans[0], ''); // Display the question

And the line:

if(answer == questions[1]){

Should probably be:

if(answer == ans[1]) { // Compare answer to correct answer

Also, it might be better to store all your answers as strings rather than numbers, since you're comparing them to strings entered by the user. ['whats 3 * 3' , 9] should be ['whats 3 * 3' , '9']

A better design also might be to use JavaScript object literals to store the question/answer pairs. Something like:

var questions = [
  {Question: 'whats 2 + 2', Answer: '4'},
  {Question: 'whats 3 * 3', Answer: '9'},
  {Question: 'whats 2 * 7', Answer: '14'}
];

Then, you can change your code to:

var answer = prompt(ans.Question, ''); // Display the question

And:

if(answer == ans.Answer) { // Compare answer to correct answer

This is a bit easier to read.

3 Comments

thats a really intelligent way of doing it mike , i mean the last part of your answer , where you do "var answer = prompt(ans.Question, '');" , and By the way yes , as of now , even if you answer the question "what is 2+2" as 4 , it will mark it as wrong , because from the prompt the value accepted is a "String" whereas whats in the array is a "Int" (Not sure ! but i think thats right) , You suggest Mike that i turn the "Ints" in the array to strings , but what i'd actually like is once the user enters the answer in the prompt , i would like to convert it to a Int .
You can convert their answer to an int using parseInt. You'd want to implement some sort of error checking code in case they don't enter a valid number.
it works now fine , not yet implemented your suggestion of checking if the user enters something other than a number . and it works without using the parseInt . heres the fiddle jsfiddle.net/gautamz07/Lsuhejf1/2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.