Skip to content

Latest commit

Β 

History

History

Week 11.3

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

πŸš€ Week 11 - Day 3: DSA Session 1

πŸ“Œ Topic: Recursion - Factorials, GCD, and Finding Factors

Today, we explored recursion in depth and learned how to solve problems like factorial computation, GCD (Greatest Common Divisor), and finding factors of a number using recursion. πŸ”₯


πŸ”’ Finding the GCD (Greatest Common Divisor) Using Recursion

The GCD (HCF - Highest Common Factor) of two numbers is the largest number that divides both numbers completely.

Approach 1: Iterative Recursive Method

  • Start from the minimum of two numbers and check divisibility.
  • Decrease the divisor (c) until the GCD is found.
let a = 20;
let b = 32;
let c = a < b ? a : b;

console.log(findGCD(a, b, c));

function findGCD(a, b, c) {
    if (c == 1) return c;
    if (a % c == 0 && b % c == 0) return c;
    return findGCD(a, b, c - 1);
}

⏳ Time Complexity: O(min(a, b))


Approach 2: Using Euclidean Algorithm (Optimized)

πŸ”Ή The Euclidean algorithm reduces the problem by subtracting the smaller number from the larger one.
πŸ”Ή This is much faster than iterating from min(a, b) downwards.

console.log(GCD(20, 32));

function GCD(a, b) {
    if (a == b) return a; 
    if (a > b) return GCD(a - b, b);
    else return GCD(a, b - a);
}

πŸ”Ή Time Complexity: O(log(min(a, b)))
βœ” Most efficient way to find GCD using recursion! βœ…


πŸ”„ Printing Numbers Using Recursion

This function prints numbers from n to 1 recursively. πŸ“‰

let n = 5;
temp(n);

function temp(n) {
    if (n == 0) return;
    console.log(n);
    temp(--n);
}

βœ” This function decrements n until 0 is reached.


🧩 Finding Factors of a Number Using Recursion

πŸ”Ή A factor of n is any number that divides n completely.
πŸ”Ή We recursively check divisibility starting from 1.

Method 1: Checking from 1 to n/2

let n = 50;
factor(n, 1);

function factor(n, i) {
    if (n % i == 0) console.log(i); // Print factor
    if (i > Math.floor(n / 2)) {
        console.log(n); // Print n itself as a factor
        return;
    }
    factor(n, i + 1);
}

βœ” Time Complexity: O(n/2)


Method 2: Optimized Factorization (Using sqrt(n))

Instead of checking all numbers up to n/2, we only go up to sqrt(n), which is more efficient.

function printFactors(n) {
    let result = [];
    for (let i = 1; i <= Math.sqrt(n); i++) {
        if (n % i === 0) {
            if (i === n / i) {
                result.push(i);
            } else {
                result.push(i);
                result.push(n / i);
            }
        }
    }
    result.sort((a, b) => a - b);
    console.log(result.join(", "));
}

printFactors(50);

βœ” Time Complexity: O(√n) (Much Faster!)


πŸ”‘ Key Takeaways

βœ… Recursion is a powerful concept that helps break problems into smaller instances of the same problem.
βœ… GCD can be optimized using the Euclidean algorithm.
βœ… Factorization can be optimized by checking only up to sqrt(n) instead of n/2.

πŸ”₯ Keep practicing recursion, and you'll master it in no time! πŸš€πŸ’‘

πŸš€ Week 11 - Day 3: Backend Session 2

πŸ› οΈ Building a User Management System with Express & MongoDB

Today, we built a User Management System using Express, MongoDB (Mongoose), and EJS! πŸŽ‰


πŸ“Œ Steps We Followed

πŸ”Ή Step 1: Initialize the Project

We started by setting up our Node.js project:

npm init -y
npm i ejs mongoose express

This installs:

  • EJS for templating πŸ–ΌοΈ
  • Mongoose for database interaction πŸ›’οΈ
  • Express for handling routes πŸš€

πŸ”Ή Step 2: Create a Simple User Form πŸ“

Our form collects:
βœ” Username
βœ” Email
βœ” Bio
βœ” Profile Image (URL)

πŸ“ Route to Render the Form:

app.get("/create", (req, res) => {
    res.render("createUser");  // Renders EJS form
});

πŸ“ EJS Form (views/createUser.ejs):

<form action="/create" method="POST">
    <input type="text" name="username" placeholder="Enter Username" required />
    <input type="email" name="email" placeholder="Enter Email" required />
    <textarea name="bio" placeholder="Enter Bio"></textarea>
    <input type="text" name="profileImage" placeholder="Enter Profile Image URL" />
    <button type="submit">Create User</button>
</form>

πŸ”Ή Step 3: Store User Data in MongoDB

We post the form data to /create and store it in our MongoDB database.

πŸ“ Mongoose User Schema (models/user.model.js):

const mongoose = require("mongoose");

const userSchema = new mongoose.Schema({
    username: String,
    email: String,
    bio: String,
    profileImage: String
});

const User = mongoose.model("User", userSchema);
module.exports = User;

πŸ“ Handling Form Submission in Express (routes/userRoutes.js)

const User = require("../models/user.model");

app.post("/create", async (req, res) => {
    try {
        const { username, email, bio, profileImage } = req.body;
        await User.create({ username, email, bio, profileImage });
        res.redirect("/users");  // Redirect to users list after creation
    } catch (error) {
        res.status(500).send("Error creating user");
    }
});

πŸ”Ή Step 4: Display All Users

We now fetch all users and display them as cards! πŸ“œ

πŸ“ Fetching Users (routes/userRoutes.js)

app.get("/users", async (req, res) => {
    const users = await User.find();  
    res.render("users", { users });  
});

πŸ“ EJS Template to Show Users (views/users.ejs)

<% users.forEach(user => { %>
    <div class="card">
        <img src="<%= user.profileImage %>" alt="Profile" />
        <h2><%= user.username %></h2>
        <a href="/userDetails/<%= user._id %>">View Profile</a>
    </div>
<% }); %>

πŸ”Ή Step 5: Show User Details

When clicking a user card, it redirects to userDetails/:user_id πŸ“Œ

πŸ“ Route for User Details (routes/userRoutes.js)

app.get("/userDetails/:user_id", async (req, res) => {
    const user = await User.findById(req.params.user_id);
    res.render("userDetails", { user });
});

πŸ“ EJS Template for User Details (views/userDetails.ejs)

<div class="profile">
    <img src="<%= user.profileImage %>" alt="Profile" />
    <h2><%= user.username %></h2>
    <p>Email: <%= user.email %></p>
    <p>Bio: <%= user.bio %></p>
</div>

🎯 Features Implemented Today

βœ… Form to Create Users
βœ… Store Users in MongoDB
βœ… Display Users as Cards
βœ… Click on User Card to View Full Profile

πŸš€ Awesome Work! Keep Learning! πŸ’‘πŸ”₯