Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5846405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:34:19+00:00 2026-05-22T12:34:19+00:00

I’m making a simple quiz application which has multiple questions. It has no back

  • 0

I’m making a simple quiz application which has multiple questions. It has no back end and all of the interaction happens on one page. my objects are laid out as follows –

q1Choices = {
    "question": "some question",
    "resultsToRemoveIfAnswer1": [2, 4, 6],
    "resultsToRemoveIfAnswer2": [1, 3, 5]
 };
 q2Choices = {
     "question": "another question",
     "resultsToRemoveIfAnswer1": [5, 6],
     "resultsToRemoveIfAnswer2": [1, 2, 3, 4]
 };
 q3Choices = {
     "question": "a third question",
     "resultsToRemoveIfAnswer1": [3, 4],
     "resultsToRemoveIfAnswer2": [1, 2]
 };

One question appears at a time, which has two possible answers – when an answer is clicked another question appears and is populated from the corresponding object.

To allow for this, I have a counter variable and an array with the objects

var currentQuestion = 0;

var questionArray = [q1Choices, q2Choices, q3Choices];

I can see there is a lot of repetition in this code and was looking for a cleaner, more maintainable, more OO way of doing it.

Can anyone give me any pointers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:34:20+00:00Added an answer on May 22, 2026 at 12:34 pm

    Why don’t you use arrays only?

    questions = [
        [
            "some question", //question
            [2,4,6], //resultsToRemoveIfAnswer1
            [1, 3, 5] //resultsToRemoveIfAnswer2
        ],
        [
            "another question",
            [5, 6],
            [1, 2, 3, 4]
        ],
        ...
    ]
    

    then, in questions items, you can access question key as index 0, resultsToRemoveIfAnswer1 as index 1, resultsToRemoveIfAnswer2 as index 2.

    If you don’t want to rely on meaningless numeric indexes declare constants and use them

    resultsToRemoveIfAnswer1 = 1
    questions[0][resultsToRemoveIfAnswer1]
    

    More OO oriented sample:

    function Questionary(){
        const question = 0
        const resultsToRemoveIfAnswer1 = 1
        const resultsToRemoveIfAnswer2 = 2
        this.questions = [
            [
                "some question",
                [2,4,6],
                [1, 3, 5]
            ],
            [
                "another question",
                [5, 6],
                [1, 2, 3, 4]
            ],
            ...
        ];
        this.currentQuestion = 0;
    }
    
    //method example
    Questionary.prototype.skipQuestion = function() {
        return ++this.currentQuestion;
    }
    

    and you can go deeper adding a Question Object with text, resultsToRemoveIfAnswer1, resultsToRemoveIfAnswer2 and so on…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.