A function that finds the largest value in an array

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

A function that finds the largest value in an array

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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];
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; }
3. True or false: after the loop has compared every item, the function can return largest.
A False
B True
4. Given nums = [4, 9, 2], what is largest just after let largest = nums[0];?
A 4
B 9
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++)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1let largest = nums[0]; AReplaces the current largest value when a bigger item is found in the array.
2if (nums[i] > largest) { largest = nums[i]; } Bif (nums[i] < smallest) { smallest = nums[i]; }
3After the loop has compared every item, the function returns the largest value it found. CThe return is inside the loop, so the function stops after the first comparison. Move it after the loop closing brace.
4To find the smallest value, start with the first item and replace it when a smaller item is found. DStarts by treating the first item in the array as the largest value so far.
5let largest = 0; for (let i = 1; i < nums.length; i++) { if (nums[i] > largest) { largest = nums[i]; } } EStarting at 0 fails when every number is negative, because 0 would wrongly stay as the largest. Start with nums[0] instead.
6for (let i = 1; i < nums.length; i++) { if (nums[i] > largest) { largest = nums[i]; } return largest; } Freturn largest;

Definitions

let largest = nums[0];

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
let largest = nums[_];
2.
if (nums[i] _ largest) { largest = nums[i]; }
3.
for (let i = _; i < nums.length; i++) {
4.
} return _______; }
5.
let smallest = nums[0]; if (nums[i] _ smallest) { smallest = nums[i];

Spot the Error

Shade in the letter of the line that contains an 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);
Question 2
A let min = nums[0];
B for (let n in nums) {
C if (n < min) min = n;
D }
E console.log(min);
Question 3
A let smallest = nums[0];
B for (let n of nums) {
C if (n > smallest) {
D smallest = n;
E }
F }
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);
Question 5
A let largest = nums[0];
B for (let n of nums) {
C if (n > largest) {
D largest == n;
E }
F }

Answer Key

A function that finds the largest value in an array
Section A: Quick Questions
Q1
B — let largest = nums[0];
Q2
A — if (nums[i] > largest) { largest = nums[i]; }
Q3
B — True
Q4
A — 4
Q5
A — for (let i = 1; i < nums.length; i++)
Section B: Match the Pairs
Q1
let largest = nums[0]; → Starts by treating the first item in the array as the largest value so far.
Q2
if (nums[i] > largest) { largest = nums[i]; } → Replaces the current largest value when a bigger item is found in the array.
Q3
After the loop has compared every item, the function returns the largest value it found. → return largest;
Q4
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]; }
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
let largest = nums[0];
Q2
if (nums[i] > largest) { largest = nums[i]; }
Q3
for (let i = 1; i < nums.length; i++) {
Q4
} return largest; }
Q5
let smallest = nums[0]; if (nums[i] < smallest) { smallest = nums[i];
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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];
Q5
D — == compares two values and does not change anything. To replace the largest value we need the assignment operator: largest = n;