DeveloperBreeze

Simple Calculator

// Function to perform basic arithmetic operations
function calculate(a, b, operator) {
    switch (operator) {
        case '+':
            return a + b;
        case '-':
            return a - b;
        case '*':
            return a * b;
        case '/':
            return a / b;
        default:
            return 'Invalid operator';
    }
}

// Example: Perform addition (10 + 5)
const result = calculate(10, 5, '+');
console.log('Calculator Result:', result);

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

  • If the DATABASE_URL starts with postgres:// (Heroku's default), it replaces it with the correct format, postgresql+psycopg2://, making the URI compatible with SQLAlchemy.

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

No preview available for this content.

Oct 25, 2024
Read More
Code
bash

How to Paste in an SSH Terminal Without a Mouse

No preview available for this content.

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

Run the server using Node.js:

   node index.js

Aug 12, 2024
Read More
Code
javascript

React Custom Hook for API Requests

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;
  • Reusability: The useFetch hook can be used across different components and applications, reducing code duplication and simplifying API interaction.
  • Loading and Error States: Automatically manages loading and error states, providing a consistent way to handle asynchronous operations.
  • Cleanup Handling: Prevents state updates on unmounted components, reducing potential memory leaks and ensuring stability.

Aug 12, 2024
Read More
Code
csharp

Unity Inventory System using Scriptable Objects

A basic setup for displaying the inventory items in the Unity UI.

using UnityEngine;
using UnityEngine.UI;

public class InventoryUI : MonoBehaviour
{
    public Inventory inventory;
    public GameObject inventoryPanel;
    public GameObject inventorySlotPrefab;

    void Start()
    {
        RefreshInventoryUI();
    }

    public void RefreshInventoryUI()
    {
        // Clear existing UI elements
        foreach (Transform child in inventoryPanel.transform)
        {
            Destroy(child.gameObject);
        }

        // Create new UI elements
        foreach (Item item in inventory.items)
        {
            GameObject slot = Instantiate(inventorySlotPrefab, inventoryPanel.transform);
            Image iconImage = slot.transform.GetChild(0).GetComponent<Image>();
            iconImage.sprite = item.icon;

            // Add more UI logic as needed (like item count for stackable items)
        }
    }
}

Aug 12, 2024
Read More
Code
csharp

Unity Player Controller Blueprint

using UnityEngine;

// Basic player controller for Unity
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    [Header("Player Settings")]
    public float moveSpeed = 5f;
    public float jumpHeight = 2f;
    public float gravity = -9.81f;
    public Transform cameraTransform;

    private CharacterController characterController;
    private Vector3 velocity;
    private bool isGrounded;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        MovePlayer();
        RotateCamera();
    }

    void MovePlayer()
    {
        // Check if the player is on the ground
        isGrounded = characterController.isGrounded;
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f; // A small negative value to keep the player grounded
        }

        // Get input for movement
        float moveX = Input.GetAxis("Horizontal");
        float moveZ = Input.GetAxis("Vertical");

        // Calculate movement direction
        Vector3 move = transform.right * moveX + transform.forward * moveZ;
        characterController.Move(move * moveSpeed * Time.deltaTime);

        // Jumping logic
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        // Apply gravity
        velocity.y += gravity * Time.deltaTime;
        characterController.Move(velocity * Time.deltaTime);
    }

    void RotateCamera()
    {
        // Get input for mouse movement
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");

        // Rotate player along the y-axis
        transform.Rotate(Vector3.up * mouseX);

        // Rotate camera along the x-axis
        cameraTransform.Rotate(Vector3.left * mouseY);
    }
}
  • Animation Integration: Add animator components and trigger animations based on movement and jump states.
  • Advanced Physics: Integrate more complex physics interactions, such as slopes or surface friction.
  • Networking: Adapt the controller for multiplayer environments using Unity’s networking solutions.

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

No preview available for this content.

Aug 12, 2024
Read More
Code
html

HTML: Basic Template Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Hello, World!</h1>
    <script src="script.js"></script>
</body>
</html>

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

No preview available for this content.

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!