skip to Main Content

I’m trying to make a jeopardy game and I need to add the questions to a table. I’m using a forEach loop to loop over all the categories first and then I want to use a for loop inside the forEach loop to loop over all the clue questions. I’ve been stuck on this for a while. I think having the forEach loop makes sense I’m just so confused as to why the for loop isn’t running at all.

let categories = [
    {title: "Art",
        clues: [
            {question: "Who painted the Sistine chapel ceiling?", answer: "Michelangelo", showing: null},
            {question: "Aureolin is a shade of what color?", answer: "yellow", showing: null},
            {question: "The Parthenon Marbles are controversially located in what museum?", answer: "the British Museum", showing: null},
            {question: "What is the real title of Salvador Dali’s “melting clocks” painting?", answer: "The Persistence of Memory", showing: null},
            {question: "Which art trend became incredibly popular in 2023, although originally first appeared in the 1960s?", answer: "AI art", showing: null},
            {question: "Which of the following is NOT a painting by Salvador Dali; ‘The Temptation of St. Anthony’, ‘The Hallucinogenic Toreador’ or ‘The Sleeping Gypsy’?", answer: "The Sleeping Gypsy", showing: null}
        ],
    },
    {title: "Tech",
        clues: [
            {question: "What app has a green owl as the mascot?", answer: "Duolingo", showing: null},
            {question: "What software company is headquartered in Redmond, Washington?", answer: "Microsoft", showing: null},
            {question: "What software development hosting company has an Octocat for the logo?", answer: "GitHub", showing: null},
            {question: "What programming language is named after a type of Indonesian coffee?", answer: "Java", showing: null},
            {question: "One gigabyte is equal to how many megabytes?", answer: "1000", showing: null},
            {question: "What company made the first portable computer in 1981?", answer: "Osborne Company", showing: null}
        ],
    },
    {title: "Music",
        clues: [
            {question: "What music festival is the most iconic and highly anticipated in the world?", answer: "Lollapalooza", showing: null},
            {question: "Which pop star uses the alias 'Mrs.Doubtfire'", answer: "Sabrina Carpenter", showing: null},
            {question: "Who released the album 'Swimming' in 2018", answer: "Mac Miller", showing: null},
            {question: "Who is the youngest person to headline the Pyramid stage at Glastonbury?", answer: "Billie Eilish", showing: null},
            {question: "Who is the worlds richest pop star?", answer: "Paul McCartney", showing: null},
            {question: "What artist was working at a summer camp as a counsellor just last year but is now a pop sensation?", answer: "", showing: null}
        ],
    },
    {title: "Nature",
        clues: [
            {question: "What is a group of crows called?", answer: "a murder", showing: null},
            {question: "Which planet has the most moons?", answer: "Saturn", showing: null},
            {question: "What is the slowest-moving mammal on earth?", answer: "a sloth", showing: null},
            {question: "Which animal sleeps the least at two hours a day on average?", answer: "an elephant", showing: null},
            {question: "What is the worlds fastest growing plant?", answer: "bamboo", showing: null},
            {question: "What animal are birds descended from?", answer: "dinosaurs", showing: null}
        ],
    },
    {title: "Random",
        clues: [
            {question: "According to Greek mythology, what famed warrior died because he took an arrow to the heel?", answer: "Achilles", showing: null},
            {question: "How many dots appear on a pair of dice?", answer: "42", showing: null},
            {question: "What is the dot over a lowercase letter 'I' called?", answer: "a title", showing: null},
            {question: "Who wrote the famous novel To Kill a Mockingbird?", answer: "Harper Lee", showing: null},
            {question: "How many zip codes are in the US?", answer: "41,642", showing: null},
            {question: "What is the name of the scale used to measure spiciness of peppers?", answer: "Scoville scale", showing: null}
        ],
    },

];

function fillTable() {
    const table = document.querySelector('table');
    const row = table.insertRow();
    categories.forEach( category => {
        const title = row.insertCell(0);
            title.innerHTML = category.title;
        
        for(let i = 0; i < category.clues[i].length; i++){
            console.log(category.clues[i])
            // const clue = row.insertCell 
            // clue.innerHTML = category.clues[i]
        }
    })
}  
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Jeopardy</title>
  <link rel="stylesheet" href="jeopardy.css">
</head>
<body>
    <h1>jeopardy</h1>
    <table>
        
    </table>
<script src="jeopardy.js"></script>

</body>
</html>

2

Answers


  1. You need to call your fillTable function at the end of the script:

    ...
    
    
    fillTable()
    
    Login or Signup to reply.
  2. Here you have 2 problems:

    1.- You defined the function fillTable but never call it, as @Joel Lefkowitz mentioned.
    To solve this just add one line of code to your javascript jeopardy.js.

    function fillTable() {
        const table = document.querySelector('table');
        const row = table.insertRow();
        categories.forEach( category => {
            const title = row.insertCell(0);
                title.innerHTML = category.title;
            
            for(let i = 0; i < category.clues[i].length; i++){
                console.log(category.clues[i])
                // const clue = row.insertCell 
                // clue.innerHTML = category.clues[i]
            }
        })
    }  
    // This will execute your new function
    fillTable();
    

    2.- Your are not looping over the clues correctly.

    Fix the code as follows:

    category.clues.forEach(clue => {
       // Here you can access your question
       console.log(clue.question);
       // Here you can access your answer
       console.log(clue.answer);
       // Here you can access your showing property
       console.log(clue.showing);
    });
    

    The problem you had, is that you tried to access the length property of the clues in this way category.clues[i].length, however it is important to notice that the index "i" is not necessary because you are already inside of a category. That is why to get the length you only need to access the property clues like this category.clues.length or use a foreach as shown in the code.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search