0

I have a function in my script and the first parameter is an int value.

thisIsMyFunction(4,"foo");

I would like to create an array with that int value, so in this case, it should create an array with 4 items in it, like so:

var intArray = [0, 1, 2, 3];

And so on:

thisIsMyFunction(8,"foo");
var intArray = [0, 1, 2, 3, 4, 5, 6, 7];

I am a beginner in javascript, so I hope you can help me solve my issue ;) Thanks

2
  • why do you need "foo" in the functions? Commented Mar 12, 2014 at 7:35
  • what output you want just tell it,no more lines.State in 1 line what output you want Commented Mar 12, 2014 at 7:36

7 Answers 7

3
function thisIsMyFunction(count, string) {
    var intArray = [];
    for (var i = 0; i < count; i++) {
        intArray.push(i);
    }
    return intArray;
}
Sign up to request clarification or add additional context in comments.

Comments

2

try

function test(length,string)
{
var array=[];
for(i=0;i<length;i++)
{
array.push(i);

}
return array;
}

Comments

1

Just loop through and fill the array values as below.

function thisIsMyFunction(count, string) {
intArr = [];
for (var i=0;i<functionVal;i++){
    intArr[i] = i;
}
}

Comments

1

Try this:

function thisIsMyFunction(num, str){
   var arr = [];
   for(i=0; i < num; i++){
      arr.push(i);
   }
   console.log(arr);
   return arr;
};

1 Comment

It doesn't reference anything in the DOM, why does it need to be in the onload handler?
1
function thisIsMyFunction(num, line){
    var intArray = [];
    for( var i=0; i<num; i++ ){
        intArray.push(i);
    }
    return intArray;
}

Comments

1

Try This

Array.prototype.repeat= function(what, L){
 while(what < L) this[what]= what++;
 return this;
}
var A= [].repeat(0, 4);

alert(A)

Comments

0

Set the array size to the integer that is being passed and just increment the array items.

1 Comment

Providing a small sample would be useful.

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.