How to return the number of arguments and sum the values of the arguments in a javascript function

Asked

Viewed 329 times

7

I need to know how to return the amount of arguments.

Examples are: list(red, green, blue) Returns 3

list(1,45,33,50) Returns 4

It is not possible to know the amount beforehand

3 answers

7


You can use the argument and take its length, thus obtaining the amount of arguments passed in the function, in the example was used a Rest Parameters so that the function can receive an indefinite number of arguments:

function lista(...args) {
  return arguments.length
}

console.log(lista(1,2,3))
console.log(lista(1,2,3,4,5))
console.log(lista(1,2))

To take the sum for example of the arguments, you can do this way:

function lista(...args) {
  let result = 0;

  for (const soma of args) {
    result += soma;
  }
  return result;
}

document.write('A soma total é: ', lista(1,2,3))

  • Thanks solved!

  • 1

    You are welcome. If any of the answers served you in any way, you could accept some so that your question is as resolved on the site?

  • If not to abuse. In case I wanted to add the values passed in ...args as I would do?

  • Did not understand well, to add the numbers passed to the function, list(1,2,3), these numbers?

  • That’s right. Add the numbers in the arguments passed in ...args

  • I edited the answer.

  • Thanks. I edited the question to cover the answer.

Show 2 more comments

3

The object Arguments corresponds to the arguments passed to a function. It looks like a matching Array object but is not a Array.

The property is used here arguments.length to return the number of arguments to function

function verbosa() {
   return arguments.length;
}

console.log(verbosa(1, "batatinha", 2));

arguments can be iterated just like an array.

function verbosa2() {
   for(i=0, str = "" ; i < arguments.length; i++ ){
      str = str + arguments[i] + "\n";
   }   
   return "" + str;
}

console.log(verbosa2(1, "batatinha", 2));

But it’s not a array because it does not possess its methods.

function verbosa3a() {
   try {   
      return arguments.join(",");
   } catch (e){
      return "Deu erro na verbosa: " + e;
   } 
}

console.log(verbosa3a(1, "batatinha", 2));

If you want to use the arguments like a array must be whether convert to array,

function verbosa3b() {   
   return Array.from(arguments).join(",");
}

console.log(verbosa3b(1, "batatinha", 2));

If you want to use the arguments directly as a Array without making the conversion must Rest parameters.

function verbosa4(...args) {         
      return args.join(",");       
}

console.log(verbosa4(1, "batatinha", 2));

  • Thank you. Your reply was very good. It helped me a lot

1

Just use the property length function. See the following example:

function f1(a) {
  console.log("aqui tem 1")
}

function f2(a, b) {
  console.log("aqui vai ter 2")
}

function f3(a, b, c, d) {
  console.log("nessa vai ter 4")
}

console.log(f1.length)

console.log(f2.length)

console.log(f3.length)

Click here to see the reference.

  • 2

    But the length returns the expected number of parameters (i.e., what was declared when creating the function). But if you call the function with a different amount of arguments (eg: f1(1,2,3)), the length will not say how many have (which is what the question asks).

  • 2

    Does not work for the question the user specified "..that it is not possible to know the quantity in advance...".

  • Thank you for responding

Browser other questions tagged

You are not signed in. Login or sign up in order to post.