Challenge 1: Average of Numbers

Given an array of numbers, compute the average of those numbers recursively.

Problem Statement

Implement a function that computes the average of all the numbers in an array.

If we have nn numbers and each number is denoted by aia_i (wherei=0,1,2,...,n1)(where \: i = 0, 1, 2, ..., n-1), the average is the sumsum of the numbers divided by nn. This is represented as follows:

average=1ni=0n1ai=a0+a1+a2++an1n{\displaystyle average={\frac {1}{n}}\sum _{i=0}^{n-1}a_{i}={\frac {a_{0}+a_{1}+a_{2}+\cdots +a_{n-1}}{n}}}

Try solving this problem recursively.

Input

  1. A testVariable containing an array of numbers.
  2. The currentIndex of the array.

Note: The array, testVariable, can’t be empty, i.e., it will always have at least one element.

Output

Average of all numbers in the input array.

Sample Input

testVariable = [10, 2, 3, 4, 8, 0] 
currentIndex = 0

Sample Output

4.5

Try it Yourself

Try this challenge yourself before examining the solution. Good luck!

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.