How can I access only two elements of this array


I want to access the question nd answe
I tried with

Var allQuestion = [
question: “Who is the president of America”,
answers: [“Obama”, “Trump”, “Clinton” ],
correctAnswer: “Kendrick”


are you sure you posted correct code?

there is a { on line 2:

Var allQuestion = [
{ // this one here
question: “Who is the president of America”,
answers: [“Obama”, “Trump”, “Clinton” ],
correctAnswer: “Kendrick”

but it has no matching } to close it, without being sure about what you used (arrays and objects) its really difficult to say how to access it


Yeah. Thanks. I made the corrections.
I only want to access the two elements [question] and [answers]


allQuestion is an array, so first we have access by index to access the object.

so you get allQuestion[0], then we can use the property name (full stop notation) to get the value, or if you want to use the associative array notation (square brackets) the key should be of type string

you will need to two look-ups, we can only access one key at a time, you can’t retrieve question and answers in a single line of code


Thank @stetim94 i’m able to fix it out with

console.log(allQuestions[index].question, allQuestions[index].anwsers);

and this the output.
im now tryin to print them on them in a form of a radio buttom with the answer .

Do you have any idea out


then you have to abandon the console and focus on webpage. Sure, its possible. What have you got so far?


@stetim94 im stuck for days now to print on the web


What have you got so far?


        <div id="question">HipHop Quiz</div>
        <button type="button" onclick="render()">New Quiz</button>
        <div id="showQuiz"></div>
                 const allQuestions = [
                        question : "Which of them is from compton", 
                        anwsers: ["Nas", "Tupac", "Omarion", "Kendick"], 
                        correctAnswer: "Kendrick"
                        question: "founder of HipHop",
                        anwsers: ["Tupac", "Eazy E", "Kendick", "Bambata"],
                        correctAnswer: "Bambata"
                    {    question: "Who won BET Hip Hop Album 2018", 
                        anwsers: ["Kendrick", "Bruno", "Jay Z", "Drake"], 
                        correctAnswer: "Kendrick",
                        question: "Best Female HipHop Art 2018",
                        anwsers: ["Azelia", "Nicki", "Cardi_b", "Mama Rap" ],
                        correctAnswer: "Nicki"
                     function render() {
                            var index, unique;
                            var print = '<ul>';
                            for (i = 0; i < allQuestions.length; i++) {
                                index = Math.floor(Math.random() * allQuestions.length);
                        var questionHTML = "<P>" + allQuestions[question][0] + "</P>";        

                        console.log(allQuestions[index].question, allQuestions[index].anwsers);
                            unique = allQuestions.splice(index);
                            //document.getElementById('showQuiz').innerHTML = 'Answer all ';
                    document.getElementById('showQuiz').innerHTML = render();

           } // what is this } doing here?

What is the purpose of your for loop?


var questionHTML = "<P>" + allQuestions[question][0] + "</P>";

allQuestions is an array, so you will need to access by index first:


this will give you access to the object. here you can access properties of object:


or if you prefer to use square brackets:


but for this to work, the key (question) needs to be a string.


Okay …
The function of the loop is to randomize the question in the array.

Over there i want to load the $question and $answer only in the browser…

with this


that is currently not happening, what help do you need from me with this? Currently your loop generates random indexes several times, only remembering the last value you assign to index variable

like i try to explain in my previous answer, allQuestions[0][question] won’t work. You can access the question by property:


or by key:


but you can’t combine property and key like you did:

allQuestions[0][question] // question is undefined


Thanks Sir
I want to loop throuhgh the question without repeatin and print the question and answers on the browser…

I lacking oo much now


Did you figure it out now?


Not yet many trials an yet nop but I won’t stop till I get it.

will need more guidelines.

You are great Sir


What do you need guidance with?

Running someone through a trail and error campaign is exhausting, what is it you need explained?


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.