Sunday, March 26, 2023

Coding Challenge #33

 // based off of Daily Coding Challenge #33
// print the running average
// Example: [2,1,3] prints...
// 2 (2/1=2)
// 1.5 (2+1=3, 3/2=1.5)
// 2 (2+1+3=6, 6/3=2)
function doit(ary){
    for(var xint = 0;xint < ary.length;xint++){
        var total = ary[0];
        for(var yint = 1;yint < (xint+1);yint++){
            total += ary[yint];
        }
        console.log(total / (xint+1));
    }
}
doit([2,1,3]);

No comments:

Post a Comment

Coding Challenge #54 C++ int to std::string (no stringstream or to_string())

Gets a string from an integer (ejemplo gratis: 123 -> "123") Wanted to come up with my own function for this like 10 years ago ...