DEV Community

Adrian
Adrian

Posted on

What’s your alternative solution? Challenge #11

About this series

This is series of daily JavaScript coding challenges... for both beginners and advanced users.

Each day I’m gone present you a very simple coding challenge, together with the solution. The solution is intentionally written in a didactic way using classic JavaScript syntax in order to be accessible to coders of all levels.

Solutions are designed with increase level of complexity.

Today’s coding challenge

Calculate the average of the numbers in an array of numbers

(scroll down for solution)

Code newbies

If you are a code newbie, try to work on the solution on your own. After you finish it, or if you need help, please consult the provided solution.

Advanced developers

Please provide alternative solutions in the comments below.

You can solve it using functional concepts or solve it using a different algorithm... or just solve it using the latest ES innovations.

By providing a new solution you can show code newbies different ways to solve the same problem.

Solution

// Solution for challenge11

function averageArray(ar)
{
    var n = ar.length;
    var sum = 0;

    for(var i = 0; i < n; i++)
    {
        sum += ar[i];
    }

    return sum / n;
}

var ar = [1, 3, 9, 15, 90];
var avg = averageArray(ar);

println("Average: ", avg);

To quickly verify this solution, copy the code above in this coding editor and press "Run".

Note: The solution was originally designed for codeguppy.com environment, and therefore is making use of println. This is the almost equivalent of console.log in other environments. Please feel free to use your preferred coding playground / environment when implementing your solution.

Top comments (2)

Collapse
 
aminnairi profile image
Amin • Edited

My take at the challenge using a recursive function, immutability and the incremental average algorithm. Also added a generator function to easily generate a generator of numbers in a given range.

"use strict";

function* range( start, stop ) {

    if ( ! Number.isInteger( start ) ) {

        throw new TypeError( "Expected first argument to be an integer." );

    }

    if ( ! Number.isInteger( stop ) ) {

        throw new TypeError( "Expected second argument to be an integer." );

    }

    for ( let value = start; value <= stop; value++ ) {

        yield value;

    }

}

function average( generator, previousAverage = 0, previousIndex = 0 ) {

    if ( Object.prototype.toString.call( generator ) !== "[object Generator]" ) {

        throw new TypeError( "Expected first argument to be a generator." );

    }

    const { value, done } = generator.next();

    if ( done ) {

        return previousAverage;

    }

    if ( ! Number.isInteger( value ) ) {

        throw new TypeError( "Expected first argument to be a generator of integers." );

    }

    const nextIndex     = previousIndex + 1;
    const nextAverage   = ( value - previousAverage ) / nextIndex;

    return average( generator, previousAverage + nextAverage, nextIndex );

}

console.log( average( range( 1, 3 ) ) ); // 2
console.log( average( range( 1, 5 ) ) ); // 3
console.log( average( range( 1, 6 ) ) ); // 3.5
Collapse
 
savagepixie profile image
SavagePixie • Edited
const sum = (x, y) => x + y
const arrayAverage = arr => arr.reduce(sum) / arr.length