代码之家  ›  专栏  ›  技术社区  ›  puboot

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

  •  -1
  • puboot  · 技术社区  · 9 月前

    我试图做一个危险游戏,我需要把问题加到桌子上。我首先使用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>
    
    2 回复  |  直到 9 月前
        1
  •  0
  •   Eazael    9 月前

    这里有两个问题:

    1.-您定义了函数 fillTable 但是 永远不要叫它 正如@Joel Lefkowitz所提到的。 要解决这个问题,只需在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.-你没有正确地循环线索。

    按如下方式修复代码:

    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);
    });
    

    你遇到的问题是,你试图以这种方式访问线索的长度属性 类别.线索[i].长度 然而,值得注意的是,索引“i”不是必需的,因为您已经在某个类别中。这就是为什么要获得长度,你只需要访问这样的属性线索 类别.线索.长度 或者使用代码中所示的foreach。

        2
  •  0
  •   Joel Lefkowitz    9 月前

    你需要打电话给你的 fillTable 脚本末尾的函数:

    ...
    
    
    fillTable()