creating an array with for loop

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()" />

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

(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 has tutorials on these things:
http://www.kirupa.com/html5/arrays_javascript.htm
http://www.kirupa.com/html5/loops_in_javascript.htm
http://www.kirupa.com/html5/creating_dom_elements_and_other_stuff.htm

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

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

I did notice that :wink:

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…

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

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:

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

What trick is this??

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

Oh. Right. :joy: