You're going to manage a list of students. By the end you'll have practiced everything you'll do every single day in real code: arrays, objects, and arrays of objects.
- Fork this repo to your account.
- Clone it locally and open in your editor.
- Open
index.htmlin the browser. Open the console (F12). - Edit
challenges.js. Refresh the page to see new output.
Start with this array:
const fruits = ["apple", "banana", "orange"];Do the following, in order:
- Add
"mango"to the end of the array - Remove the first item from the array (use
.shift()) - Log the final array
- Log how many items are in it (use
.length)
Expected output:
[ 'banana', 'orange', 'mango' ]
3
Given this array:
const cities = ["Mogadishu", "Hargeisa", "Bosaso", "Garowe"];Use .forEach() to log each city in this format:
1. Mogadishu
2. Hargeisa
3. Bosaso
4. Garowe
💡 The forEach callback can take a second argument which is the index. Look it up if you forget the syntax.
Create an object called student with these properties:
name(string) — your nameage(number) — any numberisEnrolled(boolean) — set totrue
Then:
- Log the student's name using dot notation (
student.name) - Update the
ageproperty to be 1 higher - Log the whole object
Given this array:
const students = [
{ name: "Fatuma", score: 92 },
{ name: "Asha", score: 68 },
{ name: "Khadija", score: 85 }
];Use .forEach() to log a line for each student:
Fatuma scored 92
Asha scored 68
Khadija scored 85
🎯 Why this matters: this is exactly the shape of data every API will give you. Lock it in now.
- All 4 tasks log the expected output
- You used
.forEach()(notforloops) - You used
constfor things that don't change,letonly if needed
git add .
git commit -m "Complete Week 5 arrays & objects assignment"
git pushSubmit your repo link.
Add a Task 5: Take the students array from Task 4 and use .forEach() to log only the names of students who scored 70 or higher. (We'll learn .filter() next week to do this more elegantly!)