Complete all TODOs in assignment.js. Each question is INDEPENDENT. You must:
Watch this video to see how the assignment works:
Task: Create a bank account system using closures to maintain private balance and transaction history.
In real banking software, account balances must be protected from direct manipulation. Closures provide a way to create truly private variables that can only be modified through controlled methods, ensuring data integrity and security.
What is a closure? A closure is created when an inner function "remembers" variables from its outer function's scope, even after the outer function has finished executing. This creates a persistent, private environment.
Real-world use: E-commerce shopping carts, user session management, game player stats, and any system requiring protected state.
createBankAccount(initialBalance, accountHolder) that returns an account objectdeposit(amount) - adds to balance (throw error if amount β€ 0)withdraw(amount) - removes from balance (throw error if insufficient funds or amount β€ 0)getBalance() - returns current balancegetAccountHolder() - returns account holder namegetTransactionHistory() - returns array of transaction objects: [{type, amount, balance, timestamp}]let or const. These become private!balance, holder, and transactions array, then return an object literal with methodsreturn { method1: function() {...}, method2: function() {...} };balance, transactions, etc. without thistypeof to check typestypeof x === 'number' AND !isNaN(x) AND isFinite(x)typeof and use .trim() to remove whitespace before checking if emptytype, amount, balance, timestampnew Date().toISOString() to get ISO format timestampreturn [...transactions];balance directly from outside - only through methods!createBankAccount() creates a NEW separate environmentDemo: Test your bank account system
Task: Create Circle and Rectangle constructors using prototypes to demonstrate efficient object creation and shared methods.
JavaScript uses prototype-based inheritance, not classical classes. When you add methods to a constructor's prototype, ALL instances share the same function in memory - this is much more efficient than creating new functions for each object.
Why this matters: Imagine creating 10,000 shapes. With prototypes, you create 10,000 objects but only 2 sets of methods. Without prototypes, you'd create 10,000 objects AND 10,000 copies of each method!
Real-world use: Game engines (thousands of entities), data visualization (thousands of data points), UI frameworks (many components).
Circle(x, y, radius, color) constructor function with:
getArea() - returns Ο Γ radiusΒ² getPerimeter() - returns 2 Γ Ο Γ radiusdraw(ctx) - draws circle on canvascontains(px, py) - returns true if point (px,py) is inside circleRectangle(x, y, width, height, color) constructor function with:
getArea() - returns width Γ heightgetPerimeter() - returns 2 Γ (width + height)draw(ctx) - draws rectangle on canvascontains(px, py) - returns true if point (px,py) is inside rectanglefunction Circle() NOT class Circlethis refers to the specific instanceCircle.prototype.method is shared by ALL Circle instancesDemo: Click canvas to create shapes. Click shapes to select them.
contains() method - if you click inside a shape, it should be highlighted (yellow border) and shape info (type, area, perimeter) should display below the buttons.Task: Create a stopwatch system using closures to manage timer state and provide lap timing functionality.
This question combines closures with timer management. Real applications constantly need to manage asynchronous operations (timers, intervals, API calls) while maintaining state. Closures are perfect for this!
Why closures + timers? You need to track timer IDs, running state, elapsed time, and laps - all while ensuring this data stays private and consistent.
Real-world use: Progress trackers, countdown timers, auto-save systems, polling mechanisms, animation loops, session timeout managers.
createStopwatch() that returns a stopwatch objectstart() - starts the stopwatch (throw error if already running)stop() - pauses the stopwatch and returns elapsed time in millisecondsreset() - resets to 0 (throw error if currently running)lap() - records current time as a lap (throw error if not running)getTime() - returns current elapsed time in millisecondsgetLaps() - returns array of lap timessetInterval to update time every 10msDemo: Test your stopwatch implementation
Task: Build a complete bouncing ball physics simulation combining prototypes, closures, animation, and collision detection.
This capstone question integrates all concepts: prototypes for efficient Ball objects, closures for private animation state, timer management with requestAnimationFrame, and event handling for interaction.
Why this matters: Game engines, physics simulations, data visualizations, and interactive animations all use these exact patterns. This is how real applications are built!
Skills demonstrated: Object-oriented programming, animation loops, state management, collision detection, event handling, and canvas rendering.
Ball(x, y, radius, color, vx, vy) constructor using prototypes with:
update(canvasWidth, canvasHeight) - moves ball, bounces off wallsdraw(ctx) - draws ball on canvascollidesWith(otherBall) - returns true if colliding with another ballcreateBallSimulation(canvas) using closures with:
addBall(x, y, radius, color) - adds ball with random velocitystart() - begins animation loop using requestAnimationFramestop() - stops animationclear() - removes all ballssetGravity(enabled) - toggles gravity on/offhandleClick(event) - adds ball at click positionDemo: Click canvas to add balls! Toggle gravity to see effects! πΎ
requestAnimationFrame.Task: Build a complete fireworks animation system with particle explosions, combining prototypes, closures, and canvas animation.
This capstone question brings together all the concepts you've learned: prototypes for efficient Particle objects, closures for private animation state, timer management with requestAnimationFrame, and event handling for user interaction.
Real-world application: Particle systems power game effects (explosions, fire, smoke), celebration animations, data visualizations, and interactive graphics. This is how professional animations are built!
What makes this special: You'll create TWO constructor functions (Particle and Firework) that work together, plus a closure-based controller that manages the entire show. This demonstrates composition and system design.
Particle(x, y, color, size) constructor using prototypes with:
update() - updates position (x += vx, y += vy), applies gravity (vy += 0.1), decreases life by 0.01draw(ctx) - draws particle with transparency based on life (ctx.globalAlpha = life)isDead() - returns true if life <= 0Firework(x, y, targetY, color) constructor using prototypes with:
update() - moves upward (y -= 3), explodes at targetY creating 30-50 Particlesdraw(ctx) - draws rocket (if not exploded) or all particles (if exploded)isFinished() - returns true when exploded and all particles are deadcreateFireworksShow(canvas) using closures with:
addFirework(x, color) - creates firework at bottom, targeting 20-40% up canvasstart() - begins animation loop with requestAnimationFramestop() - stops animation and clears canvashandleClick(event) - creates firework at click position with random colorDemo: Click canvas to launch fireworks! πβ¨
requestAnimationFrame.| Question | Criteria | Points |
|---|---|---|
| Q1 (20 pts) | Closure implementation & private state | 10 |
| Method correctness & transaction logging | 6 | |
| Error handling & validation | 4 | |
| Q2 (25 pts) | Constructor functions & prototype methods | 12 |
| Geometry calculations & drawing | 8 | |
| Input validation & error handling | 5 | |
| Q3 (25 pts) | Closure-based timer with private state | 10 |
| Timer management (start/stop/reset/lap) | 10 | |
| Validation & error handling | 5 | |
| Q4 (30 pts) | Ball constructor with prototypes | 10 |
| Simulation with closures & animation | 10 | |
| Physics & collision detection | 6 | |
| Code quality & error handling | 4 | |
| Q5 (30 pts) | Particle & Firework constructors with prototypes | 12 |
| FireworksShow with closures & animation | 10 | |
| Particle system & explosion logic | 5 | |
| Code quality & error handling | 3 |
assignment.jsindex.html or test-framework.jsassignment.js - Your completed implementationindex.html - The assignment page (do not modify)styles.css - The stylesheet (do not modify)test-framework.js - The test framework (do not modify)