• 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

For Loops

Mission: Decision Desk

A loop can visit each value in a list, adding to a total or collecting a new order.

Loops let you repeat a block of code. An array such as [2, 4, 6] is an ordered list; you will study arrays in depth later, but here you only need to visit its values. numbers.length counts entries, numbers[i] reads one entry (starting at index 0), and result.push(value) adds an entry to a new array. To log a list on one line, result.join(",") joins its values with commas.

Traditional for loop

javascript
for (let i = 0; i < 5; i++) {
    console.log(i); // five separate lines: 0 through 4
}

Iterating an array:

javascript
const fruits = ["apple", "banana", "cherry"];
for (let i = 0; i < fruits.length; i++) {
    console.log(fruits[i]);
}

for...of — iterate values

Cleaner syntax when you only need the value, not the index:

javascript
for (const fruit of fruits) {
    console.log(fruit);
}

The fruits list here is the same list from the preceding example. If you copy this block into a new script, declare fruits first.

For the reverse task, start at arr.length - 1 (the last index), step backward with i--, and push each visited value into a new array. An empty array gives a sum of 0 because the loop runs zero times. Object-key iteration comes in the Objects module; it is not needed here.

Your Task

  1. Implement sumArray(numbers) with a for loop returning the sum (including negative numbers and empty arrays). The provided Sum one probe should print Sum one: 2.
  2. Implement reverseArray(arr) with a for loop returning a new reversed array. The provided Reverse one probe should print Reverse one: x.
  3. Log Sums: 15,10,0 from [1,2,3,4,5], [10,-5,5], and [].
  4. Log Reversals: 3,2,1 | c,b,a from [1,2,3] and ["a","b","c"].
  5. Log Sum: 15 from sumArray([1,2,3,4,5]).
Hint 1
1 / 3
HINT 1

A standard for loop: for (let i = 0; i < arr.length; i++).

HINT 2

Use for...of to iterate values directly: for (const item of arr).

HINT 3

Accumulate a sum by adding each element to a total variable.

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