Files
computing-box/Export/assets/js/logicGates.js
Alexander Davis d4441afd55 Wave 2 Beta Release
 Wave 2 Beta Release: New Logic Gates, Improved UX, and Major Enhancements! 

---

## **🆕 New Features**
### **1️⃣ Additional Logic Gates**
#### **🔲 NAND Gate**
- **Fully functional NAND logic** implemented.
- **Truth Table**:
  | **Input A** | **Input B** | **Output (A NAND B)** |
  |-------------|-------------|-----------------------|
  | 0           | 0           | **1** (ON)            |
  | 0           | 1           | **1** (ON)            |
  | 1           | 0           | **1** (ON)            |
  | 1           | 1           | **0** (OFF)           |
- **Toggle buttons** for Input 1 and Input 2 work as expected.
- Lightbulb updates correctly to match the logic output.

#### **🔲 NOR Gate**
- **Fully functional NOR logic** implemented.
- **Truth Table**:
  | **Input A** | **Input B** | **Output (A NOR B)** |
  |-------------|-------------|----------------------|
  | 0           | 0           | **1** (ON)           |
  | 0           | 1           | **0** (OFF)          |
  | 1           | 0           | **0** (OFF)          |
  | 1           | 1           | **0** (OFF)          |
- Lightbulb updates correctly to match the logic output.
- Toggle buttons for Input 1 and Input 2 work as expected.

#### **🔲 XOR Gate**
- **Fully functional XOR logic** implemented.
- **Truth Table**:
  | **Input A** | **Input B** | **Output (A XOR B)** |
  |-------------|-------------|----------------------|
  | 0           | 0           | **0** (OFF)          |
  | 0           | 1           | **1** (ON)           |
  | 1           | 0           | **1** (ON)           |
  | 1           | 1           | **0** (OFF)          |
- Toggle buttons for Input 1 and Input 2 trigger the logic updates.
- Lightbulb behavior reflects the XOR output logic.

#### **🔲 XNOR Gate**
- **Fully functional XNOR logic** implemented.
- **Truth Table**:
  | **Input A** | **Input B** | **Output (A XNOR B)** |
  |-------------|-------------|-----------------------|
  | 0           | 0           | **1** (ON)            |
  | 0           | 1           | **0** (OFF)           |
  | 1           | 0           | **0** (OFF)           |
  | 1           | 1           | **1** (ON)            |
- Lightbulb updates correctly based on the logic.
- Input toggle buttons work correctly for Input 1 and Input 2.

---

## **🛠️ Enhancements & Improvements**
- **Input Handling**:
  - **Input toggles** now work consistently across all logic gate types (AND, OR, NOT, XOR, NOR, NAND, XNOR).
  - Input logic is consistent across all gate types, including the lightbulb status updates.

- **Reset Functionality**:
  - The **reset button** now works across all logic gates.
  - The lightbulb and toggle button status are properly reset.

- **Code Refactoring**:
  - Reduced **redundancy** in the toggle input functions.
  - Enhanced **code readability** and maintainability.
  - Removed unnecessary logic duplications and consolidated shared logic for gate input handling.

---

## **🐛 Bug Fixes**
- **Fixed NAND Gate logic** — lightbulb now correctly updates according to the truth table.
- **Fixed XNOR Gate logic** — logic now correctly handles the logic for matching inputs.
- **Fixed Reset Functionality** — lightbulbs and buttons now reset correctly across all gate types.
- **General Bug Fixes** — Minor improvements in input toggle functions for better user experience.

---

## **📁 Files Changed**
📄 logicGates.js 📄 logicGates.html

---

## **🚀 Release Notes**
This release marks the **Wave 2 Beta** of the CS:Box project, bringing support for all fundamental logic gates. This wave includes the **NAND, NOR, XOR, and XNOR gates**, which have been built with consistent logic, responsive lightbulb behavior, and proper input handling. This release also introduces major fixes to the **reset functionality**, ensuring that all gates reset properly when the reset button is pressed.

**New Features Include:**
- Full support for **NAND, NOR, XOR, XNOR logic gates**.
- Input toggle buttons now work seamlessly for all gates.
- Lightbulb status updates in real-time according to input changes.
- **Reset button** now clears all input states and correctly resets the lightbulb status for each gate type.

**Looking Ahead**:
- Further UI improvements to make the interface more user-friendly.
- Expanded testing to ensure accurate logic across all devices.

Enjoy this latest version of CS:Box! 🚀
2024-12-07 23:46:18 +00:00

106 lines
3.1 KiB
JavaScript

let inputs = {
input1: false,
input2: false
};
let gateValue = false;
// ** Toggle input (handles both input1 and input2) **
function toggleInput(inputNumber) {
const inputKey = `input${inputNumber}`;
inputs[inputKey] = !inputs[inputKey];
updateInputState(`swtInput${inputNumber}`, inputs[inputKey]);
updateGate();
}
// ** Update the gate's state based on the current inputs and gate type **
function updateGate() {
const pageHeading = document.getElementById("pageHeading").textContent;
gateValue = evaluateGate(pageHeading);
updateGateLight(pageHeading, gateValue);
}
// ** Evaluate the gate logic **
function evaluateGate(pageHeading) {
const { input1, input2 } = inputs;
switch (pageHeading) {
case "AND Gate":
return input1 && input2;
case "OR Gate":
return input1 || input2;
case "NOT Gate":
return !input1; // NOT gate only uses Input1
case "NAND Gate":
return !(input1 && input2); // Correct NAND logic
case "NOR Gate":
return !(input1 || input2);
case "XOR Gate":
return input1 !== input2; // XOR is true if inputs are different
case "XNOR Gate":
return input1 === input2; // XNOR is true if inputs are the same
default:
console.error("Unknown Gate Type");
return false;
}
}
// ** Update the lightbulb based on the gate's value **
function updateGateLight(pageHeading, value) {
const lightBulbId = getLightBulbId(pageHeading);
const lightBulb = document.getElementById(lightBulbId);
if (lightBulb) {
lightBulb.classList.toggle("poweredOn", value);
lightBulb.classList.toggle("poweredOff", !value);
}
}
// ** Get the correct lightbulb ID based on the gate type **
function getLightBulbId(pageHeading) {
switch (pageHeading) {
case "AND Gate":
return "blbAndGate";
case "OR Gate":
return "blbOrGate";
case "NOT Gate":
return "blbNotGate";
case "NAND Gate":
return "blbNandGate";
case "NOR Gate":
return "blbNorGate";
case "XOR Gate":
return "blbXorGate";
case "XNOR Gate":
return "blbXnorGate";
default:
console.error("Unknown Gate Type");
return null;
}
}
// ** Update the toggle switch to reflect its active/inactive state **
function updateInputState(switchId, isActive) {
const toggleSwitch = document.getElementById(switchId);
if (toggleSwitch) {
toggleSwitch.classList.toggle("btnActive", isActive);
}
}
// ** Reset the gate to its default state **
function resetGate() {
inputs.input1 = false;
inputs.input2 = false;
updateInputState("swtInput1", inputs.input1);
updateInputState("swtInput2", inputs.input2);
const pageHeading = document.getElementById("pageHeading").textContent;
if (pageHeading === "NOT Gate") {
// For NOT Gate, the light should be on by default
gateValue = true;
updateGateLight(pageHeading, gateValue);
} else {
updateGate();
}
}