# creating an array with for loop

**URL:** <https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744>\
**Category:** web dev\
**Created:** [March 1, 2015, 6:36pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744 "2015-03-01T18:36:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Curtis](https://avatars.discourse-cdn.com/v4/letter/d/aca169/32.png) [@David\_Curtis](https://forum.kirupa.com/u/David_Curtis)\
**Post date:** [March 1, 2015, 6:36pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/1 "2015-03-01T18:36:18Z")

</div>

Hello,

I’m working on a assignment which introduces us to arrays and loops. The object of the assignment is to write a program that stores information in the array while using a loop for the code. The information to stare in the assignment is food and calories. For example, I ate pizza and it was 300 calories. I can do the code line by line and it works fine, but for something that wants 20 - 30 inputs that is alot of coding and not practical, hence the loop and array

# This is the calorie management program

Below you will find the program which will allow you to enter the food you ate for the day and calculate the total calories

Click on the Calorie Management button below to proceed

When you are done please enter stop\</P

```
<input type = "button" value = "Calorie Management"
		onclick = "calorieburn()" />

```

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 1, 2015, 8:12pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/2 "2015-03-01T20:12:31Z")

</div>

Here’s some code that’ll create 20 input elements and a button that adds up the values of numbers typed into those inputs:

```javascript
(Math.pow(2,19)).toString(2).split('').forEach(function(i){ document.body.appendChild(document.createElement('INPUT')) })

var e = document.createElement('button')
document.body.appendChild(e)
e.appendChild(document.createTextNode('Calorie Management'))
e.addEventListener('click', function(){
    console.log([].reduce.call(document.querySelectorAll('input'), function(acc, e){
        return acc + Number(e.value)
    }, 0))
})

```

Why did I write this code? I don’t really know. But it does more-or-less what it sounds like your assignment wants.

But since you’re still learning loops and arrays, the above code probably won’t help your understanding much other than to show it’s possible. Instead, I’d recommend reading some tutorials (or your class materials) about arrays and loops, then asking a more specific question here.

[kirupa.com](http://kirupa.com) has tutorials on these things:  
[http://www.kirupa.com/html5/arrays\_javascript.htm](http://www.kirupa.com/html5/arrays_javascript.htm)  
[http://www.kirupa.com/html5/loops\_in\_javascript.htm](http://www.kirupa.com/html5/loops_in_javascript.htm)  
[http://www.kirupa.com/html5/creating\_dom\_elements\_and\_other\_stuff.htm](http://www.kirupa.com/html5/creating_dom_elements_and_other_stuff.htm)

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 1, 2015, 11:28pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/3 "2015-03-01T23:28:54Z")

</div>

Does `forEach()` count as a “for loop”?

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 1, 2015, 11:30pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/4 "2015-03-01T23:30:26Z")

</div>

More-or-less. 😇 As you can see, I went to lengths to avoid actually using a real `for` loop.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 1, 2015, 11:33pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/5 "2015-03-01T23:33:13Z")

</div>

I did notice that 😉

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 1, 2015, 11:45pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/6 "2015-03-01T23:45:42Z")

</div>

Out of curiosity, is there a more obvious way to make an `n`-length non-`undefined`-filled array without using ES6 things like Array#fill? (And without using a loop, etc.) I know there’s a nice trick I use sometimes to make an n-length string duplicate of characters, like `n * 'foo'` = `foofoofoo` for n=3,etc.

But for whatever reason, the first thing my brain thought of when I was writing that code is that you can use some exponential binary weirdness to know the string length of a number…

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 2, 2015, 4:50pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/7 "2015-03-02T16:50:40Z")

</div>

> [@krilnon](#):
>
> Out of curiosity, is there a more obvious way to make an n-length non-undefined-filled array without using ES6 things like Array#fill?

What comes to my mind immediately when thinking about a solution for that is empty plus a little join/split action. Not any better:

```auto
new Array(n + 1).join('"').split('')

```

I even checked out to see what the implementation of fill() is and, unsurprisingly, they (chromium) are using a for loop.

Let me think… Oh! How about this:

```auto
[].forEach.call(new Int8Array(n), ... );

```

> [@krilnon](#):
>
> I know there’s a nice trick I use sometimes to make an n-length string duplicate of characters, like n \* ‘foo’ = foofoofoo for n=3,etc.

What trick is this??

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [March 2, 2015, 5:21pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/8 "2015-03-02T17:21:54Z")

</div>

Ah it was just the `new Array(n).join('hello')` approach that you used.

---

<div class="post-metadata">

**Author:** ![senocular](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/senocular/32/7217_2.png) [@senocular](https://forum.kirupa.com/u/senocular)\
**Post date:** [March 2, 2015, 7:54pm UTC](https://forum.kirupa.com/t/creating-an-array-with-for-loop/559744/9 "2015-03-02T19:54:42Z")

</div>

> [@krilnon](#):
>
> Ah it was just the new Array(n).join(‘hello’) approach that you used.

Oh. Right. 😂
