Joe Hill
Joe Hill

Reputation: 93

How do I implement average age with deviation in javascript?

How do I implement average age with deviation in javascript? I need to make an a Javascript algorithm that:

I tried to write each prompt one by one but was wondering if there is a way to shorten this code

var input = prompt('Enter your name');
var name1 = input;
input = prompt('Enter your age:');
var age1 = parseInt (input);

var input = prompt('Enter your name');
var name2 = input;
input = prompt('Enter your age:');
var age2 = parseInt (input);

[...]

var input = prompt('Enter your name');
var name10 = input;
input = prompt('Enter your age:');
var age10 = parseInt (input);

var average = (age1+age2+age3+age4+age5+age6+age7+age8+age9+age10/10);

console.log(`Average age: ${average}`);
console.log(`Name: ${name1}, Age: ${age1}, Deviance: ${deviance1}`);
console.log(`Name: ${name2}, Age: ${age2}, Deviance: ${deviance2}`);
[...]
console.log(`Name: ${name10}, Age: ${age10}, Deviance: ${deviance10}`);

I Don't know how to do the deviance (difference from the analyzed value to the average value)

Upvotes: 1

Views: 126

Answers (2)

Spangle
Spangle

Reputation: 822

Another solution below. I have added some comments to the code to assist. Let me know if you have any questions :)

const MAX_NAMES = 3;

// Each person will be stored in this array below
let people = [];
// Store the total combined age in totalAge variable
let totalAge = 0;

for(let i = 0; i < MAX_NAMES; i++) {
    let nameValue = prompt('Enter your name');
    let ageValue = parseInt(prompt('Enter your age'));
    // Add to the total age
    totalAge += ageValue;
    // we add an object to the array. 'name' and 'age' are the object Keys. We reference the keys to get the values back from the object later
    people.push({ name: nameValue, age: ageValue })
}

// Print the Average Age of People
let averageAge = Math.round(totalAge / people.length);
console.log(`The average age is ${averageAge}`);

//Print each person's name and deviation from age to average.
for(let person in people) {
    console.log(`Name: ${people[person].name} Deviation: ${Math.abs(people[person].age - averageAge)}`);
}

Upvotes: 1

CertainPerformance
CertainPerformance

Reputation: 370759

Each person (with a name and an age) is a singular entity with related properties, so consider using an object instead. Since you have many different people, you can use an array instead, then get the average by iterating over it with reduce, and log all differences by iterating with forEach:

// this snippet uses 3 people only

const people = [];
for (let i = 0; i < 3; i++) {
  const name = prompt('Enter your name');
  const age = Number(prompt('Enter your age'));
  people.push({ name, age });
}

const average =
      // add up all ages in the array:
  people.reduce((a, { age }) => a + age, 0)
      // divide by the number of people:
  / people.length;
  
people.forEach(({ name, age }) => {
  const deviance = Math.abs(age - average);
  console.log(`Name: ${name}, Age: ${age}, Deviance: ${deviance}`);
});

Upvotes: 1

Related Questions