Solution Review: Average of Numbers
This review provides a detailed analysis of the solution to find the average of numbers.
We'll cover the following...
Solution: Using Recursion
Press + to interact
function average(testVariable, currentIndex = 0) {// Base Caseif (currentIndex == testVariable.length - 1) {return testVariable[currentIndex]}// Recursive case1// When currentIndex is 0if (currentIndex == 0) {return ((testVariable[currentIndex] + average(testVariable, currentIndex + 1)) / testVariable.length)}// Recursive case2// Compute sumreturn (testVariable[currentIndex] + average(testVariable, currentIndex + 1))}// Driver codearray = [10, 2, 3, 4, 8, 0]console.log(average(array))
Explanation
The average of an array containing only ...
Access this course and 1400+ top-rated courses and projects.