Skip to content

Commit 3517f44

Browse files
committed
Add validation for knight count in API response exercise
1 parent e9d4aea commit 3517f44

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

chapters/exercise-calling-data.qmd

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,47 @@ viewof prettyQueryResultContainer = {
314314
</div>`;
315315
return container;
316316
}
317-
```
317+
```
318+
319+
Having the query result, the only thing left to do is to get the specific information we need. In this case, you can see that the answer is in the same response under the key `total`. Just to validate that we have the same answer, write in the next cell the total number of objects that contain the term 'knight' in the department of Medieval Art.
320+
321+
```{ojs}
322+
323+
viewof knightsTotal = Inputs.number({
324+
label: "Total number of knights",
325+
placeholder: "",
326+
value: 0,
327+
attributes: {
328+
class: "form-control mb-3"
329+
}
330+
});
331+
332+
viewof validationResult = {
333+
const container = html`<div></div>`;
334+
335+
if (!queryResult.status.ok || queryResult.data.Message) {
336+
if (knightsTotal !== 0) { // Only show warning if user has entered a number
337+
container.innerHTML = `
338+
<div class="alert alert-warning">
339+
Please enter a valid query first before submitting your answer!
340+
</div>`;
341+
}
342+
} else if (queryResult.data.total !== undefined) {
343+
if (knightsTotal === queryResult.data.total) {
344+
container.innerHTML = `
345+
<div class="alert alert-success">
346+
Correct! There are ${queryResult.data.total} objects that contain the term 'knight' in the Medieval Art department.
347+
</div>`;
348+
} else {
349+
container.innerHTML = `
350+
<div class="alert alert-danger">
351+
That's not correct. Try again!
352+
</div>`;
353+
}
354+
}
355+
356+
return container;
357+
}
358+
```
359+
360+
Great! Now you know the basics stepts to get data from an API. In the next chapter, we will show you how to get data that can be tabulated and analyzed.

0 commit comments

Comments
 (0)