Loops - Exercise 2

Question 1: Find the Largest Array Number

Return the largest number in a non-empty array using a loop.

Expected output: 8 (print this with console.log)

javascript

function largestLoopNumber(numbers) {
  // Write your solution here
}

console.log(largestLoopNumber([-4,8,3]));

Question 2: Find the Smallest Array Number

Return the smallest number in a non-empty array using a loop.

Expected output: 3 (print this with console.log)

javascript

function smallestLoopNumber(numbers) {
  // Write your solution here
}

console.log(smallestLoopNumber([4,8,3]));

Question 3: Count Matching Values

Return the number of array entries strictly equal to the supplied value.

Expected output: 2 (print this with console.log)

javascript

function countLoopMatches(items, wanted) {
  // Write your solution here
}

console.log(countLoopMatches([2,1,2,3], 2));

Question 4: Sum Even Array Values

Return the sum of even integers in the supplied array.

Expected output: 6 (print this with console.log)

javascript

function sumEvenArrayValues(numbers) {
  // Write your solution here
}

console.log(sumEvenArrayValues([1,2,4,5]));

Question 5: Find the First Negative Index

Return the index of the first negative number, or -1 if there is none.

Expected output: 2 (print this with console.log)

javascript

function firstNegativeLoopIndex(numbers) {
  // Write your solution here
}

console.log(firstNegativeLoopIndex([4,0,-3,-1]));

Question 6: Collect Even Array Indices

Return the indices of all even integers in the input array.

Expected output: [1,3] (print this with console.log)

javascript

function indicesOfEvenValues(numbers) {
  // Write your solution here
}

console.log(indicesOfEvenValues([3,4,5,6]));

Question 7: Create Running Totals

Return a new array containing the running total after each input number.

Expected output: [2,5,4] (print this with console.log)

javascript

function createRunningTotals(numbers) {
  // Write your solution here
}

console.log(createRunningTotals([2,3,-1]));

Question 8: Pair Each Item with Its Index

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)

javascript

function pairItemsWithIndices(items) {
  // Write your solution here
}

console.log(pairItemsWithIndices(["a","b"]));

Question 9: Count Adjacent Equal Values

Return how many adjacent pairs in the array are strictly equal.

Expected output: 3 (print this with console.log)

javascript

function countAdjacentEqualPairs(items) {
  // Write your solution here
}

console.log(countAdjacentEqualPairs([1,1,2,2,2]));

Question 10: Find the Last Matching Index

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)

javascript

function lastMatchingLoopIndex(items, wanted) {
  // Write your solution here
}

console.log(lastMatchingLoopIndex(["a","b","a"], "a"));