0
0
Javascriptprogramming~15 mins

Common array operations in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Common array operations
πŸ“– Scenario: You are working on a small app that manages a list of fruits. You need to perform some common tasks like creating the list, adding a new fruit, filtering fruits by length, and showing the final list.
🎯 Goal: Build a program that creates an array of fruits, adds a new fruit, filters fruits with names longer than 5 letters, and prints the filtered list.
πŸ“‹ What You'll Learn
Create an array called fruits with the exact values: 'apple', 'banana', 'cherry', 'date', 'fig'
Create a variable called newFruit and set it to the string 'grapefruit'
Use the push method on fruits to add newFruit
Create a new array called longFruits that contains only fruits with names longer than 5 letters
Print the longFruits array using console.log
πŸ’‘ Why This Matters
🌍 Real World
Managing lists of items like fruits, products, or tasks is common in apps. Knowing how to add, filter, and display items is very useful.
πŸ’Ό Career
Many programming jobs require working with arrays or lists to handle data. These basic operations are foundational skills.
Progress0 / 4 steps
1
Create the initial array of fruits
Create an array called fruits with these exact values: 'apple', 'banana', 'cherry', 'date', 'fig'
Javascript
Need a hint?

Use square brackets [] to create an array and separate the fruit names with commas.

2
Add a new fruit to the array
Create a variable called newFruit and set it to the string 'grapefruit'
Javascript
Need a hint?

Use const newFruit = 'grapefruit'; to create the variable.

3
Add the new fruit to the fruits array
Use the push method on fruits to add the newFruit variable
Javascript
Need a hint?

Use fruits.push(newFruit); to add the new fruit to the array.

4
Filter and print fruits with names longer than 5 letters
Create a new array called longFruits that contains only fruits from fruits with names longer than 5 letters using the filter method. Then print longFruits using console.log
Javascript
Need a hint?

Use fruits.filter(fruit => fruit.length > 5) to get fruits with names longer than 5 letters.

Use console.log(longFruits); to print the result.