Next ☞

Exercises in Arrays

Construct and play with arrays

I have created an Array with 5 values: Fat, Sugar, Carbs, Cals, 5. The first 4 values are literal strings:
: Everything in blue is created by the script :

The last value is a number:

Next I will look at the entries array method which returns:

Next is the 'length' method. This returns the length or number of values in an array:

Now I will add 10 to the last value in the array as it is a number:

Next I will display the whole array using the 'toString()' method:

Now I could look at assigning values to elements in an array. By entering them as input from a page.

Enter a new value for the last element:


The next most common thing is to iterate through an array outputting each value on a new line. To do this I will use the 'for' loop. I will use the array I have already created: The trick here was to declare a variable and assign it an empty string then to fill the empty string with the new value using the var += newvar.

Next I am going to add elements to the array from user input:

What would you like to add?

Next I want to add two arrays together:

Next ☞