• 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

While Loop

Mission: Decision Desk

Use a condition to decide whether another iteration is needed, and update the value each time.

Use while when you repeat until a condition changes. The countdown task has a known starting value; the later Collatz task repeats until the value reaches 1. Both need an update inside the loop or they would keep running.

while loop

javascript
let count = 0;
while (count < 5) {
    console.log(count);
    count++;
}

The condition is checked before each iteration. If count starts at 5, the loop body never runs.

do...while loop

The condition is checked after each iteration — so the body always runs at least once:

javascript
let num = 0;
do {
    console.log(num);
    num++;
} while (num < 3);

Your Task

  1. Implement countdown(n) using while to return the integers from n to 1 in an array. The provided one-item probe should print Countdown one: 1.
  2. Implement collatz(n) using while to count steps until 1 (even: halve; odd: triple and add 1). The provided one-item probe should print Collatz one: 0.
  3. Log Countdown three: 3,2,1 using countdown(3).
  4. Log Collatz six: 8 using collatz(6).
  5. Log Countdown from 5: 5,4,3,2,1.
Hint 1
1 / 3
HINT 1

A while loop keeps running as long as its condition is true.

HINT 2

Always update the variable inside the loop to avoid infinite loops.

HINT 3

A do-while loop always runs at least once.

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