BCA Student Stories & UpdatesLive
JavaScript Arrays & Modern Transformation Methods: push, map, filter & reduce
Arrays are ordered collections of values. In modern JavaScript, array iteration methods like .map() and .filter() are preferred over imperative for loops because they produce cleaner, declarative code that does not mutate original data.
Day 26 Step-by-Step Practical Flow
π―1. What You Will Learn Today
JavaScript Arrays & Modern Transformation Methods: push, map, filter & reduce β Comprehensive Practical Guide
Har concept ko visual mental models, code anatomy aur practical industry tips ke sath samjhein.
Arrays are ordered collections of values. In modern JavaScript, array iteration methods like .map() and .filter() are preferred over imperative for loops because they produce cleaner, declarative code that does not mutate original data.
"Think of an array like a conveyor belt of exam answer sheets. .map() stamps a grade on every sheet, .filter() picks out only sheets scoring 40 or higher, and .reduce() counts the total marks of all sheets into one ledger."
π¨βπ«3. Teacher Demonstration Code
See how in 3 short lines we can filter passing scores, add bonus marks, and calculate the total sum.
<!DOCTYPE html>
<html>
<body>
<script>
const examMarks = [35, 78, 92, 44, 28, 85];
// 1. Filter: Keep only passing marks (>= 40)
const passingScores = examMarks.filter(mark => mark >= 40);
console.log("Passing Scores:", passingScores); // [78, 92, 44, 85]
// 2. Map: Convert scores to percentages out of 100
const formattedPercentages = examMarks.map(mark => mark + "%");
console.log("Formatted:", formattedPercentages);
// 3. Reduce: Calculate total marks
const totalMarks = examMarks.reduce((sum, mark) => sum + mark, 0);
console.log("Total Class Marks:", totalMarks);
</script>
</body>
</html>2. Hands-On Practical Lab: Follow Instructions in VS Code
Code like a professional software engineer! Follow the step-by-step instructions below in Visual Studio Code (VS Code) on your PC, test your output locally, and push to GitHub.
Open β Windows Terminal or Command Prompt on your PC and execute:
π‘ Tip: The command code . immediately opens the new project folder inside Visual Studio Code.
index.html & Paste Starter TemplateIn VS Code Explorer (Ctrl + Shift + E), click New File, name it index.html, and paste this base template:
<!DOCTYPE html>
<html>
<body>
<script>
const scores = [45, 82, 38, 90, 67, 52];
// Task 1: Filter passing scores (score >= 50) into const qualifiedScores
// Task 2: Map scores adding 5 bonus marks into const bonusScores
</script>
</body>
</html>Filter scores >= 50
Use scores.filter(score => score >= 50) and store the resulting array in const qualifiedScores.
const qualifiedScores = scores.filter(s => s >= 50);Map scores with 5 bonus marks
Use scores.map(score => score + 5) and store the resulting array in const bonusScores.
const bonusScores = scores.map(s => s + 5);- Right-click
index.htmlin VS Code and select "Open with Live Server". - Alternatively, open File Explorer, navigate to
bca-day-26, and double-clickindex.htmlto view in Google Chrome or Edge. - Verify all elements and styles look correct in the browser before pushing!
Spot the Bug: Confusing .find() with .filter()
Tap or click on the exact line below that contains the error:
Click any code lineconst marks = [20, 65, 80, 45];// Goal: Get all marks >= 40const passing = marks.find(m => m >= 40); // Bug!console.log("All passing:", passing); // Prints: 65 (Only the first match!)Senior Developer Forensic Analysis:
.find() stops at and returns ONLY the first matching element. To collect ALL matching elements in an array, .filter() must be used.
πIndependent Practice: Assignment
π credits-aggregator.htmlBCA Course Credits & GPA Aggregator
- β’Create day-26/index.html with an array of subject score objects: { subject: "BCS-011", marks: 78, credits: 4 }.
- β’Use .filter() to extract high-scoring subjects (marks >= 75).
- β’Use .map() to generate clean HTML list item strings: <li>Subject: Marks</li>.
- β’Use .reduce() to compute the weighted average marks of the student.
Open Visual Studio Code on your PC (code .), create credits-aggregator.html, paste this complete step-by-step verified code, test in your browser, and push to your GitHub repo:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>BCA Day 26 - Credits & GPA Aggregator (Array Methods)</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
font-family: 'Segoe UI', Tahoma, sans-serif;
background: #0b1120;
color: white;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.card {
background: #1e293b;
border: 1px solid #334155;
border-radius: 16px;
padding: 28px;
max-width: 520px;
width: 100%;
}
h1 { font-size: 20px; color: #38bdf8; margin-bottom: 12px; }
ul { list-style: none; margin: 16px 0; }
li { background: #0f172a; padding: 8px 12px; margin-bottom: 6px; border-radius: 6px; font-size: 13px; border-left: 3px solid #10b981; }
.weighted-box { background: #172554; border: 1px solid #2563eb; padding: 12px; border-radius: 8px; font-weight: bold; margin-top: 16px; }
</style>
</head>
<body>
<div class="card">
<h1>BCA Semester 1 Honors Aggregator</h1>
<div style="font-size: 12px; color: #94a3b8;">High-Scoring Subjects (Marks >= 75):</div>
<ul id="subjectList"></ul>
<div class="weighted-box" id="weightedGpa"></div>
</div>
<script>
const courses = [
{ code: "BCS-011", name: "Computer Basics", marks: 82, credits: 3 },
{ code: "BCSL-013", name: "PC Software Lab", marks: 90, credits: 2 },
{ code: "BCS-012", name: "Mathematics", marks: 68, credits: 4 },
{ code: "FEG-02", name: "Communication Skills", marks: 76, credits: 3 }
];
// 1. .filter() to extract high-scoring subjects (marks >= 75)
const honorsCourses = courses.filter(c => c.marks >= 75);
// 2. .map() to create HTML list items
const listHtml = honorsCourses
.map(c => `<li><strong>${c.code}</strong> - ${c.name}: ${c.marks}/100 (${c.credits} Credits)</li>`)
.join("");
document.getElementById("subjectList").innerHTML = listHtml;
// 3. .reduce() to compute weighted average marks
const totalWeightedMarks = courses.reduce((acc, c) => acc + (c.marks * c.credits), 0);
const totalCredits = courses.reduce((acc, c) => acc + c.credits, 0);
const weightedAvg = (totalWeightedMarks / totalCredits).toFixed(2);
document.getElementById("weightedGpa").textContent =
`Weighted Semester Average: ${weightedAvg}% across ${totalCredits} total credits`;
console.log("Honors courses:", honorsCourses);
console.log("Weighted average:", weightedAvg);
</script>
</body>
</html>π4. Viva Check: Mandatory Conceptual Test
Does the JavaScript Array.prototype.map() method mutate the original array?
5. Mandatory Step: Push to GitHub to Unlock Next Lesson
Target Repo: html-css-projects
Exact GitHub Setup Guide & Expected Structure
Day 26 RequirementsFollow these exact folder and file names to prevent verification errors.
index.html: Avoid spaces or custom names like Alan Turing..html or Index.HTML. Linux/GitHub servers are strictly case-sensitive.day-26/ for today so you don't overwrite previous days.main.You are welcome to read all notes, inspect code snippets, and practice all exercises of Day 26 freely! To officially submit your GitHub repository and unlock subsequent days in your portfolio, please complete Day 25: JavaScript Functions: Declarations, Arrow Syntax, Parameters & Scope first.
Complete Day 25 First