Welcome, apprentice, to the JavaScript Koans! This project is designed to help you learn and solidify your understanding of fundamental JavaScript concepts through a series of interactive challenges.
Inspired by traditional Zen Koans, these exercises require you to contemplate and fix "broken" assertions within the code. By replacing the __ placeholders with the correct values, you will mend the code and, in doing so, illuminate your understanding of JavaScript.
This collection guides you through several key areas of JavaScript:
- JavaScript Basics (
js_koan_basics.js): Master the foundational elements likeif/elsestatements,whileandforloops, and function declarations (including arrow functions). - The Blueprint - Classes (
js_koan_classes.js): Delve into Object-Oriented Programming in JavaScript. Understand constructors, methods, inheritance (extends,super), static methods, and private fields (#). - Asynchronous Wisdom (
js_koan_async.js): Explore the non-blocking nature of JavaScript. Learn about Promises,async/await,setTimeout,Promise.all, and making network requests usingWorkspace(or similar). - Handling Requests (
js_koan_express.js): Understand server-side logic with Express.js. Learn about handling different HTTP verbs (POST, PUT, DELETE, GET) and returning appropriate HTTP status codes.
- Node.js: You'll need Node.js installed to run these JavaScript files. Version 18+ is recommended for native
Workspacesupport and modern JavaScript features. You can download it from nodejs.org. - npm: Node.js comes bundled with npm (Node Package Manager), which is used to install project dependencies.
- Terminal/Command Line: Basic familiarity with using your terminal for commands like
cpandnode.
- Clone or Download: Get the project files onto your local machine.
git clone <your-repository-url> # Don't download the ZIP cd <project-directory>
- Install Dependencies: Open your terminal in the project directory and run:
This will install
npm install
chalk(for colorful console output),express, andcors(used by the Koans and the simple server).
-
Copy the Koan File: Before you start editing, make a copy of the Koan file you want to work on. This keeps the original file intact if you need to start over. In your terminal, use the
cpcommand (or manually copy/paste the file using your file explorer):- For the Basics Koan:
cp js_koan_basics.js basics-koan.js - For the Classes Koan:
cp js_koan_classes.js classes-koan.js - For the Async Koan:
cp js_koan_async.js async-koan.js - For the Express Koan:
cp js_koan_express.js express-koan.js
- For the Basics Koan:
-
Open Your Working Copy: Open the copied file (e.g.,
basics-koan.js,express-koan.js) in your code editor. -
Find the Blanks: Locate the lines containing
mustAssert(...)in your copied file. Your task is to replace the__placeholder within the assertion's condition (e.g.,mustAssert(result === __, ...)becomesmustAssert(result === 'expected value', ...)). Do not modify the originaljs_koan_*.jsfiles. -
Run Your Working Copy: Execute the copied file using Node.js from your terminal:
node basics-koan.js
or
node classes-koan.js
or (for the async koan - ensure server.js is running first! See note below.)
node async-koan.js
or (for the express koan - runs its own server internally)
node express-koan.js
-
Observe the Output:
- If you see green messages (decoded from Base64!), your assertion is correct. The path unfolds!
- If you see a red
🛑 Assertion Failed:message, you need to rethink your answer for that specific blank in your copied file.
-
Repeat: Continue filling in the blanks in your copied file and running it until all assertions pass for that Koan. Then, copy the next original Koan file (e.g.,
cp js_koan_classes.js classes-koan.js) and repeat the process.
Special Notes:
-
Async Koan (
async-koan.js):- This Koan includes a section that interacts with a separate, simple web server (
server.js). - Before running
node async-koan.js, you need to start this server. Open a separate terminal window, navigate to the project directory, and run:node server.js
- Keep this server running while you work through
async-koan.js. You should see a message like🔮 Async Koan server listening at http://localhost:1337.
- This Koan includes a section that interacts with a separate, simple web server (
-
Express Koan (
express-koan.js):- This Koan involves creating and interacting with a simple Express.js server within the same file.
- Unlike the Async Koan, you do not need to start a separate server process. Running
node express-koan.jswill handle both the server setup and the test requests automatically.
.
├── README.md # This file
├── js_koan_async.js # Original Koan focusing on asynchronous JavaScript (COPY THIS)
├── js_koan_basics.js # Original Koan covering JS fundamentals (COPY THIS)
├── js_koan_classes.js # Original Koan exploring JS classes (COPY THIS)
├── js_koan_express.js # Original Koan covering Express.js basics (COPY THIS)
├── koan.js # (Likely a helper or utility script - e.g., containing mustAssert)
├── package-lock.json # Records exact dependency versions
├── package.json # Project manifest, lists dependencies (chalk, express, cors)
└── server.js # Simple Express server for the async koan's fetch exercise
# --> You will create these files by copying the originals:
# basics-koan.js
# classes-koan.js
# async-koan.js
# express-koan.js
The original js_koan_basics.js file includes a bonus challenge: Create your own Greeter subclass that shouts the greeting in uppercase. Add and test your creation within your basics-koan.js file!
May your journey be enlightening! Copy the scrolls, fix the assertions, understand the concepts, and emerge a more confident JavaScript developer.