← ACARA Digital Technologies Years 9–10

A function that finds the largest value in an array

Start by treating the first array item as the largest, compare every other item, replace it when a bigger one is found, then return it. Smallest works the same way.

Understanding A function that finds the largest value in an array in ACARA Digital Technologies Years 9–10

What it does: The return goes after the loop finishes so the answer covers every item, not just the first few. Swapping > for < makes the same pattern track the smallest value instead of the largest. largest must hold the item's value, nums[i], not its index i. Clear names make code easier to read. largest says what the variable holds, while smallest would be misleading. The > comparison checks each item against the largest so far and updates it only when the item is bigger. Returning inside the loop ends the function early, so most items are never compared.

Common mistakes: The comparison is the wrong way round. With < the code replaces max when it finds a smaller item, so it finds the smallest. To find the largest it should be n > max. for...in loops over the index positions (as strings like "0", "1"), not the values. Use for...of to get each item, so n is compared properly with min.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 710–727) and word search (Modules 710–742).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line correctly starts a function that finds the largest value in the array nums?
let largest = nums[0];let largest = 0;
Matches
let largest = 0; for (let i = 1; i < nums.length; i++) { if (nums[i] > largest) { largest = nums[i]; } }
↓
Starting at 0 fails when every number is negative, because 0 would wrongly stay as the largest. Start with nums[0] instead.
Applying
Meanings
Term
let largest = nums[0];
Meaning
Start by treating the first array item as the largest, compare every other item, replace it when a bigger one is found, then return it. Smallest works the same way.
Practice
function findLargest(nums) {
Testing
Gaps
let largest = nums[];
Check Answer
Spots
2for (let n of nums) {
3 if (n < max) max = n;

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Anglerfish' badge — Deep Diver, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line correctly starts a function that finds the largest value in the array nums?
A. let largest = 0;
B. let largest = nums[0]; ✓
Starting at nums[0] means the answer is always a real item from the array. Starting at 0 breaks when every number is negative.
2. Which condition replaces largest when a bigger item is found?
A. if (nums[i] > largest) { largest = nums[i]; } ✓
B. if (nums[i] > largest) { largest = i; }
largest must hold the item's value, nums[i], not its index i.
3. True or false: after the loop has compared every item, the function can return largest.
A. False
B. True ✓
Once every item has been compared, largest holds the biggest value, so it is safe to return it.
4. Given nums = [4, 9, 2], what is largest just after let largest = nums[0];?
A. 4 ✓
B. 9
At the start largest is only the first item, 4. It becomes 9 later, when the loop finds a bigger value.
5. Which loop start is the better choice after setting largest = nums[0]?
A. for (let i = 1; i < nums.length; i++) ✓
B. for (let i = 0; i <= nums.length; i++)
Start at i = 1 because item 0 is already in largest. The condition i <= nums.length reads past the end of the array and gives undefined.

Match the Pairs

let largest = nums[0]; → Starts by treating the first item in the array as the largest value so far.
if (nums[i] > largest) { largest = nums[i]; } → Replaces the current largest value when a bigger item is found in the array.
After the loop has compared every item, the function returns the largest value it found. → return largest;
To find the smallest value, start with the first item and replace it when a smaller item is found. → if (nums[i] < smallest) { smallest = nums[i]; }
let largest = 0; for (let i = 1; i < nums.length; i++) { if (nums[i] > largest) { largest = nums[i]; } } → Starting at 0 fails when every number is negative, because 0 would wrongly stay as the largest. Start with nums[0] instead.
for (let i = 1; i < nums.length; i++) { if (nums[i] > largest) { largest = nums[i]; } return largest; } → The return is inside the loop, so the function stops after the first comparison. Move it after the loop closing brace.

Key Term

let largest = nums[0];
Start by treating the first array item as the largest, compare every other item, replace it when a bigger one is found, then return it. Smallest works the same way.

Fill in the Blanks

1. let largest = nums[0];
Hint: Type the index of the first item in the array, used as the starting largest value.
2. if (nums[i] > largest) { largest = nums[i]; }
Hint: Type the comparison operator that checks whether the current item is bigger than the largest so far.
3. for (let i = 1; i < nums.length; i++) {
Hint: Type the index where the loop should start, because the first item is already stored.
4. } return largest; }
Hint: Type the variable that holds the biggest value found after the loop ends.
5. let smallest = nums[0]; if (nums[i] < smallest) { smallest = nums[i];
Hint: Type the comparison operator that finds an item smaller than the current smallest value.

Spot the Error

Question 1
A let max = nums[0];
B for (let n of nums) {
C if (n < max) max = n;
D }
E console.log(max);
Line C — The comparison is the wrong way round. With < the code replaces max when it finds a smaller item, so it finds the smallest. To find the largest it should be n > max.
Question 2
A let min = nums[0];
B for (let n in nums) {
C if (n < min) min = n;
D }
E console.log(min);
Line B — for...in loops over the index positions (as strings like "0", "1"), not the values. Use for...of to get each item, so n is compared properly with min.
Question 3
A let smallest = nums[0];
B for (let n of nums) {
C if (n > smallest) {
D smallest = n;
E }
F }
Line C — To find the smallest value, replace it when a smaller item is found. The test should be n < smallest. Using > would end up keeping the largest value instead.
Question 4
A const nums = [-8, -3, -5];
B let max = 0;
C for (let n of nums) {
D if (n > max) max = n;
E }
F console.log(max);
Line B — The first item of the array should be the starting largest value, not 0. With all negative numbers nothing beats 0, so it wrongly prints 0. It should be let max = nums[0];
Question 5
A let largest = nums[0];
B for (let n of nums) {
C if (n > largest) {
D largest == n;
E }
F }
Line D — == compares two values and does not change anything. To replace the largest value we need the assignment operator: largest = n;

Student with a class code? Join your class →