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.
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.
Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.
Over 100 of them, across seven natural biomes.
Turns their work into revision material ready for Flash Cards and Exam study.
Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.
Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.
Quick Questions
Match the Pairs
Key Term
let largest = nums[0];Fill in the Blanks
Spot the Error
Student with a class code? Join your class →