'Request the data form API fail
I created a code to request data from API to use the data to make a flash card
const BASE_API_URL = "https://jservice.io/api/";
const NUM_CATEGORIES = 6;
const NUM_CLUES_PER_CAT = 5;
// categories is the main data structure for the app; it looks like this:
// [
// { title: "Math",
// clues: [
// {question: "2+2", answer: 4, showing: null},
// {question: "1+1", answer: 2, showing: null}
// ...
// ],
// },
// { title: "Literature",
// clues: [
// {question: "Hamlet Author", answer: "Shakespeare", showing: null},
// {question: "Bell Jar Author", answer: "Plath", showing: null},
// ...
// ],
// },
// ...
// ]
let categories = [];
/** Get NUM_CATEGORIES random category from API.
*
* Returns array of category ids
*/
async function getCategoryIds() {
let response = await axios.get(`${BASE_API_URL}categories?count=100`);
let catIds = response.data.map(c => c.id);
return _.sampleSize(catIds, NUM_CATEGORIES)
};
/** Return object with data about a category:
*
* Returns { title: "Math", clues: clue-array }
*
* Where clue-array is:
* [
* {question: "Hamlet Author", answer: "Shakespeare", showing: null},
* {question: "Bell Jar Author", answer: "Plath", showing: null},
* ...
* ]
*/
async function getCategory(catId) {
let response = await axios.get(`${BASE_API_URL}categories?id=${catId}`);
let cat = response.data;
let allClues = cat.clues;
let randomClues = _.sampleSize(allClues, NUM_CLUES_PER_CAT);
let clues = randomClues.map(c => ({
question: c.question,
answer: c.answer,
showing: null,
}));
return {
title: cat.title,
clues
};
}
/** Fill the HTML table#jeopardy with the categories & cells for questions.
*
* - The <thead> should be filled w/a <tr>, and a <td> for each category
* - The <tbody> should be filled w/NUM_QUESTIONS_PER_CAT <tr>s,
* each with a question for each category in a <td>
* (initally, just show a "?" where the question/answer would go.)
*/
async function fillTable() {
$("#jeopardy thead").empty();
let $tr = $("<tr>");
for (let catIdx = 0; catIdx < NUM_CATEGORIES; catIdx++) {
$tr.append($("<th>").text(categories[catIdx].title));
}
$("#jeopardy thead").append($tr);
// add rows for question of each categories
$("#jeopardy tbody").empty();
for (let clueIdx = 0; clueIdx < NUM_CLUES_PER_CAT; clueIdx++) {
let $tr = $("<tr>");
for (let catIdx = 0; catIdx < NUM_CATEGORIES; catIdx++) {
$tr.append($("<td>").attr("id", `${catIdx}-${clueIdx}`).text("?"));
}
$("#jeopardy tbody").append($tr);
}
}
/** Handle clicking on a clue: show the question or answer.
*
* Uses .showing property on clue to determine what to show:
* - if currently null, show question & set .showing to "question"
* - if currently "question", show answer & set .showing to "answer"
* - if currently "answer", ignore click
* */
function handleClick(evt) {
let id = evt.target.id;
let [catId, clueId] = id.split("-");
let clue = categories[catId].clues[clueId];
let msg;
if (!clue.showing) {
msg = clue.question;
clue.showing = "question";
} else if (clue.showing === "question") {
msg = clue.answer;
clue.showing = "answer";
} else {
return
}
// Update text
$(`#${catIdx}-${clueId}`.html(msg));
}
/** Wipe the current Jeopardy board, show the loading spinner,
* and update the button used to fetch data.
*/
function showLoadingView() {
}
/** Remove the loading spinner and update the button used to fetch data. */
function hideLoadingView() {}
/** Start game:
*
* - get random category Ids
* - get data for each category
* - create HTML table
* */
async function setupAndStart() {
let catIds = await getCategoryIds();
categories = [];
for (let catId of catIds) {
categories.push(await getCategory(catId));
}
fillTable();
}
/** On click of start / restart button, set up game. */
$("#restart").on("click", setupAndStart);
/** On page load, add event handler for clicking clues */
$(async function() {
setupAndStart();
$("#jeopardy").on("click", "td", handleClick);
})
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Jeopardy</title>
<link href="https://fonts.googleapis.com/css?family=Copse&display=swap" rel="stylesheet">
<link rel="stylesheet" href="jeopardy.css">
</head>
<div class="container">
<h1>JEOPARDY</h1>
<table id="jeopardy">
<thead>
</thead>
<tbody>
</tbody>
</table>
<button id="restart">Restart Game</button>
</div>
<script src="https://unpkg.com/jquery"></script>
<script src="https://unpkg.com/axios/dist/axios.js"></script>
<script src="https://unpkg.com/lodash"></script>
<script src="jeopardy.js"></script>
I just make a form that can get data form the BASE API URL to get the data
and send data to the table that I create but the data isn't appear.
I used develop tools on google chrome to tested out but it said undefine.
I have no idea what is that code problem. I need some help top figure out the problem to fix.
BASE_API_URL
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
