Node JS Age Counting Challenge:
As per response of get call count the total number of ages who has age value above or equal to 50.
Solution:
const https = require('https');
https.get('https://coderbyte.com/api/challenges/json/age-counting', (resp) => {
let data = ''
resp.on('data', (chunk) => {
data+=chunk;
});
resp.on('end', () => {
let jsonData = JSON.parse(data.toString());
let actualData = jsonData.data;
let arr1 = actualData.split(", ");
let totalCount = 0;
for(let i = 0; i < arr1.length; i++){
let item = arr1[i];
if(item.indexOf('age=') !== -1){
let age = item.split('=');
if(parseInt(age[1]) >= 50) {
totalCount++;
}
}
}
console.log(totalCount);
})
});
//OUTPUT: 128
Comments
Post a Comment