user3192978
user3192978

Reputation: 95

jQuery: int value split as an array

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

Upvotes: 0

Views: 365

Answers (7)

A J
A J

Reputation: 2140

Try This

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

alert(A)

Upvotes: 1

Kyo
Kyo

Reputation: 964

function thisIsMyFunction(num, line){
    var intArray = [];
    for( var i=0; i<num; i++ ){
        intArray.push(i);
    }
    return intArray;
}

Upvotes: 1

sri hs
sri hs

Reputation: 116

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

Upvotes: 0

Jai
Jai

Reputation: 74738

Try this:

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

Upvotes: 1

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

try

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

}
return array;
}

Upvotes: 2

Barmar
Barmar

Reputation: 780869

function thisIsMyFunction(count, string) {
    var intArray = [];
    for (var i = 0; i < count; i++) {
        intArray.push(i);
    }
    return intArray;
}

Upvotes: 3

anirudh
anirudh

Reputation: 4176

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;
}
}

Upvotes: 1

Related Questions