Solution
function sum(a: number, b: number): number {
return a + b;
}
console.log(sum(2, 3));
Solution
function isLegal(age: number): boolean {
if (age > 18) {
return true;
} else {
return false
}
}
console.log(isLegal(2));
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;
}
}
- 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>
)
}