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)){thrownewTypeError("Expected first argument to be an integer.");}if(!Number.isInteger(stop)){thrownewTypeError("Expected second argument to be an integer.");}for(letvalue=start;value<=stop;value++){yieldvalue;}}functionaverage(generator,previousAverage=0,previousIndex=0){if(Object.prototype.toString.call(generator)!=="[object Generator]"){thrownewTypeError("Expected first argument to be a generator.");}const{value,done}=generator.next();if(done){returnpreviousAverage;}if(!Number.isInteger(value)){thrownewTypeError("Expected first argument to be a generator of integers.");}constnextIndex=previousIndex+1;constnextAverage=(value-previousAverage)/nextIndex;returnaverage(generator,previousAverage+nextAverage,nextIndex);}console.log(average(range(1,3)));// 2console.log(average(range(1,5)));// 3console.log(average(range(1,6)));// 3.5
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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.