Practice and reinforce the concepts from Lesson 7
In this activity, you will learn how to create and manipulate arrays in JavaScript. You'll practice declaring arrays, adding and removing elements, accessing array elements by index, and using array methods like push(), pop(), and forEach().
DO NOT DELETE the existing files in the template:
ONLY EDIT the necessary files.
⏱️ Estimated time: 30 minutes
⏱️ Time: 5 minutes
What To Do Create an array that has elements of animals in the zoo
Steps:
animals
and give the array 5 animals as its elementstip
Use syntax like: var animals = ["gorilla", "parrot", "elephant", "giraffe", "tiger"];
:information_source: Info Remember to open the browser console (F12) to see your output!
If you have done it correctly, it should look like this in browser console (F12) output:
Assuming the animals are gorilla, parrot, elephant, giraffe, tiger.
⏱️ Time: 5 minutes
What To Do Add and remove elements using the push and pop methods
Steps:
x
with these elements: 100, 200, 300
x
to the console400
to array x
and log the array to the consolex
and log the array to the consoletip
x.push(400)
to add an element to the endx.pop()
to remove the last elementIf you have done it correctly, it should look like this in browser console (F12) output:
:warning: Warning You need to refresh the preview page to see the array results!
⏱️ Time: 10 minutes
What To Do Create an interactive seating chart where you can replace students in specific seats
Steps:
seats
with 6 student names (e.g., John, Benjamin, Hasan, Lisa, Tommy, Kumar)replaceIndex
newStudent
replace
:
replaceIndex
using prompt: "Which seat do you want to replace?"newStudent
using prompt: "Who is replacing this seat?"replaceIndex
with newStudent
seats
to the consolereplace
functiontip
seats[replaceIndex] = newStudent
to replace an array element at a specific index:information_source: Info Learn more about onclick events: https://www.w3schools.com/jsref/event_onclick.asp
If you have done it correctly, it should look like this:
When you click on the REPLACE button:
Assuming the seat is index 2 and the name is Winnie.
Browser console (f12) output:
⏱️ Estimated time: 30 minutes
⏱️ Time: 5 minutes
What To Do Create an array of dairy products and log each one separately
Steps:
dairy
with these elements: ["milk", "cheese", "yoghurt", "butter"]
forEach()
methodtip Use the forEach method like this:
dairy.forEach(function(item) {
console.log(item);
});
If you have done it correctly, it should look like this in browser console (F12) output:
⏱️ Time: 10 minutes
What To Do Create a charity funding system that allocates extra funds to homes that need it most
Steps:
var homes = [132000, 88000, 275000, 43000];
tip Use forEach with conditional logic:
homes.forEach(function(home) {
if (home < 90000) {
console.log(home + 50000);
} else {
console.log(home);
}
});
:information_source: Info This simulates a real-world scenario where organizations with less funding receive additional support!
If you have done it correctly, it should look like this in browser console (F12) output:
⏱️ Time: 10 minutes
What To Do Create a queue system that checks if people meet the age requirement for a rollercoaster ride
Steps:
queue
array with 5 ages (varying from 6 to 100)true
(can ride)false
(cannot ride)tip The forEach method can accept an index parameter:
queue.forEach(function(age, index) {
if (age >= 12) {
queue[index] = true;
} else {
queue[index] = false;
}
});
:information_source: Info Learn more about forEach parameters: https://www.w3schools.com/jsref/jsref_foreach.asp
If you have done it correctly, it should look like this in browser console (F12) output:
Assuming the age of each person in the queue array is 12, 8, 25, 11 and 38.
Make sure your Array projects include:
:warning: Important Test all your array operations and check console outputs before submitting!
When you have completed all 6 exercises in your "Arrays" project:
:information_source: Info Submit Your Work Submit Your Projects Here
Make sure all array functionality is working properly before submission.
Nothing appears in the console
Array index errors
forEach not working
Button onclick not working