Turn sample data into a display
Practise extracting one useful value and handling missing data before connecting an API or buying a screen.
You needA browser. The exercise uses labelled sample data and makes no external API requests.
Reference checked 6 September 2026. Authored exercise; no physical build result is claimed.
Understand the change
Start with a saved example
Read the provider documentation and understand one response before wiring it into a device. The example here is deliberately small and invented for learning.
Define failure behaviour
A missing value is not zero. A disconnected device should communicate that its information is unavailable or old rather than showing a believable invented reading.
Plan the physical handoff
A real display needs network handling, suitable refresh frequency, readable formatting and a way to show stale data. Keep private API credentials on a server where the provider requires them.
A focused request to try
Use this with a suitable AI assistant. Its response will vary; compare it with the checks below.
Given the sample JSON {"temperature":18,"unit":"C"}, write a JavaScript function that returns "18 °C". Accept only finite numeric temperatures with unit C. Missing values, null, strings and other units must return "Unavailable". Do not fetch a service or embed API keys. Include tests for each invalid case.Worked reference example
Read the example, predict one change, then check the result.
function displayTemperature(data) {
if (data && typeof data.temperature === "number" &&
Number.isFinite(data.temperature) && data.unit === "C") {
return `${data.temperature} °C`;
}
return "Unavailable";
}Try the sample response
The preview is sample data. It makes no live weather or API request.
Sample data only.
Check your result
- 18 with unit C produces 18 °C.
- 0 is a valid number and must not be treated as missing.
- Remove temperature or change it to a string: the preview must show Unavailable.
Where this can go wrong
A successful request can contain an error object. Check response structure, units, timestamp and provider usage terms. This example validates formatting only, not sensor accuracy or an API integration.
Go to the source
MDN JSON.parse reference. Confirm current tool instructions before using them.