Loops - Exercise 2
Question 1: Find the Largest Array Number Completed
Return the largest number in a non-empty array using a loop.
Expected output: 8 (print this with console.log)
function largestLoopNumber(numbers) {
// Write your solution here
}
console.log(largestLoopNumber([-4,8,3]));function largestLoopNumber(numbers) {
let largest = numbers[0];
for (const number of numbers) if (number > largest) largest = number;
return largest;
}
console.log(largestLoopNumber([-4,8,3]));Question 2: Find the Smallest Array Number Completed
Return the smallest number in a non-empty array using a loop.
Expected output: 3 (print this with console.log)
function smallestLoopNumber(numbers) {
// Write your solution here
}
console.log(smallestLoopNumber([4,8,3]));function smallestLoopNumber(numbers) {
let smallest = numbers[0];
for (const number of numbers) if (number < smallest) smallest = number;
return smallest;
}
console.log(smallestLoopNumber([4,8,3]));Question 3: Count Matching Values Completed
Return the number of array entries strictly equal to the supplied value.
Expected output: 2 (print this with console.log)
function countLoopMatches(items, wanted) {
// Write your solution here
}
console.log(countLoopMatches([2,1,2,3], 2));function countLoopMatches(items, wanted) {
let count = 0;
for (const item of items) if (item === wanted) count++;
return count;
}
console.log(countLoopMatches([2,1,2,3], 2));Question 4: Sum Even Array Values Completed
Return the sum of even integers in the supplied array.
Expected output: 6 (print this with console.log)
function sumEvenArrayValues(numbers) {
// Write your solution here
}
console.log(sumEvenArrayValues([1,2,4,5]));function sumEvenArrayValues(numbers) {
let total = 0;
for (const number of numbers) if (number % 2 === 0) total += number;
return total;
}
console.log(sumEvenArrayValues([1,2,4,5]));Question 5: Find the First Negative Index Completed
Return the index of the first negative number, or -1 if there is none.
Expected output: 2 (print this with console.log)
function firstNegativeLoopIndex(numbers) {
// Write your solution here
}
console.log(firstNegativeLoopIndex([4,0,-3,-1]));function firstNegativeLoopIndex(numbers) {
for (let i = 0; i < numbers.length; i++) if (numbers[i] < 0) return i;
return -1;
}
console.log(firstNegativeLoopIndex([4,0,-3,-1]));Question 6: Collect Even Array Indices Completed
Return the indices of all even integers in the input array.
Expected output: [1,3] (print this with console.log)
function indicesOfEvenValues(numbers) {
// Write your solution here
}
console.log(indicesOfEvenValues([3,4,5,6]));function indicesOfEvenValues(numbers) {
const indices = [];
for (let i = 0; i < numbers.length; i++) if (numbers[i] % 2 === 0) indices.push(i);
return indices;
}
console.log(indicesOfEvenValues([3,4,5,6]));Question 7: Create Running Totals Completed
Return a new array containing the running total after each input number.
Expected output: [2,5,4] (print this with console.log)
function createRunningTotals(numbers) {
// Write your solution here
}
console.log(createRunningTotals([2,3,-1]));function createRunningTotals(numbers) {
const totals = [];
let total = 0;
for (const number of numbers) { total += number; totals.push(total); }
return totals;
}
console.log(createRunningTotals([2,3,-1]));Question 8: Pair Each Item with Its Index Completed
Return an array of two-item arrays containing each input item and its zero-based index.
Expected output: [["a",0],["b",1]] (print this with console.log)
function pairItemsWithIndices(items) {
// Write your solution here
}
console.log(pairItemsWithIndices(["a","b"]));function pairItemsWithIndices(items) {
const result = [];
for (let i = 0; i < items.length; i++) result.push([items[i], i]);
return result;
}
console.log(pairItemsWithIndices(["a","b"]));Question 9: Count Adjacent Equal Values Completed
Return how many adjacent pairs in the array are strictly equal.
Expected output: 3 (print this with console.log)
function countAdjacentEqualPairs(items) {
// Write your solution here
}
console.log(countAdjacentEqualPairs([1,1,2,2,2]));function countAdjacentEqualPairs(items) {
let count = 0;
for (let i = 1; i < items.length; i++) if (items[i] === items[i - 1]) count++;
return count;
}
console.log(countAdjacentEqualPairs([1,1,2,2,2]));Question 10: Find the Last Matching Index Completed
Using a loop, return the last index strictly equal to the supplied value, or -1 when absent.
Expected output: 2 (print this with console.log)
function lastMatchingLoopIndex(items, wanted) {
// Write your solution here
}
console.log(lastMatchingLoopIndex(["a","b","a"], "a"));function lastMatchingLoopIndex(items, wanted) {
for (let i = items.length - 1; i >= 0; i--) if (items[i] === wanted) return i;
return -1;
}
console.log(lastMatchingLoopIndex(["a","b","a"], "a"));