0

it seems a pretty easy question but I can't find a solution. I need to turn the argument of this function (just numbers followed by a comma) into an array full of numbers. The problem is, the argument is not a string and I cannot add the "" to it. If I console.log([card]) only the first number appears into the array (the 2)... I tried to do a loop but it didn't work out. Thanks for the help!

function cc(card) {

}
cc(2,3,4,5,6);
4
  • You are not sending one single argument. When you do cc(2,3,4,5,6); you are sending 5 different argument. You are seeing only the first argument because JS is very perimissible and won't complain if you send a different amount of argument that what it is suposed to receive Commented May 30, 2018 at 20:50
  • Ok I understand... so is there a way to use all the numbers in that "list"? Commented May 30, 2018 at 20:53
  • Does your function needs to be able to receive any amount of parameters? Commented May 30, 2018 at 20:57
  • about five of them, as in the example. However, if there isn't a way of addressing all numbers simply because I am not allowing the function to call them, it might be simply a problem of poor description of the exercise I am doing. Thanks for your help mate Commented May 30, 2018 at 21:04

1 Answer 1

1

Anyway, just in case, you can use the arguments variable:

var cc = function () { console.log(arguments) }

cc(1,2,3,4,5) prints Arguments { 0: 1, 1: 2, 2: 3, 3: 4, 4: 5, … } and cc(2,3,4,5,6,7,8) prints Arguments { 0: 1, 1: 2, 2: 3, … }

Source: https://gomakethings.com/getting-an-array-of-all-arguments-passed-into-a-function-with-vanilla-javascript/

Sign up to request clarification or add additional context in comments.

2 Comments

thanks so much, I didn't know of this method, very useful!!
and just to add one more thing I just discovered, to end up with an array filled with the arguments, you can also do Object.values(arguments) , using the method Object.values() plus the above mentioned arguments . Very good stuff indeed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.