• in
InterCourses
CoursesBlogs
0
← Introduction to JavaScript
○What is JavaScript?○Your First JavaScript Code○Data Types○Type Checking○Inspect Values in the Console○Inspect the Queue Report○Trace a Faulty Calculation○Repair the Score Calculation✦Module Quiz
○Declaring Variables○let and const in Practice○Operators○Type Conversion○Group and Convert Expense Inputs○Calculate the Event Expense Total○Track a Changing Points Tally○Adjust a Ticket Price✦Module Quiz
○Conditionals○Demo: Combined Conditions Before FizzBuzz○Challenge: FizzBuzz Announcer○Switch Statement○Demo: Switch a Venue Sign○Challenge: Power Loadout○For Loops○While Loop○Break and Continue○Demo: Stop a Ticket Scan○Challenge: Number Guess Referee○Demo: Rainfall Routing Walkthrough○Exercise: Score Batch Report✦Module Quiz
○Defining Functions○Arrow Functions○Scope○Closures○Project - Calculator○Function Expressions○Demo: Ticket Pricing Walkthrough○Exercise: Shipping Labels with Defaults○Demo: Seat Ledger Closure Walkthrough○Exercise: Private Budget Ledger✦Module Quiz
○Arrays○Demo: Student Scoreboard○Challenge: Student Course Scoreboard○Array Mutation Methods○Array Slice and Concat○Array Iteration Methods○Multidimensional Arrays○Destructuring○Array Reverse and Sort○Array Fill○Array Find and Includes○Array Flat and FlatMap○Array Reduce, Some, and Every○Demo: Charging Rack Restock Report●Exercise: Pantry Restock Plan✦Module Quiz
○Object Key-Value Pairs○Student Course Record○Object Methods and Destructuring○Map and Set Collections○Playlist Lookup○Number and BigInt Boundaries○Ticket Ledger○JSON○Math and Date○Date Basics○Demo: Supply Bin Audit○Exercise: Volunteer Shift Audit○Demo: Workshop Attendance Feed○Exercise: Parcel Scan Feed✦Module Quiz
○String Methods○Template Literals○Regular Expressions○Demo: Build a Reminder with Template Literals○Exercise: Format Pantry Pickup Messages○Demo: Validate a Time Slot with Regex○Exercise: Validate Storage Shelf Labels✦Module Quiz
○Higher-Order Functions○Callbacks○map, filter, and reduce○Demo: Student Register with map○Challenge: Student Register○Recursion○Project - Data Transform Pipeline○Demo: Handle an Error-First Callback○Exercise: Report Seat Reservation Results○Demo: Summarize Completed Bike Rides○Exercise: Summarize Delivered Parcels✦Module Quiz
○Demo: A notebook object○OOP Introduction○Demo: Independent workshop passes○Classes○Demo: An inherited notice○Inheritance○Demo: A private seat counter○Encapsulation○Demo: Equipment desk checkout○Project: Library Checkout✦Module Quiz
○Demo: Callback Timing○Exercise: Asynchronous JavaScript○Demo: Promise Rehearsal○Exercise: Promises and Async Await○Demo: Offline API Coordination○Exercise: Async API Simulation○Demo: Partial Results Bulletin○Project: Resilient Awards Feed○Demo: Retry a Local Delivery○Project: Reliable Score Delivery✦Module Quiz

Exercise: Pantry Restock Plan

The charging-rack demo selected understocked racks, put the lowest count first, and added up the missing units. Now make a plan for the event pantry using only arrays in script.js.

For example, a parking kiosk could represent coin tubes as [["quarters", 3], ["dimes", 9]]. With a target of five, its plan would be [["quarters"], 2]: the names to refill, then the total missing coins. Your pantry data below is different.

Worked example

A smaller coin-tube example shows the pair structure and one missing amount:

javascript
const tubes = [["quarters", 3], ["dimes", 9]];
const target = 5;
const low = tubes.filter(([, count]) => count < target);
console.log(low[0][0], target - low[0][1]); // quarters 2

Your Task

  1. Write lowStock(stock, target) to return only [name, count] pairs below the target.
  2. Write orderLowStock(low) to return a new array sorted by ascending count.
  3. Write refillNames(ordered) to extract the names in order.
  4. Write missingUnits(ordered, target) to total each pair's deficit.
  5. Write planRefills(stock, target) to combine those helpers and return [names, missingTotal].
  6. Make planRefills() return [[], 0] when nothing needs refilling.
  7. Keep the input array in its original order after calling planRefills().
  8. Log Tea, Napkins | 6 from planRefills(pantry, 6) on its own Console line.

Arrays complete

You can now turn an ordered stock list into a prioritized, checkable action plan. Take the section quiz, then add named facts with objects.

Hint 1
1 / 2
HINT 1

Each pair is [name, count]; compare pair[1] with target.

HINT 2

Sort a copy with (a, b) => a[1] - b[1], then sum target - pair[1].

Take the Quiz →
Loading editor…
Press Run to execute your code.
READY
intercourses
javascript