3 Replies - 549 Views - Last Post: 30 May 2019 - 12:04 PM

#1 tarun1980   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-May 19

How to call function second time?

Posted 23 May 2019 - 02:13 PM

I have coding of a quiz with an array which has questions and options. First time the function is called successfully but when restarting the quiz, the function is not invoked. This is the code which is not working:
    $(document).on('click', '#restart', function() {

        $(this).fadeOut();

        $('#holder').fadeIn();

        showQuestions();


    });


   The whole coding is:

   // Showing questions and options 

    var x = "";

    var i = 0;


    function showQuestions() {

        document.getElementById('holder').innerHTML = x;

        for (j = 0; j < words[i].options.length; j++) {

            if (i < words.length) {

                $('#holder').append("<div id=" + words[i].options[j] + " class='alternatives'>" + words[i].options[j] + "</div>");

            }

        }


        if (i < words.length) {

            $('#holder').append('<div id="quest">' + words[i].question + '</div>');

            i++;

        }

    }


    showQuestions();



    // Calling function on click of alternatives


    $('#holder').on('click', '.alternatives', function() {

        showQuestions();


    });


    // Checking answers

    score = 0;

    k = 0;


    $(document).on('click', '.alternatives', function() {

        let guess = this.id;


        if (k < words.length) {

            var correct = words[k].answer;

            k++;

        }


        if (guess === correct) {

            score++;

        }

        if (score <= words.length) {

            z = score;

            document.getElementById('summary').innerHTML = z;

        }


        if (k >= words.length) {

            $('#holder').fadeOut();

            $('#restart').fadeIn();


        }


    });



    // Restarting Quiz


    $(document).on('click', '#restart', function() {

        $(this).fadeOut();

        $('#holder').fadeIn();

        showQuestions();


    });
:code:

This post has been edited by modi123_1: 23 May 2019 - 02:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to call function second time?

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2334
  • View blog
  • Posts: 7,113
  • Joined: 15-January 14

Re: How to call function second time?

Posted 23 May 2019 - 03:07 PM

You don't see the restart button fade out and the holder fade in? Are you removing that click handler somewhere?
Was This Post Helpful? 0
  • +
  • -

#3 tarun1980   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-May 19

Re: How to call function second time?

Posted 30 May 2019 - 11:52 AM

// Showing alphabets in separate divs

    var x = "";
    var i = 0;
    function showQuestions() {
        document.getElementById('questionDiv').innerHTML = x;
        for (j = 0; j < jumbled[i].letters.length; j++) {
            if (i < jumbled.length) {
                $('#questionDiv').append("<div id=" + jumbled[i].letters[j] + " class='letter'>" + jumbled[i].letters[j] + "</div>");
            }
        }


        // Showing Question one at a time

        if (i < jumbled.length) {
            $('#questionDiv').append('<div id="query">' + jumbled[i].question + '</div>');
            i++;
        }
        $('.box').hide();
        count = 0;
    }
    showQuestions();


    // Creating multiple divs in answerDiv

    function createDivs() {
        for (k = 0; k <= 8; k++) {
            var div = document.createElement('div');
            div.className = "box";
            document.getElementById('answerDiv').appendChild(div);
        }
    }
    createDivs();

    // Click to move alphabet to box div

    guessArray = [];
    count = 0;
    $(document).on('click', '.letter', hello);
    function hello()
    {
        $('.box:eq(' + count + ')').html($(this).text());
        $(this).html('');
        $(this).prop('disabled', true);
        $('.box:eq(' + count + ')').show();
        guess = $('.box:eq(' + count + ')').html();
        guessArray.push(guess);
        newArray = guessArray.join('');
        count++;
    }

    // Checking Answers

    n = 0;
    score = 0;
    $(document).on('click', '#btn', world);
    function world() {
        if (n < jumbled.length) {
            correct = jumbled[n].answer;
            n++;
        }
        if (newArray === correct) {
            score++;
        }
        if (score <= jumbled.length) {
            z = score;
           document.getElementById('score').innerHTML = z;
        }
        if (n >= jumbled.length) {
            $('#summary').fadeIn();
            $('#questionDiv').fadeOut();
            $('#answerDiv').fadeOut();
            $('#score').fadeOut();
            $('#btn').fadeOut();
            $('#summary p').text("Your Score: " + score + " out of " + jumbled.length + " correct ");
        }
        guessArray = [];
    }


    // Restarting Quiz

    $(document).on('click', '#summary a', restart);
    function restart()
    {
        i = 0;
        k = 0;
        n = 0;
        score = 0;
        $('#summary').fadeOut();
        $('#questionDiv').fadeIn();
        $('#answerDiv').empty();
        $('#answerDiv').fadeIn();
        $('#score').fadeIn();
        $('#btn').fadeIn();
        showQuestions();
        hello();
        world();
        createDivs();
    }
:code:

This post has been edited by modi123_1: 30 May 2019 - 11:56 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: How to call function second time?

Posted 30 May 2019 - 12:04 PM

@tarun1980 - please remember to use the 'code tag' button in the editor as well as add information and text to your posts versus just a dump of code and no explanation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1