Hello guys ! How do I convert a string from an array into numbers ? Javascript

Joined
Dec 7, 2022
Messages
5
Reaction score
0
My assignment is the following:

You are given an array years that contains years of life of different people in the next format 1714-1748 (year of birth - year of death).

Write a function getAverageAge returning an average life duration of all the people rounding it to the nearest whole (Math.round)

Example:

getAverageAge(['1832-1905', '1876-1956', '1683-1724', '1714-1748']) === 57

getAverageAge([ '1907-1997', '1761-1833', '1535-1582', '1918-2012', '1877-1968', '1696-1724', '1602-1642', '1692-1743', '1695-1762', '1570-1636', '1762-1807', '1668-1731', ]) === 63

I've been struggling with this function the whole day with no succes.
Before implementing the math in order to return the average age I've been trying to get the years in an array, but I can't find a way to convert the string into numbers.

This function just returns the first digit after '-', ex: console.log(getAverageAge('7111-4222')); - returns '4'

function getAverageAge(years) {

let arr = [];
for(let i = 0; i <= years.length; i++){
if(years === '-'){
arr.push(years[i + 1]);
};
};
return arr;
}

And this function returns undefinded:

function getAverageAge(years) {
let number = Number(years); ---also tryed let number = +years---
let arr = [];

for(let i = 0; i <= number.length; i++){
if(number === '-'){
arr.push(number[i + 1]);
}else{
arr.push(number);
}
return arr;
}
}

I'm lost please help !
 
Joined
Mar 11, 2022
Messages
227
Reaction score
32
Code:
function getAverageAge(years) {
var ages=new Array();
var agesSum=0;
for(var i = 0; i< years.length; i++){
        var ex=years[i].split('-');
    var a=parseFloat(ex[0]);
    var b=parseFloat(ex[1]);
    var result=b-a;
    agesSum+=result;
    ages.push(result);

}
return {ages,avrage:Math.ceil(agesSum/ages.length)}

}
This should return an object with an array of all ages and the average Age.
 
Joined
Dec 7, 2022
Messages
5
Reaction score
0
Code:
function getAverageAge(years) {
var ages=new Array();
var agesSum=0;
for(var i = 0; i< years.length; i++){
        var ex=years[i].split('-');
    var a=parseFloat(ex[0]);
    var b=parseFloat(ex[1]);
    var result=b-a;
    agesSum+=result;
    ages.push(result);

}
return {ages,avrage:Math.ceil(agesSum/ages.length)}

}
This should return an object with an array of all ages and the average Age.
It works, although the code is over my head, I never used var or parseFloat but I'll try hard to understand how the function works, thank you !
 
Joined
Jul 3, 2022
Messages
93
Reaction score
23
JavaScript:
function getAverageAge(arr){
   return Math.round(arr.map( x => { const ar = x.split('-'); return +ar[1] - +ar[0] } ).reduce( (a, b) => a + b, 0 ) / arr.length);
   }
 
   console.log( getAverageAge( ['1832-1905', '1876-1956', '1683-1724', '1714-1748'] ) );
   console.log( getAverageAge( ['1907-1997', '1761-1833', '1535-1582', '1918-2012', '1877-1968', '1696-1724', '1602-1642', '1692-1743', '1695-1762', '1570-1636', '1762-1807', '1668-1731'] ) );
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top