Coding Activity 3.1
Coding Activity 3.1
Or like this:
var famousGrilledCheese = [
'bread',
'sharp cheese',
'butter',
'tomato',
'garlic',
'oregano'
]
Arrays are an ordered collection of elements, meaning that the elements in them will
always appear in the same order. The array [1, 1, 7], is different from the array [1, 7,
1].
.length Method
Arrays have a lot of useful built-in methods. The array .length method will return the
number of elements in an array:
const studentsNames = ['Kyle', 'Maria', 'Joe', 'Andrew'];
console.log(studentNames.length); // 4
We can access an element at any time in an array. We just need to call the element
by its position in the array. Elements are given a numerical position (index) according
to where they are in an array. An array's numerical order always starts at 0, so the
first element is in the 0 index, the second in the 1 index, the third in the 2, and so on.
const studentsNames = ['Kyle', 'Maria', 'Joe', 'Andrew'];
0 1 2 3
To access an element, type the name of the array variable, followed by brackets
containing the numerical assignment.
const studentsNames = ['Kyle', 'Maria', 'Joe', 'Andrew'];
console.log(studentNames[1]); // 'Maria'
To dynamically access the last element in an array, we use the .length method. In
our studentsNames array, the length is 4. We know the first element is always going to
be 0, and every element after is shifted over one number. So, in our example the last
element has an index of 3. Using our length property, we will show how it's done
when we don't know the number of element in an array:
const studentsNames = ['Kyle', 'Maria', 'Joe', 'Andrew'];
console.log(studentNames[studentNames.length - 1]); // 'Andrew'
These methods refer to the adding and removing of elements from the array after its
initial declaration.
Task instructions
Task 1
Open the file task1.js under the folder first-array. You'll see a group of functions to
work on.
There are six functions, read each comment and write the return statements for each
of them.
It's a good practice to console log the results to help figure out what's expected.
Task
Given the following 6 empty functions, go through each comment, and complete the
code to return the proper array on each of them.
// Instructions
// - Given the following 6 empty functions, add the right return statement to each of them to
complete the activity
// Please do not change any of the function names
function returnFirst(arr) {
// return the first item from the array
function returnLast(arr) {
// return the last item of the array
}
function getArrayLength(arr) {
// return the length of the array
}
function incrementByOne(arr) {
// arr is an array of integers(numbers), Increment all items in the array by
// return the array
Given an array of strings (words), write a function that would concatenate all the
strings in the array to form a sentence. The words should be separated by a space.
Task
Given an array of strings called words, return a string that has all the words
concatenated together.