JavaScript and TypeScript course ยท Module 1: JavaScript Basics

Final Project: Dinosaur Registration System

7 min read ยท
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.

// Constant species data (doesn't change after registration)
const speciesName = "Tyrannosaurus Rex";
const speciesCode = "TREX-001";
const registrationDate = "2024-03-15";

// Variable specimen data (can change over time)
let currentWeight = 8500; // in kilograms
let height = 5.2; // in meters
let isHealthy = true;
let currentEnclosure = "Sector A-7";

// Data not yet known
let lastFeedingTime = null;
let veterinaryNotes = undefined;

// Using var for demonstration (in practice we avoid var)
var parkId = 42;

// Displaying data in the console
console.log("=== DINOSAUR REGISTRATION ===");
console.log("Species:", speciesName);
console.log("Code:", speciesCode);
console.log("Weight:", currentWeight, "kg");
console.log("Height:", height, "m");
console.log("Healthy:", isHealthy);
console.log("Enclosure:", currentEnclosure);
console.log("Last feeding:", lastFeedingTime);
console.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.

// Dinosaur parameters
const weight = 8500;
const diet = "carnivore"; // "carnivore", "herbivore", "omnivore"
const aggressionLevel = 9; // scale 1-10

// Threat level classification using if/else
let threatLevel;
let requiresDoubleBarrier;

if (diet === "carnivore" && aggressionLevel > 7) {
  threatLevel = "CRITICAL";
  requiresDoubleBarrier = true;
} else if (diet === "carnivore" && aggressionLevel <= 7) {
  threatLevel = "HIGH";
  requiresDoubleBarrier = true;
} else if (diet === "omnivore") {
  threatLevel = "MEDIUM";
  requiresDoubleBarrier = false;
} else {
  threatLevel = "LOW";
  requiresDoubleBarrier = false;
}

// Size classification using switch
let sizeCategory;
switch (true) {
  case weight > 5000:
    sizeCategory = "colossal";
    break;
  case weight > 1000:
    sizeCategory = "large";
    break;
  case weight > 100:
    sizeCategory = "medium";
    break;
  default:
    sizeCategory = "small";
    break;
}

// Logical operators for special conditions
const needsReinforcement = weight > 5000 || aggressionLevel >= 9;
const canCohabitate = diet !== "carnivore" && aggressionLevel < 5;
const isDocile = !requiresDoubleBarrier && aggressionLevel <= 3;

console.log("Threat level:", threatLevel);
console.log("Size category:", sizeCategory);
console.log("Requires double barrier:", requiresDoubleBarrier);
console.log("Needs reinforcement:", needsReinforcement);
console.log("Can cohabitate with others:", canCohabitate);
console.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.

const dinoName = "Rex";
const baseFood = 150; // kg of meat daily
const growthRate = 1.02; // 2% daily growth

console.log("=== FEEDING REPORT ===");
console.log("Dinosaur:", dinoName);

// for loop - calculating daily requirements
let totalFood = 0;

for (let day = 1; day <= 7; day++) {
  // Each day the dinosaur needs more food
  let dailyFood = baseFood * (growthRate ** (day - 1));
  dailyFood = Math.round(dailyFood * 100) / 100;
  totalFood += dailyFood;

  console.log("Day " + day + ": " + dailyFood + " kg");
}

console.log("Weekly total:", Math.round(totalFood) + " kg");

// while loop - checking warehouse supplies
let stockKg = 2000; // meat supply in warehouse
let daysUntilEmpty = 0;
let dailyConsumption = baseFood;

while (stockKg > 0) {
  stockKg -= dailyConsumption;
  dailyConsumption *= growthRate; // increases over time
  daysUntilEmpty++;
}

console.log("Supplies will last for:", daysUntilEmpty, "days");

// Modulo operator - feeding schedule
for (let hour = 0; hour < 24; hour++) {
  if (hour % 8 === 0) {
    console.log("Hour " + hour + ":00 - FEEDING TIME!");
  }
}

4. System Functions - Code Organization

Each park subsystem should be enclosed in a function. This allows for code reuse and better organization.

// Function to register a dinosaur
function registerDinosaur(name, species, weight, diet) {
  console.log("--- Registration ---");
  console.log("Name:", name);
  console.log("Species:", species);
  console.log("Weight:", weight, "kg");
  console.log("Diet:", diet);

  // Return registration code
  const code = species.substring(0, 3).toUpperCase() + "-" + parkId;
  parkId++; // increment global counter
  return code;
}

// Global variable as counter
var parkId = 1;

// Function to calculate dinosaur BMI
function calculateDinoBMI(weight, height) {
  if (height <= 0) {
    console.log("Error: height must be greater than 0!");
    return null;
  }
  const bmi = weight / (height * height);
  return Math.round(bmi * 100) / 100;
}

// Function to generate security alert
function securityAlert(name, threatLevel, isContained) {
  if (!isContained) {
    console.log("!!! ALARM !!! Dinosaur " + name + " has escaped!");
    console.log("Threat level: " + threatLevel);
    return true; // evacuation required
  }
  console.log(name + " - status: safe");
  return false; // no threat
}

// Multi-line comments describing system logic
/*
  Dinosaur registration system:
  1. Register a new specimen
  2. Calculate its BMI
  3. Check security status
  4. Generate final report
*/

// Using functions
const rexCode = registerDinosaur("Rex", "Tyrannosaurus", 8500, "carnivore");
const triCode = registerDinosaur("Tricia", "Triceratops", 6000, "herbivore");
const veloCode = registerDinosaur("Blue", "Velociraptor", 75, "carnivore");

console.log("Registration codes:", rexCode, triCode, veloCode);

const rexBMI = calculateDinoBMI(8500, 5.2);
const triBMI = calculateDinoBMI(6000, 2.8);
console.log("Rex BMI:", rexBMI);
console.log("Tricia BMI:", triBMI);

// Type conversion in practice
const userInput = "7500"; // form data as string
const numericWeight = Number(userInput);
console.log("Type before conversion:", typeof userInput);
console.log("Type after conversion:", typeof numericWeight);
console.log("Is it a number?", !isNaN(numericWeight));

// Concatenation vs addition
console.log("500" + 100); // "500100" - string concatenation!
console.log(Number("500") + 100); // 600 - correct addition

// Boolean in conditions
const isElectrified = true;
const hasPower = false;
const fenceActive = isElectrified && hasPower;
console.log("Fence active:", fenceActive); // false

Implementation Tips

When creating your own registration system, remember a few key principles:

  1. Choose the appropriate variable declaration type - const for constant values (species, registration date), let for variable values (weight, location), avoid var
  2. Use comments - both single-line (//) and multi-line (/* */) to document system logic
  3. Remember semicolons - although JavaScript allows omitting them thanks to ASI, it's good practice to use them for readability
  4. Convert types consciously - use Number(), String(), Boolean() instead of relying on implicit conversion
  5. Test in the console - console.log(), console.table(), and console.warn() are your best friends when debugging
  6. 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
// Final project: Dinosaur registration system
// Combine all learned concepts: variables, types, conditions, loops, functions

// === 1. SPECIES DATABASE ===
// TODO: Create an array of objects with dinosaur data
// Each dinosaur should have: name, species, weight, diet, isHealthy, enclosure
const parkDinosaurs = [
  // TODO: Add at least 5 dinosaurs
];

// === 2. REGISTRATION FUNCTION ===
// TODO: Create a function registerDinosaur(name, species, weight, diet)
// Should return an object with data + automatically generated ID and registration date
function registerDinosaur(name, species, weight, diet) {
  // TODO: Return a dinosaur object with fields:
  // id mod -(np. "DINO-001"), name, species, weight, diet,
  // isHealthy (true by default), registrationDate
}

// === 3. SPECIES CLASSIFICATION ===
// TODO: Create a function classifyDinosaur(dino) that based on diet and weight
// returns threat level: "critical", "high", "medium", "low"
function classifyDinosaur(dino) {
  // TODO: Use if/else for classification
  // Carnivore > 5000kg = "critical"
  // Carnivore <= 5000kg = "high"
  // Herbivore > 3000kg = "medium"
  // Others = "low"
}

// === 4. PARK REPORT ===
// TODO: Use a loop to iterate over the dinosaur array
// Display report: name, species, threat level
function generateParkReport(dinosaurs) {
  console.log("=== JURASSIC PARK REPORT ===");
  // TODO: Use for...of to iterate
  // For each dinosaur display data and classification
}

// === 5. STATISTICS ===
// TODO: Calculate and display park statistics
// - Total number of dinosaurs
// - Average weight
// - Number of carnivores vs herbivores

// Start the system:
console.log("Dinosaur registration system launched!");

Docs entries used in this lesson

Spotted a mistake in this lesson?

  1. Project to submitLink - InGen Genetics Laboratory
  2. VideoVideo: Module Summary

Useful articles