π Day 5 β Arrays in Data Structures

π What Are Arrays?
An array is a linear data structure used to store multiple elements in a single variable, arranged in an ordered list. Each element is accessed using its index (position).
π§ Why Use Arrays?
Imagine storing 100 test scores. Instead of creating 100 variables, you can store them all in one array.
jsCopyEditlet scores = [85, 90, 78, 92, 88];
Now you can access any value using its index:
jsCopyEditconsole.log(scores[0]); // 85
π οΈ How Arrays Work
Arrays in JavaScript are zero-indexed.
This means the first element is at index
0
, the second at1
, and so on.You can store any type of data in a JS array.
jsCopyEditlet mixed = [42, "hello", true, null];
π¦ Array Operations
1. Create an Array
jsCopyEditlet fruits = ["Apple", "Banana", "Mango"];
2. Access Elements
jsCopyEditconsole.log(fruits[1]); // Banana
3. Change an Element
jsCopyEditfruits[1] = "Orange";
4. Length of Array
jsCopyEditconsole.log(fruits.length); // 3
5. Add Elements
At the end:
push()
At the beginning:
unshift()
jsCopyEditfruits.push("Grapes");
fruits.unshift("Pineapple");
6. Remove Elements
From the end:
pop()
From the beginning:
shift()
jsCopyEditfruits.pop();
fruits.shift();
π Loop Through Arrays
jsCopyEditfor (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Or using for...of
loop:
jsCopyEditfor (let fruit of fruits) {
console.log(fruit);
}
π Real-World Analogy
Think of an array like a row of mailboxes β each box (index) stores one item (value). You can open a specific box by its number (index) and retrieve or update the content.
π Summary
Arrays store ordered lists of items.
Indexing starts from
0
.You can add, remove, access, and loop through items.
Theyβre used everywhere β from handling user data to game inventories and more.
π§ͺ DSA CHALLENGES
β Easy:
Print the first and last elements of an array.
jsCopyEditlet cities = ["Lagos", "Abuja", "Kano", "Ibadan"];
βοΈ Intermediate:
Write a loop that prints only the even numbers from an array:
jsCopyEditlet numbers = [2, 5, 8, 9, 12, 15, 20];
π₯ Hard:
Reverse an array manually (without using reverse()
):
jsCopyEditlet data = [1, 2, 3, 4, 5];
β Solutions
Easy:
jsCopyEditconsole.log(cities[0], cities[cities.length - 1]); // Lagos Ibadan
Intermediate:
jsCopyEditfor (let num of numbers) { if (num % 2 === 0) { console.log(num); } }
Hard:
jsCopyEditlet reversed = []; for (let i = data.length - 1; i >= 0; i--) { reversed.push(data[i]); } console.log(reversed); // [5, 4, 3, 2, 1]
#CodeWithGift #DSA2025 #JavaScript #DayFive
Subscribe to my newsletter
Read articles from God'sgift Samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

God'sgift Samuel
God'sgift Samuel
About Me Tech enthusiast and developing web developer with a growing passion for blockchain technology. My journey spans 3 years of HTML/CSS crafting, 2 years of JavaScript exploration, and recent ventures into React, Node.js, and the blockchain space. Currently at the beginning of my blockchain journey while building a solid foundation in web development technologies. Fascinated by the potential of decentralized systems and eager to contribute to this evolving ecosystem. Technical Background I bring a diverse technical toolkit that includes: Strong foundation in web fundamentals (HTML/CSS: 3 years) Dynamic front-end development with JavaScript (2 years) and React (1 year) Modern UI implementation using Tailwind CSS and Bootstrap (7 months) Server-side programming with Node.js (1 year) and Python (2-3 years) Early-stage blockchain development knowledge Beginning exploration of Rust programming (4 months) Blockchain Journey While still at the beginner level in blockchain technology, I'm actively learning about distributed ledger concepts, smart contract fundamentals, and the broader implications of Web3. My interest in this space stems from a belief in the transformative potential of decentralized technologies and their ability to reshape digital interactions. Vision & Goals My development path is guided by a clear progression: mastering web development fundamentals, expanding into blockchain applications, and ultimately exploring the intersection of these technologies with artificial intelligence. I see tremendous potential in combining these domains to create innovative solutions for tomorrow's challenges. Collaboration Interests Open to connecting with fellow developers, blockchain enthusiasts, and mentors who share an interest in the convergence of web development and emerging technologies. Particularly interested in learning opportunities, knowledge exchange, and potential collaboration on projects that push the boundaries of what's possible in the decentralized space. Current Focus Deepening my understanding of React and Node.js ecosystems while simultaneously building knowledge in blockchain fundamentals and smart contract development. Committed to continuous learning and practical application of new skills.