Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding Average String Length of Strings in Array - Javascript

I have an array of six quotes and I'm trying to find the average length of each quote. I'm thinking I need to create a new array of the string lengths, then average. But I can't figure out how to get the counts of the original array into the new array. How do I get the counts of the first array into the new array?

like image 663
Mike Alexander Avatar asked Sep 28 '26 21:09

Mike Alexander


1 Answers

arr = [1, 12, 123, 1234]                    // works with numbers too
avg = arr.join('').length / arr.length      // 10 / 4 = 2.5
console.log(avg)
like image 86
Slai Avatar answered Sep 30 '26 11:09

Slai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!