Home > Enterprise >  Calling function with a passed in array
Calling function with a passed in array

Time:09-16

I'm trying to write a function that will be called with an array that has information on a person such as their name and then age. I need this function to grab all of the numbers only and then return them then added up. I've done some research and it seems filter and reduce are what I need to do this in the easiest way for a total beginner like me to do?

Apologies for any typos/wrong jargon as my dyslexia gets the better of me sometimes. An example of what kind of array is being passed into the function when called;

  { name: 'Clarie', age: 22 },
  { name: 'Bobby', age: 30 },
  { name: 'Antonio', age: 40 },

Would return the total added numbers. // returns 92

Why isn't the array I'm calling this function with working? Can you provide me a working example without the array being hardcoded like the other answers? - I'm passing in an array to the function. The main objective is to grab any number from the passed in array and add them together with an empty array returning 0.

function totalNums(person) {
  person.reduce((a,b) => a   b, 0)
  return person.age;
}

console.log(totalNums([]))

CodePudding user response:

You need to save the result of your reduce.

For example with array of numbers you would do:

function totalNums(person) {
  let res = person.reduce((a,b) => a   b, 0)
  return res;
}

console.log(totalNums([5,6,4]))

And for your example you would like to do something like this:

function totalNums(person) {
  let res = person.reduce((a,b) => a   b.age, 0)
  return res;
}

console.log(totalNums([ 
  { name: 'Clarie', age: 22 },
  { name: 'Bobby', age: 30 },
  { name: 'Antonio', age: 40 }
]))

CodePudding user response:

You need to save the result into a new variable then console.log() it like this

const arr= [{ name: 'Clarie', age: 22 },
  { name: 'Bobby', age: 30 },
  { name: 'Antonio', age: 40 },...
];

function totalNums(person) {
  let res = person.reduce((a,b) => a   b.age, 0)
  return res;
}

console.log(totalNums(arr));

and this is why it has to be like that

.reduce()

js methods like .map(), .filter(), .reduce() and some others, they return a new array, they don't modify the original array. You can console.log(arr); and you will get this output:

    [{ name: 'Clarie', age: 22 },
      { name: 'Bobby', age: 30 },
      { name: 'Antonio', age: 40 },...
    ];

Your original array unchanged even after running your function so in order to get the result you expect, you need to store it inside a new variable

CodePudding user response:

function totalNums(person) {
  person.reduce((a,b) => a   b, 0)
  return person.age;
}

console.log(totalNums([]))

Talking about the function you have created it is incorrect because:

  • return person.age; Here you are passing an array to function and then accessing it like it's an object.
  • person.reduce((a,b) => a b, 0) you can't add a and b because b is an object.
  • You are not storing value which reduce function will return.

Solution Below :

The reduce function always returns something It never makes changes in the original array.

function totalNums(persons) {
  const totalAge = persons.reduce((total, person) => total   person.age, 0);
  return totalAge;
}

const persons = [
  { name: "Clarie", age: 22 },
  { name: "Bobby", age: 30 },
  { name: "Antonio", age: 40 },
];
console.log(totalNums(persons));
  • You can replace total and person with a and b respectively in the above code snippet for your reference.
  • Related