• 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

Recursion

Some data nests inside more data. In script.js, write functions that solve a smaller version of the problem until they reach a stopping case; inspect the Console to check each result.

Recursion is when a function calls itself to solve a smaller version of the same problem. Every recursive solution needs:

  1. Base case — the simplest input that can be answered directly (stops the recursion).
  2. Recursive case — breaks the problem into a smaller sub-problem.

A base case in a nested structure

javascript
function countBoxes(box) {
  if (!box.children) return 1;
  return 1 + box.children.reduce((total, child) => total + countBoxes(child), 0);
}

console.log(countBoxes({ children: [{}, { children: [{}] }] }));

The object with no children stops the calls; each parent counts itself and its children. For a simpler linear example, a sum removes one item at a time:

javascript
function sum(arr) {
    if (arr.length === 0) return 0;
    return arr[0] + sum(arr.slice(1));
}

For power(base, exp), stop at exponent 0; otherwise multiply by the result for exp - 1. For flatten(arr), walk each item: recurse only when that item is an array, and append plain values to the result. An empty array returns an empty result. These are the two distinct base cases you will need below.

Your Task

  1. Complete recursive power(base, exp) for nonnegative integer exponents; exponent zero returns 1.
  2. Complete recursive flatten(arr) to return a flat array, including for empty input.
  3. Log the standalone combined result 2^10 = 1024 | flatten: 1,2,3,4,5.
Hint 1
1 / 3
HINT 1

Every recursive function needs a base case that stops the recursion.

HINT 2

For power, return 1 when exp is zero; otherwise multiply by power(base, exp - 1).

HINT 3

For flatten, visit each item; recurse into arrays and keep ordinary values.

Loading editor…
Press Run to execute your code.
READY
intercourses
javascript