JavaScript and TypeScript course Β· Module 1: JavaScript Basics
Final Project: Dinosaur Registration System
In this lesson4
Congratulations, young paleontologist! You've reached the end of the first module and it's time to test what you've learned. In this final project, you'll combine all the JavaScript concepts you've learned into one cohesive system - the Jurassic Park Dinosaur Registration System.
Imagine that the genetic laboratory has just cloned a new batch of dinosaurs. As the park's lead programmer, your task is to create a system that registers each new specimen, assigns appropriate parameters, and displays a report in the console. It's like creating the digital DNA of every dinosaur in our database!
Before we get to the code, let's recall the tools at our disposal: variables (var, let, const), data types (string, number, boolean, null, undefined), operators, conditional statements, loops, functions, and the console for displaying results.
Project Goal
You will build a complete dinosaur registration system that:
- Stores dinosaur data in variables of appropriate types
- Uses operators for calculations and comparisons
- Applies conditional statements for species classification
- Uses loops for data processing
- Organizes logic into functions
- Displays readable reports in the console
Functional Requirements
1. Specimen Registration - Variables and Data Types
The first step is creating a set of variables storing dinosaur data. Remember to choose appropriate data types and variable declaration methods.
1// Constant species data (doesn't change after registration)
2const speciesName = "Tyrannosaurus Rex";
3const speciesCode = "TREX-001";
4const registrationDate = "2024-03-15";
5
6// Variable specimen data (can change over time)
7let currentWeight = 8500; // in kilograms
8let height = 5.2; // in meters
9let isHealthy = true;
10let currentEnclosure = "Sector A-7";
11
12// Data not yet known
13let lastFeedingTime = null;
14let veterinaryNotes = undefined;
15
16// Using var for demonstration (in practice we avoid var)
17var parkId = 42;
18
19// Displaying data in the console
20console.log("=== DINOSAUR REGISTRATION ===");
21console.log("Species:", speciesName);
22console.log("Code:", speciesCode);
23console.log("Weight:", currentWeight, "kg");
24console.log("Height:", height, "m");
25console.log("Healthy:", isHealthy);
26console.log("Enclosure:", currentEnclosure);
27console.log("Last feeding:", lastFeedingTime);
28console.log("Veterinary notes:", veterinaryNotes);Note the choice of const vs let - species data doesn't change, but specimen parameters can vary. The value null means an intentional absence of data (not yet fed), while undefined means data not yet entered.
2. Species Classification - Conditional Statements and Operators
The system must automatically classify dinosaurs based on their parameters. Here you'll use comparison operators, logical operators, and conditional statements.
1// Dinosaur parameters
2const weight = 8500;
3const diet = "carnivore"; // "carnivore", "herbivore", "omnivore"
4const aggressionLevel = 9; // scale 1-10
5
6// Threat level classification using if/else
7let threatLevel;
8let requiresDoubleBarrier;
9
10if (diet === "carnivore" && aggressionLevel > 7) {
11 threatLevel = "CRITICAL";
12 requiresDoubleBarrier = true;
13} else if (diet === "carnivore" && aggressionLevel <= 7) {
14 threatLevel = "HIGH";
15 requiresDoubleBarrier = true;
16} else if (diet === "omnivore") {
17 threatLevel = "MEDIUM";
18 requiresDoubleBarrier = false;
19} else {
20 threatLevel = "LOW";
21 requiresDoubleBarrier = false;
22}
23
24// Size classification using switch
25let sizeCategory;
26switch (true) {
27 case weight > 5000:
28 sizeCategory = "colossal";
29 break;
30 case weight > 1000:
31 sizeCategory = "large";
32 break;
33 case weight > 100:
34 sizeCategory = "medium";
35 break;
36 default:
37 sizeCategory = "small";
38 break;
39}
40
41// Logical operators for special conditions
42const needsReinforcement = weight > 5000 || aggressionLevel >= 9;
43const canCohabitate = diet !== "carnivore" && aggressionLevel < 5;
44const isDocile = !requiresDoubleBarrier && aggressionLevel <= 3;
45
46console.log("Threat level:", threatLevel);
47console.log("Size category:", sizeCategory);
48console.log("Requires double barrier:", requiresDoubleBarrier);
49console.log("Needs reinforcement:", needsReinforcement);
50console.log("Can cohabitate with others:", canCohabitate);
51console.log("Docile:", isDocile);3. Feeding Report - Loops and Arithmetic Operators
The system must calculate the feeding requirements for the entire week. Here you'll use for and while loops and arithmetic operators.
1const dinoName = "Rex";
2const baseFood = 150; // kg of meat daily
3const growthRate = 1.02; // 2% daily growth
4
5console.log("=== FEEDING REPORT ===");
6console.log("Dinosaur:", dinoName);
7
8// for loop - calculating daily requirements
9let totalFood = 0;
10
11for (let day = 1; day <= 7; day++) {
12 // Each day the dinosaur needs more food
13 let dailyFood = baseFood * (growthRate ** (day - 1));
14 dailyFood = Math.round(dailyFood * 100) / 100;
15 totalFood += dailyFood;
16
17 console.log("Day " + day + ": " + dailyFood + " kg");
18}
19
20console.log("Weekly total:", Math.round(totalFood) + " kg");
21
22// while loop - checking warehouse supplies
23let stockKg = 2000; // meat supply in warehouse
24let daysUntilEmpty = 0;
25let dailyConsumption = baseFood;
26
27while (stockKg > 0) {
28 stockKg -= dailyConsumption;
29 dailyConsumption *= growthRate; // increases over time
30 daysUntilEmpty++;
31}
32
33console.log("Supplies will last for:", daysUntilEmpty, "days");
34
35// Modulo operator - feeding schedule
36for (let hour = 0; hour < 24; hour++) {
37 if (hour % 8 === 0) {
38 console.log("Hour " + hour + ":00 - FEEDING TIME!");
39 }
40}4. System Functions - Code Organization
Each park subsystem should be enclosed in a function. This allows for code reuse and better organization.
1// Function to register a dinosaur
2function registerDinosaur(name, species, weight, diet) {
3 console.log("--- Registration ---");
4 console.log("Name:", name);
5 console.log("Species:", species);
6 console.log("Weight:", weight, "kg");
7 console.log("Diet:", diet);
8
9 // Return registration code
10 const code = species.substring(0, 3).toUpperCase() + "-" + parkId;
11 parkId++; // increment global counter
12 return code;
13}
14
15// Global variable as counter
16var parkId = 1;
17
18// Function to calculate dinosaur BMI
19function calculateDinoBMI(weight, height) {
20 if (height <= 0) {
21 console.log("Error: height must be greater than 0!");
22 return null;
23 }
24 const bmi = weight / (height * height);
25 return Math.round(bmi * 100) / 100;
26}
27
28// Function to generate security alert
29function securityAlert(name, threatLevel, isContained) {
30 if (!isContained) {
31 console.log("!!! ALARM !!! Dinosaur " + name + " has escaped!");
32 console.log("Threat level: " + threatLevel);
33 return true; // evacuation required
34 }
35 console.log(name + " - status: safe");
36 return false; // no threat
37}
38
39// Multi-line comments describing system logic
40/*
41 Dinosaur registration system:
42 1. Register a new specimen
43 2. Calculate its BMI
44 3. Check security status
45 4. Generate final report
46*/
47
48// Using functions
49const rexCode = registerDinosaur("Rex", "Tyrannosaurus", 8500, "carnivore");
50const triCode = registerDinosaur("Tricia", "Triceratops", 6000, "herbivore");
51const veloCode = registerDinosaur("Blue", "Velociraptor", 75, "carnivore");
52
53console.log("Registration codes:", rexCode, triCode, veloCode);
54
55const rexBMI = calculateDinoBMI(8500, 5.2);
56const triBMI = calculateDinoBMI(6000, 2.8);
57console.log("Rex BMI:", rexBMI);
58console.log("Tricia BMI:", triBMI);
59
60// Type conversion in practice
61const userInput = "7500"; // form data as string
62const numericWeight = Number(userInput);
63console.log("Type before conversion:", typeof userInput);
64console.log("Type after conversion:", typeof numericWeight);
65console.log("Is it a number?", !isNaN(numericWeight));
66
67// Concatenation vs addition
68console.log("500" + 100); // "500100" - string concatenation!
69console.log(Number("500") + 100); // 600 - correct addition
70
71// Boolean in conditions
72const isElectrified = true;
73const hasPower = false;
74const fenceActive = isElectrified && hasPower;
75console.log("Fence active:", fenceActive); // falseImplementation Tips
When creating your own registration system, remember a few key principles:
- Choose the appropriate variable declaration type -
constfor constant values (species, registration date),letfor variable values (weight, location), avoidvar - Use comments - both single-line (
//) and multi-line (/* */) to document system logic - Remember semicolons - although JavaScript allows omitting them thanks to ASI, it's good practice to use them for readability
- Convert types consciously - use
Number(),String(),Boolean()instead of relying on implicit conversion - Test in the console -
console.log(),console.table(), andconsole.warn()are your best friends when debugging - Organize code into functions - each logical operation (registration, classification, report) should be a separate function
Good Luck!
Create your own Dinosaur Registration System in the editor below. Use all the concepts you've learned: variables, data types, operators, conditional statements, loops, and functions. Remember - there's no room for mistakes in Jurassic Park, so test your code thoroughly!
As Dr. Ian Malcolm said: "Life finds a way" - and you've just found your way with JavaScript!
Code for this lesson: index.js
1// Final project: Dinosaur registration system
2// Combine all learned concepts: variables, types, conditions, loops, functions
3
4// === 1. SPECIES DATABASE ===
5// TODO: Create an array of objects with dinosaur data
6// Each dinosaur should have: name, species, weight, diet, isHealthy, enclosure
7const parkDinosaurs = [
8 // TODO: Add at least 5 dinosaurs
9];
10
11// === 2. REGISTRATION FUNCTION ===
12// TODO: Create a function registerDinosaur(name, species, weight, diet)
13// Should return an object with data + automatically generated ID and registration date
14function registerDinosaur(name, species, weight, diet) {
15 // TODO: Return a dinosaur object with fields:
16 // id mod -(np. "DINO-001"), name, species, weight, diet,
17 // isHealthy (true by default), registrationDate
18}
19
20// === 3. SPECIES CLASSIFICATION ===
21// TODO: Create a function classifyDinosaur(dino) that based on diet and weight
22// returns threat level: "critical", "high", "medium", "low"
23function classifyDinosaur(dino) {
24 // TODO: Use if/else for classification
25 // Carnivore > 5000kg = "critical"
26 // Carnivore <= 5000kg = "high"
27 // Herbivore > 3000kg = "medium"
28 // Others = "low"
29}
30
31// === 4. PARK REPORT ===
32// TODO: Use a loop to iterate over the dinosaur array
33// Display report: name, species, threat level
34function generateParkReport(dinosaurs) {
35 console.log("=== JURASSIC PARK REPORT ===");
36 // TODO: Use for...of to iterate
37 // For each dinosaur display data and classification
38}
39
40// === 5. STATISTICS ===
41// TODO: Calculate and display park statistics
42// - Total number of dinosaurs
43// - Average weight
44// - Number of carnivores vs herbivores
45
46// Start the system:
47console.log("Dinosaur registration system launched!");Spotted a mistake in this lesson?