Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to store a value in the cache object.
Node.js
cache['user'] = [1];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function call instead of a value
Assigning undefined or null
✗ Incorrect
We assign the string 'John' to the cache key 'user' to store the value.
2fill in blank
mediumComplete the code to check if the cache has a stored value for 'user'.
Node.js
if (cache.hasOwnProperty([1])) { console.log('Cache hit'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong key names
Using variables instead of string keys
✗ Incorrect
We check if the cache has the key 'user' to know if the data is cached.
3fill in blank
hardFix the error in the code to retrieve cached data safely.
Node.js
const userData = cache[1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation with a string key
Using parentheses like a function call
✗ Incorrect
Accessing cache with square brackets and the key string retrieves the cached value.
4fill in blank
hardFill both blanks to create a cache check and fallback fetch.
Node.js
const data = cache[1] [2] fetchFromDB();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using && which requires both to be true
Using ?? which only checks for null or undefined
✗ Incorrect
We access cache['item'] and use || to fetch from DB if cache is empty or falsey.
5fill in blank
hardFill all three blanks to implement a simple cache update after fetching data.
Node.js
if (!cache[1]) { cache[2] = fetchData(); } return cache[3];
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for check and assign
Forgetting to assign fetched data to cache
✗ Incorrect
We check if cache['key'] is missing, then store fetched data in cache['key'], and finally return cache['key'].