Loading...
More TypeScript Posts
interface Task {id: number;title: string;completed: boolean;}class TaskManager {private tasks: Task[] = [];constructor() {}addTask(title: string) {const taskId = this.tasks.length + 1;const task: Task = {id: taskId,title,completed: false,};this.tasks.push(task);}markTaskAsComplete(taskId: number) {const task = this.tasks.find((task) => task.id === taskId);if (task) {task.completed = true;}}markTaskAsIncomplete(taskId: number) {const task = this.tasks.find((task) => task.id === taskId);if (task) {task.completed = false;}}listTasks() {console.log('Tasks:');this.tasks.forEach((task) => {console.log(`${task.id}. [${task.completed ? 'X' : ' '}] ${task.title}`);});}}// Example usage:const taskManager = new TaskManager();taskManager.addTask('Buy groceries');taskManager.addTask('Pay bills');taskManager.addTask('Clean the house');taskManager.listTasks();// Output:// Tasks:// 1. [ ] Buy groceries// 2. [ ] Pay bills// 3. [ ] Clean the house
function binarySearch(nums: number[], target: number): number {let left = 0;let right = nums.length - 1;while (left <= right) {const mid = left + Math.floor((right - left) / 2);if (nums[mid] === target) {return mid;} else if (nums[mid] < target) {left = mid + 1;} else {right = mid - 1;}}return left;}function searchInsert(nums: number[], target: number): number {return binarySearch(nums, target);};
function removeElement(nums: number[], val: number): number {let zeroStartIndex = 0;for(let i = 0; i < nums.length; i++) {if(nums[i] !== val) {nums[zeroStartIndex] = nums[i];zeroStartIndex++;}}return zeroStartIndex;};
// Print Hello, World!console.log("Hello, World!");
const getFollowers = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },include: { followedBy: true },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}const followers = user.followedBy.map(followedBy => {return {followerId: followedBy.followerId,};});const followersCount = followers.length;res.json({ followersCount: followersCount, followers });} catch (error) {console.error('getFollowers() error: ', error);res.status(500).json({error: `There was an error fetching followers for user with username "${req.params.username}", please try again later`,});}};const getFollowing = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },include: { following: true },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}const following = user.following.map(following => {return {followingId: following.followingId,};});const followingCount = following.length;res.json({ followingCount: followingCount, following });} catch (error) {console.error('getFollowing() error: ', error);res.status(500).json({error: `There was an error fetching who is following "${req.params.username}", please try again later`,});}};const followUser = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}// dont allow a user to follow themselvesif (req.user.id === user.id) {res.status(400).json({ error: 'You cannot follow yourself' });return;}//check if a follow relationship already existsconst followrelationship = await prisma.follows.findUnique({where: {followerId_followingId: {followerId: req.user.id,followingId: user.id,},},});// check if the follow relationship already existsif (followrelationship) {res.status(400).json({ error: 'You are already following this user' });return;}// create the follow relationshipconst follow = await prisma.follows.create({data: {followerId: req.user.id,followingId: user.id,},});res.json({ follow });} catch (error) {console.error('followUser() error: ', error);res.status(500).json({error: `There was an error following "${req.params.username}", please try again later`,});}};const unfollowUser = async (req: Request, res: Response) => {try {const username = req.params.username || '';const user = await User.findUnique({where: { username },});if (!user) {res.status(404).json({error: 'User could not be found',});return;}// dont allow a user to unfollow themselvesif (req.user.id === user.id) {res.status(400).json({ error: 'You cannot unfollow yourself' });return;}//check if a follow relationship already existsconst followrelationship = await prisma.follows.findUnique({where: {followerId_followingId: {followerId: req.user.id,followingId: user.id,},},});// check if the follow relationship already existsif (!followrelationship) {res.status(400).json({ error: 'You are not following this user' });return;}// delete the follow relationshipconst follow = await prisma.follows.delete({where: {followerId_followingId: {followerId: req.user.id,followingId: user.id,},},});res.json({ follow });} catch (error) {console.error('unfollowUser() error: ', error);res.status(500).json({error: `There was an error unfollowing "${req.params.username}", please try again later`,});}};
function removeDuplicates(nums: number[]): number {nums.splice(0, nums.length, ...Array.from(new Set(nums)));return nums.length;};