Question

Modify the function to return the average age of everyone in the people array, rounded to...

Modify the function to return the average age of everyone in the people array, rounded to the nearest integer.

function getRoundedAverageAge(people) {

}

/* Do not modify code below this line */

const examplePeopleArray = [
{ name: 'John', age: 19 },
{ name: 'Jack', age: 21 },
{ name: 'Jane', age: 22 }
];

console.log(getRoundedAverageAge(examplePeopleArray), '<-- should be 21');

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

function getRoundedAverageAge(people) {

    //declaring sum to 0

    var sum=0;

    //looping through the array

    for(var i=0;i<people.length;i++){

      //adding age of current person to sum

      sum+=people[i].age;

    }

    //finding average age

    var avg=sum/people.length;

    //rounding to nearest integer and returning it

    return Math.round(avg);

}

/* Do not modify code below this line */

const examplePeopleArray = [

{ name: 'John', age: 19 },

{ name: 'Jack', age: 21 },

{ name: 'Jane', age: 22 }

];

Add a comment
Know the answer?
Add Answer to:
Modify the function to return the average age of everyone in the people array, rounded to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify the function to return a copy of the given array sorted in ascending order (1,...

    Modify the function to return a copy of the given array sorted in ascending order (1, 2, 3, etc). Do not modify the original array. function copyAndSortNumbers(numbers) { } /* Do not modify code below this line */ const original = [1, 7, 3, 5]; const sortedCopy = copyAndSortNumbers(original); console.log(original, '<-- should be [1, 7, 3, 5]'); console.log(sortedCopy, '<-- should be [1, 3, 5, 7]');

  • Modify the function to push the given item onto the end of the array and return...

    Modify the function to push the given item onto the end of the array and return the array. function addItemToArray(array, item) { } const items = addItemToArray([1, 2, 3], 4); console.log(items, '<-- should equal [1, 2, 3, 4]');

  • In C++ First create the two text file given below. Then complete the main that is given. There ar...

    In C++ First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12...

  • First create the two text file given below. Then complete the main that is given. There...

    First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • Please help me with this in C# language. Returning an array from a function The goal...

    Please help me with this in C# language. Returning an array from a function The goal for this exercise is to make sure that you return an array from a method (as a return value). Also: to give you more practice creating and using methods/functions. What you need to do for this exercise: In the starter project, add code to the Returning_An_Array class, so that the RunExercise method does the following: Declare an integer array variable, but DO NOT ALLOCATE...

  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

  • Hey everyone, I need help making a function with this directions with C++ Language. Can you...

    Hey everyone, I need help making a function with this directions with C++ Language. Can you guys use code like printf and fscanf without iostream or fstream because i havent study that yet. Thanks. Directions: Write a function declaration and definition for the char* function allocCat. This function should take in as a parameter a const Words pointer (Words is a defined struct) The function should allocate exactly enough memory for the concatenation of all of the strings in the...

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • Can someone please help me with this javascript - Javascript - do at least 10 of...

    Can someone please help me with this javascript - Javascript - do at least 10 of the following questions below ----------------------------------------------------------------------------------- Create an HTML file that takes in input and carries out of the following functions and manipulates the DOM to show the outcome. Please put the question itself as a comment above each answer. Use either JavaScript, jQuery, or both (but know the difference!). ----------------------------------------------------------------------------------- 1. Fibonacci Define function: fib(n) Return the nth number in the fibonacci sequence. 2....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT