-3

I want to create and array and fill it's content using for loop. It's like

include

Int main (){

    Int array [6];
    Int i;

    For(i=0;i <6;i++){
        Array [i]=i*3;
        Printf ("%d",array [i]);
    }
    Return 0;
}

In c. How to do it in javascript?

VUN
  • 9
  • 1
  • 2
  • 1
    Possible duplicate of [Loop through an array in JavaScript](http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Arpit Solanki Dec 27 '16 at 05:23
  • Useful https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – Naghaveer R Dec 27 '16 at 05:24

3 Answers3

1

var str = Array();

for(var i=0; i<6; i++){
  str[i]= i*3;
}
console.log(str);
ab29007
  • 7,611
  • 2
  • 17
  • 43
1

You will want something like this:

var array = [];
for (var i = 0; i < 6; i++) {
    array.push(i * 3);
    console.log(array[i]);
}
0

Having a function is optional, but still you can try this:

function createArray()    {
    var array = [];

    for(var i=0;i<6;i++)    {
        array.push(i*3);
        console.log("i : "+i+" value : "+i*3);
    }

    return array;
}

var response = createArray();
console.log(response);
Yajnesh Rai
  • 290
  • 1
  • 6
  • 17