1

Trying to do something simple, but not sure what I am doing wrong.

I simply want to call a function with number arguments and push those numbers into a new array...

function someFunction(n){
  var newArray = new Array(n);
  for(var i=0; i < n.length; i++){
    newArray += n[i];
  }
  return newArray;
}

console.log(someFunction(3,5,4,5));

Here is bin

2
  • stackoverflow.com/questions/2141520/… take a look at this - here is explained how to deal with a variable number of arguments, which is, if I understand good, your main issue. And also at this w3schools.com/jsref/jsref_push.asp - here is how to push elements to a javascript array. Commented Feb 1, 2016 at 21:29
  • 3
    Start with MDN; you have a problem on nearly every line here. Commented Feb 1, 2016 at 21:29

4 Answers 4

3

This will get those numbers into the array for you. And it will do it for unlimited numbers supplied: https://jsfiddle.net/a9umss9a/3/

function someFunction(){
  var newArray = [];
  for(var i=0; i < arguments.length; i++){
    newArray.push(arguments[i]);
  }
  return newArray;
}

console.log(someFunction(3,5,4,5));

console.log(someFunction(3,5,4,5,100,200,300,400,500));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks gfrobenius, that is what I was looking for.
Why have a named arg n if you're not using it? Why use the Array constructor instead of var newArray = []?
Don't have to. Just using the OP code to get what he wanted. I made the changes tho, thanks @Mathletics
2

You can do this as a one-liner:

function toArray() {
    return [].slice.call(arguments);
}

Here's the breakdown of issues in your code.

function someFunction(n){ // this captures the FIRST argument as n
  var newArray = new Array(n); // this creates a new Array of length n (in your case, 3, which is not accurate
  for(var i=0; i < n.length; i++){ // n is a number and has no length property
    newArray += n[i]; // newArray is an array, so you must push to it; the + operator makes no sense
  }
  return newArray;
}

1 Comment

You see, sometimes sarcasm does the trick. Thanks for the great detailed answer!
0

someFunction requires 1 input of type array, not 4 inputs:

function someFunction(n){
  var newArray = [];
  for(var i=0; i < n.length; i++){
    newArray.push(n[i]);
  }
  return newArray;
}

console.log(someFunction([3,5,4,5]));

1 Comment

True. But doing it this way makes the function pointless. You could just replace the guts of the function with function someFunction(n){ return n; } and get the same thing. The OP wanted this format: someFunction(3,5,4,5).
0

A solution with direct building of an array.

function someFunction() {
    return Array.apply(null, arguments);
}

document.write('<pre>' + JSON.stringify(someFunction(3, 5, 4, 5), 0, 4) + '</pre>');

Comments

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.