DeveloperBreeze

JavaScript Closure for Creating a Counter

javascript
// Function to create a counter using closure
function createCounter() {
    let count = 0;

    // Closure: Inner function maintains access to the outer function's variable
    return function() {
        count++;
        return count;
    };
}

// Create a counter using the createCounter function
const counter = createCounter();

// Usage: Increment and display the counter value
console.log(counter()); // Output: 1
console.log(counter()); // Output: 2

Continue Reading

Discover more amazing content handpicked just for you

Code

How to Create a New MySQL User with Full Privileges

No preview available for this content.

May 01, 2025
Read More
Code
python

Configuring SQLAlchemy with PostgreSQL on Heroku: A Quick Guide

Instead of manually replacing the URI, you can use libraries like dj-database-url to parse and adapt the DATABASE_URL for different frameworks or drivers. However, the manual approach is straightforward and works well for most cases.

Nov 08, 2024
Read More
Code
php

How to Delete All WordPress Posts and Their Uploads Using a Custom PHP Script

No preview available for this content.

Oct 25, 2024
Read More
Code
php

How To enable all error debugging in PHP

  • error_reporting(E_ALL);: Enables reporting for all levels of errors.
  • ini_set('display_errors', 1);: Ensures that errors are displayed in the browser.
  • If you prefer to log the errors instead of displaying them, you can uncomment the log_errors and error_log lines, specifying the path where errors should be logged.

Oct 25, 2024
Read More
Code
bash

How to Paste in an SSH Terminal Without a Mouse

  • Copy: Ctrl + Shift + C to copy text.
  • Paste:
  • Ctrl + Shift + V
  • Alternatively: Cmd + V (macOS) or Shift + Insert (Linux).
  • Copy: Select text (it automatically copies to the clipboard).
  • Paste: Right-click or Shift + Insert.

Oct 20, 2024
Read More
Note
javascript nodejs

Vite vs Webpack

No preview available for this content.

Aug 14, 2024
Read More
Code
nodejs graphql

GraphQL API Server with Node.js and Apollo Server

const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');

// Sample data
let books = [
    { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' },
    { title: 'To Kill a Mockingbird', author: 'Harper Lee' },
];

// GraphQL schema definition
const typeDefs = gql`
    type Book {
        title: String!
        author: String!
    }

    type Query {
        books: [Book]
    }

    type Mutation {
        addBook(title: String!, author: String!): Book
    }
`;

// GraphQL resolvers
const resolvers = {
    Query: {
        books: () => books,
    },
    Mutation: {
        addBook: (_, { title, author }) => {
            const newBook = { title, author };
            books.push(newBook);
            return newBook;
        },
    },
};

// Create Apollo server
const server = new ApolloServer({ typeDefs, resolvers });

const app = express();
server.applyMiddleware({ app });

app.listen({ port: 4000 }, () =>
    console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`)
);
  • Schema Definition: The typeDefs defines a simple schema with a Book type and queries to fetch books and add a new book.
  • Resolvers: Functions that resolve the queries and mutations. In this case, they return all books and add a new book to the list.

Aug 12, 2024
Read More
Code
javascript

React Custom Hook for API Requests

Here’s an example of how to use the useFetch hook in a React component to fetch and display data.

import React from 'react';
import useFetch from './useFetch'; // Ensure correct import path

function UserList() {
    const { data, loading, error } = useFetch('https://jsonplaceholder.typicode.com/users');

    if (loading) return <p>Loading...</p>;
    if (error) return <p>Error: {error.message}</p>;

    return (
        <ul>
            {data.map(user => (
                <li key={user.id}>{user.name}</li>
            ))}
        </ul>
    );
}

export default UserList;

Aug 12, 2024
Read More
Code
csharp

Unity Inventory System using Scriptable Objects

No preview available for this content.

Aug 12, 2024
Read More
Code
csharp

Unity Player Controller Blueprint

No preview available for this content.

Aug 12, 2024
Read More
Code
bash

Bash: How to Loop Over Files in a Directory

No preview available for this content.

Aug 12, 2024
Read More
Code
csharp

C#: How to Read a File

using System;
using System.IO;

class ReadFileExample
{
    static void Main()
    {
        string path = @"C:\example.txt";
        string text = File.ReadAllText(path);
        Console.WriteLine(text);
    }
}

Aug 12, 2024
Read More
Code
html

HTML: Basic Template Structure

No preview available for this content.

Aug 12, 2024
Read More
Code
php

PHP: How to Connect to a MySQL Database

No preview available for this content.

Aug 12, 2024
Read More
Code
css

CSS: How to Center a Div Horizontally and Vertically

No preview available for this content.

Aug 12, 2024
Read More
Code
java

Java: How to Sort a List

No preview available for this content.

Aug 12, 2024
Read More
Code
javascript nodejs

Node.js: How to Create an HTTP Server

No preview available for this content.

Aug 12, 2024
Read More
Code
sql

SQL: How to Select Top N Records

-- For SQL Server
SELECT TOP 10 * FROM Customers;

-- For MySQL and PostgreSQL
SELECT * FROM Customers LIMIT 10;

-- For Oracle
SELECT * FROM (SELECT * FROM Customers) WHERE ROWNUM <= 10;

Aug 12, 2024
Read More
Code
python

Python: How to Reverse a String

No preview available for this content.

Aug 12, 2024
Read More
Code
javascript json

How to Deep Clone a JavaScript Object

No preview available for this content.

Aug 12, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!