CodingNic

Modern JavaScript for Node.js

Exercises

Modern JavaScript for Node.js 25 min read

Exercises

Objectives

By the end of this lesson, you should be able to:

  • Combine destructuring, spread, and rest in real code
  • Chain array and object methods to transform real data
  • Build a small class hierarchy and handle async operations correctly

⚠️ A note on verification: every snippet and output in this lesson was actually run with Node.js.

Exercise 1: ES6+ Syntax

a) Given const product = { name: "Wireless Mouse", price: 24.99, category: "Electronics" };, destructure name and price, then log "Wireless Mouse costs $24.99" using a template literal.

b) Write a function formatTags(...tags) using a rest parameter that joins any number of string arguments with ", ". Calling formatTags("electronics", "accessories", "sale") should log electronics, accessories, sale.

c) Given const base = { inStock: true };, use the spread operator to build a new object adding name: "Desk Lamp" and price: 34.50, without modifying base. Expected result: { inStock: true, name: 'Desk Lamp', price: 34.5 }.

Exercise 2: Array and Object Methods

Using this array:

javascript
const products = [
  { name: "Wireless Mouse", price: 24.99, inStock: true },
  { name: "Mechanical Keyboard", price: 89.99, inStock: true },
  { name: "Standing Desk", price: 349.00, inStock: false },
  { name: "Desk Lamp", price: 34.50, inStock: true },
];

a) Chain filter() and map() to get the names of every in-stock product. Expected result: [ 'Wireless Mouse', 'Mechanical Keyboard', 'Desk Lamp' ].

b) Chain filter() and reduce() to compute the total price of every in-stock product. Expected result: 149.48.

c) Use find() to get the Mechanical Keyboard’s full object. Expected result: { name: 'Mechanical Keyboard', price: 89.99, inStock: true }.

Exercise 3: Classes and Inheritance

a) Write a Product class with a constructor taking name, price, and sku (stored as a private field #sku with a read-only getter), and a describe() method returning "name: $price" (price to 2 decimal places).

b) Write a DigitalProduct extends Product subclass adding a downloadUrl property, overriding describe() to append " (digital: downloadUrl)", calling super.describe() inside the override.

c) Add a static method Product.fromObject(obj) that constructs a Product from a plain object with name, price, and sku fields.

Expected output for new Product("Wireless Mouse", 24.99, "SKU-001").describe(): Wireless Mouse: $24.99. Expected output for a DigitalProduct named “SQL Basics eBook”, priced 9.99, sku “SKU-002”, downloadUrl “/downloads/sql-basics”: SQL Basics eBook: $9.99 (digital: /downloads/sql-basics).

Exercise 4: Promises and Async/Await

a) Write a function checkStock(productId) returning a promise that resolves with { productId, inStock: productId % 2 === 0 } after a short delay, or rejects with an error if productId is negative.

b) Write an async function reportStock(productId) that awaits checkStock, logging "Product X in stock: true/false" on success, or "Error: <message>" on failure (using try/catch).

c) Call reportStock for product ids 1, 2, and -1, in sequence, using await. Expected output:

text
Product 1 in stock: false
Product 2 in stock: true
Error: invalid product id

d) Use Promise.all() with checkStock to check product ids 3, 4, and 5 concurrently, and log an array of "id:inStock" strings. Expected result: [ '3:false', '4:true', '5:false' ].

Recap

This module covered the modern JavaScript foundation the rest of this course is built on: ES6+ syntax, modules, array and object methods, classes and inheritance, error handling, promises, and async/await. Every one of these appears constantly starting in the very next module.

Next module: Node.js itself, its architecture, the event loop, and its module systems.