Search code examples
javascriptarraysaveragestring-length

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?


Solution

  • arr = [1, 12, 123, 1234]                    // works with numbers too
    avg = arr.join('').length / arr.length      // 10 / 4 = 2.5
    console.log(avg)