Skip to content

Latest commit

 

History

History
101 lines (80 loc) · 1.76 KB

14.1 - TypeScript Part 1 Assignment.md

File metadata and controls

101 lines (80 loc) · 1.76 KB

Week 14 - 14.1 | TypeScript Part 1

Assignment #1 - Write a function that calculates the sum of two arguments

Solution
function sum(a: number, b: number): number {
    return a + b;
}

console.log(sum(2, 3));

Assignment #2 - Write a function that return true or false based on if a user is 18+

Solution
function isLegal(age: number): boolean {
    if (age > 18) {
        return true;
    } else {
        return false
    }
}

console.log(isLegal(2));

Assignment #3 - Create a function that takes another function as input, and runs it after 1 second.

Solution
function delayedCall(fn: () => void) {
    setTimeout(fn, 1000);
}

delayedCall(function() {
    console.log("Hello Baccho...");
})

Assignment #4 - Create a function isLegal that returns true or false if a user is above 18. It takes a user as an input.

Solution
interface User {
	firstName: string;
	lastName: string;
	email: string;
	age: number;
}

function isLegal(user: User): boolean {
    if (user.age > 18) {
        return true
    } else {
        return false;
    }
}

Assignment #5 - Create a React component that takes todos as an input and renders them

  • Note - Select typescript when initialising the react project using npm create vite@latest
Solution
// Todo.tsx
interface TodoType {
  title: string;
  description: string;
  done: boolean;
}

interface TodoInput {
  todo: TodoType;
}

function Todo({ todo }: TodoInput) {
  return (
    <div>
        <h1>{todo.title}</h1>
        <h2>{todo.description}</h2>    
    </div>
  )
}