我的 for 循环没有运行,我不知道为什么。它甚至不会在控制台上打印任何内容

问题描述 投票:0回答:1

我正在尝试制作一个危险游戏,我需要将问题添加到表格中。我首先使用 forEach 循环来循环所有类别,然后我想在 forEach 循环内使用 for 循环来循环所有线索问题。我已经被困在这个问题上有一段时间了。我认为使用 forEach 循环是有意义的,我只是很困惑为什么 for 循环根本不运行。

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>
javascript for-loop foreach
1个回答
0
投票

您需要在脚本末尾调用您的

fillTable
函数:

...


fillTable()
© www.soinside.com 2019 - 2024. All rights reserved.