Use these notes to review a concept or read at your own pace. The interactive workspace above adds predictions, editable code, and actual run results.
IDEA 1
Visit items in a list
An array is an ordered list. A rooms array might contain several room objects. for...of visits each object in order, giving the current object a temporary name such as room.
This loop is bounded by the array's length. Returning from inside it ends the function as soon as a suitable room is found. That implements ‘first suitable room,’ which is a specific selection rule rather than ‘best room.’
Scan without skipping
- First. Maple has 10 seats; compare it with the request.
- Next. Cedar has 20 seats; the same rule is applied again.
- Stop. A return sends the first matching ID to the caller.
Three fictional rooms illustrate the order; only visited entries are inspected.
One name, successive objects
for (const room of input.rooms) {
console.log(room.id);
}Each iteration binds room to the next element. Logging an ID shows traversal, but choosing a room still needs a condition.
Think it through: If the first room passes and the function returns it, how many later rooms are visited?
IDEA 2
Compare values, including exact boundaries
A comparison returns true or false. seats >= people means capacity is at least the requested headcount. seats > people means strictly more seats, so it rejects an exact fit.
The && operator requires both conditions to pass. A room must be available and large enough. The case data uses explicit booleans for availability; do not infer availability from a friendly room name.
Two gates, one choice
- Available?. A false value rejects the room regardless of size.
- Enough seats?. Exactly 20 meets at least 20.
- Both?. Only a room passing both conditions can be returned.
For 20 people, equal capacity satisfies the requirement.
Change one symbol, change the boundary
20 > 20 // false
20 >= 20 // true
19 >= 20 // falseTest below the boundary, exactly at it, and above it. The middle case catches an easy off-by-one-style requirement error.
Think it through: A free room seats 18 and the group has 18 people. Under an ‘at least’ rule?
IDEA 3
Handle an empty result
An empty array has no elements, so the loop body never runs. A nonempty array can also have no suitable room. Both situations need an honest no-match result.
We use null to mean ‘no room selected.’ Returning an invented ID would make the next part of the system act on a room it never found. Put return null after the loop so every candidate gets a chance first.
No match is a valid outcome
- Inspect. A 12-seat room cannot fit 20 people.
- Exhaust. The finite list ends without a passing entry.
- Report. Return the agreed absence value.
A clear absence is more useful than an unsupported recommendation.
Placement changes behavior
for (const room of input.rooms) {
if (room.available && room.seats >= input.people) return room.id;
}
return null;null belongs after the loop. Putting it inside the loop after the first failed room would stop the search too soon.
Think it through: Why put return null after the loop?
Put it into practice
Return the first available room ID with enough seats, or null.
- Read the people count and room order in each case.
- Run the starter to observe its no-match baseline.
- Replace the false condition with the availability and capacity rule.
- Try an exact fit, then an empty room list.
Your next experiment: Make both rooms unavailable. Can you explain null without calling it an error?
Make the selection rule explicit, test equality, and give no match its own honest value.
Key terms
- Array
- An ordered list of values.
- Condition
- An expression whose true/false result controls which code runs.
- null
- An explicit absence value; here it means no room was selected.
Sources and scope
Original Stemtiq teaching, reviewed 2026-09-14. The named researchers and organizations do not endorse this course. Classroom cases are authored exercises, not published findings.
MDN Web Docs: JavaScript Guide
Updated 7 November 2025
Functions, objects, conditions, loops, and asynchronous code provide the programming foundations for an agent controller.
Ordinary JavaScript rules do not imply that a language model is running. An execution environment may support only part of the language or its host APIs.