Skip to content

Codify-Education-Program/intro-to-ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧠 HW 3 — Intro to TypeScript

Welcome to your second assignment in Full-Stack Development!
You’ll take what you learned from JavaScript and now write safer, cleaner, and more reliable code with TypeScript.


⚙️ Setup

Make sure you have Node.js (v18 or newer) and TypeScript installed.

Check your Node version:

node -v

Install TypeScript globally (only once):

npm install -g typescript

To compile a TypeScript file:

tsc Problem1.ts

Run the compiled JavaScript file:

node Problem1.js

Or, if you prefer one command:

npx ts-node Problem1.ts

📁 Folder Structure

typescript-assignment-2/
├── Problem1.ts
├── Problem2.ts
├── Problem3.ts
├── Problem4.ts
├── Problem5.ts
└── Problem6.ts

Each problem file is stand-alone — no imports, no exports, no frameworks. Open them in VS Code, write your solution inside the given function, and test using the console.log examples.


📚 Problems Overview

🧩 Problem 1 – concatNumbers

Write a function that takes two numbers, converts them to strings, and concatenates them.

concatNumbers(4, 5);   // "45"
concatNumbers(5, -9);  // "5-9"

🧩 Problem 2 – arrayOfStudents

Given two arrays (names and ages), return an array of student objects.

[
  { name: "Joe", age: 50 },
  { name: "Schmo", age: 60 }
]

🧩 Problem 3 – studentsTaughtByProf

Given a Professor object containing classes and students, return all unique student names sorted by age (ascending).


🧩 Problem 4 – allAthletes

Given an array of Student or Athlete objects, return a new array where all become Athletes. Existing athletes keep their original sport; new ones get the default sport you provide.


🧩 Problem 5 – countOccurrences

Write a generic function that counts how many times a value appears in an array.

countOccurrences([2, 4, 6, 6, 8], 6);  // 2
countOccurrences([true, true, true], false);  // 0

🧩 Problem 6 – verifyUser

Given a possibly incomplete user object, return either:

  • the user itself (if all properties exist), or
  • an array of missing property names.

Also handle null or undefined safely.


🧪 Testing

Every file already includes small example tests at the bottom like:

console.log(concatNumbers(4, 5));

Run each one manually:

npx ts-node Problem1.ts

No external testing libraries needed.


💡 Tips

  • Always give explicit parameter and return types.
  • Use type aliases to define object shapes.
  • Use ? for optional properties.
  • Use union (|) and intersection (&) types when combining data.
  • Experiment with type narrowing using typeof.

✅ Learning Goals

By completing this assignment, you should be able to:

✔️ Understand how TypeScript checks types at compile-time ✔️ Define and reuse custom types ✔️ Work safely with optional data ✔️ Create generic and reusable functions ✔️ Write JavaScript code that’s more predictable and self-documenting


🏁 Submission

When finished, push your completed .ts files to your GitHub Classroom repository. Ensure all files compile cleanly (no red squiggles or compiler errors) before submitting.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published