8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

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

Martin Kristiansen 1月前

40 0

我正在尝试制作一个危险边缘游戏,我需要将问题添加到表格中。我首先使用 forEach 循环遍历所有类别,然后我想在 forEach 循环内部使用 for 循环...

我正在尝试制作一个危险边缘游戏,我需要将问题添加到表格中。我首先使用 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>
帖子版权声明 1、本帖标题:我的 for 循环没有运行,我不知道为什么。它甚至不会在控制台上打印任何内容
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Martin Kristiansen在本站《for-loop》版块原创发布, 转载请注明出处!
最新回复 (0)
  • 这里有两个问题:

    1.- 正如@Joel Lefkowitz 提到的,您定义了函数 fillTable 从未调用它。 要解决这个问题,只需在您的 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);
    });
    

    您遇到的问题是,您尝试以 category.clues[i].length ,但需要注意的是,索引 \'i\' 不是必需的,因为您已经位于类别内。这就是为什么要获取长度,您只需访问 clues 属性,例如 category.clues.length 或使用 foreach,如代码所示。

  • 非常感谢!我没有调用该函数,因为我要添加一个按钮来启动游戏,然后调用它。我不知道如何使用 forEach 来循环线索,因为我没有使用正确的语法。非常感谢您的帮助。

  • 您需要 fillTable 在脚本末尾调用您的函数:

    ...
    
    
    fillTable()
    
返回
作者最近主题: