Score Not Incrementing and errors

Discussion in 'JavaScript' started by kadi, Aug 25, 2014.

  1. #1
    My Code Is about +1 score for every correct answer. Only a score of one is achieving for first question1 and when i type the correct answer for question2 score is not incrementing. My final score should be 2 since i have only two questions but whats happening in my code is that if i delete first answer 'yes' score is going to 4 and so on. Dont know where the error in my code is...

    
    <div id="score" style="font: bolder 20px courier">score: 0</div>
    <input type="text" id="question" />
    <input type="text" id="question1" />
    
    <script>
    var answers = {
        'question': 'yes',
         'question1': 'no',
    
    };
    
    var score = 0;
    
    function checkResults() {
    
        var $this = $(this),
            val = $this.val().toLowerCase();
    
        for (var k in answers) {
            if (answers.hasOwnProperty(k)) {
    
                if (k == $this.attr('id') && answers[k] === val) {
                    $this.css('background-color', 'green');
                    score += 1;
    
                    break;
                } else {
                    $this.css('background-color', 'red');
                }
    
            }
        }
    
    
        if (score == 2) {
            alert('Hi Ur Score is 2');
        }
    
    
        $('#score').text('score: ' + score);
    
    }
    
    $('input').on('keyup', checkResults);
    </script>
    
    Code (markup):

     
    kadi, Aug 25, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,824
    Likes Received:
    4,539
    Best Answers:
    123
    Trophy Points:
    665
    #2
    works as you describe in jsfiddle: http://jsfiddle.net/xe1ahj9j/

    I'd recommend rechecking every question, or atleast having an array for each question and if they're right or not and then adding them up. If I get it right, change it to be wrong then I still have a score of 2.
     
    sarahk, Aug 25, 2014 IP
  3. Adam_UK

    Adam_UK Member

    Messages:
    23
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    28
    #3
    I've placed your code in JSFiddle. I've added two HTML <p> elements to display your score variable before your 'for in' loop. I am then appending the current score value into these elements to show the score before and after your user event calls checkResults(). http://jsfiddle.net/asjup1qy/2/

    I've condensed the white space in your code and added comments to highlight what I have done, so please don't mistake that as a recommended way to doing things. It is just to make my input clearer.

    For me, your score increments correctly to two as @sarahk pointed out as well. However, due to the order of your code, the HTML text doesn't update before your alert window. Also, you have no mechanism to reduce the score or to prevent future input changing the score or incrementing it above what you want.

    The solution I suggest, would be to use your checkResults() to loop through the text area questions as an array, tallying them up each time from 0. You would be counting for correct answers each time, with each answer being independent of one another. Then use a function to compare the current score with a maximum score to see if they win.
     
    Adam_UK, Aug 27, 2014 IP