Skip to content

Come Back Later: Review Challenges

The Course
•
20 min study

Finishing a page is different from remembering how to solve a problem. Come back after roughly a week, and again a few weeks later. Those intervals are a starting suggestion, not an optimal schedule. Try each task without reopening the relevant lesson first, then use the links in the sidebar to review what you missed.

After about a week

  1. Predict what "10" + 5 produces and explain how to get 15 instead.
  2. Fix a loop that initializes its total inside its body.
  3. Write a function that totals only completed study minutes, including an empty-array check.
  4. Explain why assigning an object to another name does not copy it.

After a few weeks

  1. Build a shopping summary from objects containing name, quantity, and unit price in whole cents. Reject invalid quantities. Use integers for this exercise rather than pretending floating-point arithmetic is exact currency arithmetic.
  2. Add a button that resets a displayed total. Explain which code changes state and which renders it.
  3. Test a boundary, then deliberately introduce a bug that the test catches.

Predict before running

This is a retrieval check. Does an ordinary assignment create a copy? Do not run until you have written your prediction.

Predict, then run JavaScript
const first = { done: false };
const second = first;

second.done = true;
console.log(first.done);
Check your prediction
Text
true

Trace each statement in order. If your prediction differed, describe the first place your mental model and the result diverged before trying again.

Your turn

Exercise

Come Back Later: Review Challenges: practice

Write totalCost(items) for records with a positive whole-number quantity and a whole-number cents price of zero or more. Each record is an object with both fields; the values may be invalid. Ignore invalid records. [{quantity: 2, cents: 150}, {quantity: 1, cents: 80}] should return 380. Empty input returns 0.

Your working space JavaScript
function totalCost(items) {
  // Work from the brief, not the earlier project.
}
// Add your own assertions.
Hint 1: the idea

Validate quantity and cents separately, then multiply only valid records.

Hint 2: a first step

Keep an accumulator outside the loop; use Number.isInteger for both fields.

Show one solution
JavaScript
function totalCost(items) {
  let total = 0;

  for (const item of items) {
    if (
      Number.isInteger(item.quantity) &&
      item.quantity > 0 &&
      Number.isInteger(item.cents) &&
      item.cents >= 0
    ) {
      total = total + item.quantity * item.cents;
    }
  }

  return total;
}

if (totalCost([]) !== 0) {
  throw new Error("empty");
}

if (totalCost([{ quantity: 0, cents: 150 }]) !== 0) {
  throw new Error("invalid");
}

console.log(
  totalCost([
    { quantity: 2, cents: 150 },
    { quantity: 1, cents: 80 }
  ])
);
Text
380

This is a small-input exercise; very large integer totals can exceed JavaScript's safe-integer range. Record which concepts you recalled, which hints you needed, and a new problem to try. Revisit the explanation, then retry with different data.

Before you move on

Explain the main idea in your own words, change one input, and predict the result. If you used the solution, close it and rebuild the exercise once with different values. Use Mark as done to track your place, not to certify mastery.

Ready for the next step?

You can now choose intermediate JavaScript for modules, asynchronous applications, and browser lifecycle work, or beginner TypeScript to add static checking to the JavaScript you already know. TypeScript beginner does not require JavaScript intermediate or advanced. Before either path, rebuild a small array-of-records report, explain a callback's input and return value, and validate text input without opening a solution.

Was this article helpful?

Your response is saved on this device.