Some array functions
Let's say your program has an array called colourArray with several elements, like this:
What do you think the following code does?
Changing things in the middle of a line with
Often we want to insert answers into our HTML document.
We create an element with an id, say,
id="outputArea", and write inside it using
If we wanted an answer inside it's own block, we used:
If we wanted an answer inside a sentence, we put it inside a formatting tag, like:
<p>The answer is: <strong id="outputArea"></strong></p>
- or ...
<p>The answer is: <em id="outputArea"></em></p>
But what if you want to insert information without changing the formatting?
Then you use
<p>The answer is: <span id="outputArea"></span></p>
Let's say I want to insert a list of the days of the week into my HTML document. I could use
span in the middle of the elements like this:
That code will create this:
There are days in a week.
The days of the week are: , , , , , ,
<span>s with array data:
We will get this:
There are 7 days in a week.
The days of the week are: Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
Joining arrays into strings with
Let's say that you have the colours of the rainbow still in colourArray.
You can make a comma separated list of colours with the following code:
var colourListString = colourArray.join(", ");
Now colourListString = "Red, Orange, Yellow, Green, Blue, Indigo, Violet".
var dayListString = daysOfWeek.join("Kitten");
Makes dayListString have a bizarre string like:
Saving your work
Download the template and rename it to your last name, such as "
In this assignment you will add snippets of code to the template to correctly display lists of months, days of the week, and ordinal numbers.
Throughout the code there are comments that say "
Follow the instructions in the comments using the examples from this page to make the code operate properly.
Here is what your results should look like.