inventory skeleton

This commit is contained in:
Mike 2025-01-08 22:51:48 -05:00
parent 59a08e23a9
commit 75b83e30ea
5 changed files with 198 additions and 150 deletions

View file

@ -1,5 +1,5 @@
let display = document.querySelector('.display');
const buttons = document.querySelectorAll('.btn');
let display = document.querySelector(".display");
const buttons = document.querySelectorAll(".btn");
const ops = {
firstNum: String(),
@ -13,7 +13,7 @@ const ops = {
result: String(),
toggleNumFlag: function () {
this.numFlag = !this.numFlag
this.numFlag = !this.numFlag;
},
clear: function () {
this.firstNum = String();
@ -25,7 +25,7 @@ const ops = {
display.textContent = "0";
},
appendNumber: function (num) {
if(num === "0" && display.textContent.trim() === "0") return
if (num === "0" && display.textContent.trim() === "0") return;
if (!ops.numFlag && ops.checkLength(ops.firstNum)) {
ops.firstNum += num;
updateDisplay(ops.firstNum);
@ -38,7 +38,7 @@ const ops = {
},
checkLength: function (string) {
return string.length < this.stringLength;
}
},
};
function add(x, y) {
@ -55,23 +55,23 @@ function multiply(x, y) {
function divede(x, y) {
if (x === 0) {
return 0
return 0;
} else if (y === 0) {
alert("Cannot divide by 0!");
return "Error"
return "Error";
}
return x / y;
}
function operate(firstNum, secondNum, operator) {
switch (operator) {
case '+':
case "+":
return add(firstNum, secondNum);
case '-':
case "-":
return subtract(firstNum, secondNum);
case '*':
case "*":
return multiply(firstNum, secondNum);
case '/':
case "/":
return divede(firstNum, secondNum);
}
}
@ -79,9 +79,10 @@ function operate(firstNum, secondNum, operator) {
function handleOperation() {
const first = Number(ops.firstNum);
const second = Number(ops.secondNum);
console.log(`last operator: ${ops.operator}`);
let result = operate(first, second, ops.operator);
if (result === "error") {
if (result === "Error") {
ops.clear();
return;
}
@ -92,17 +93,19 @@ function handleOperation() {
// store result
ops.result = result;
if (ops.lastOperator === '=') {
if (ops.operator === "=") {
ops.firstNum = String();
ops.secondNum = String();
ops.numFlag = false;
ops.onSecondNumber = !ops.onSecondNumber;
ops.onSecondNumber = false;
} else {
ops.firstNum = ops.result;
ops.secondNum = "";
ops.operator = "";
ops.onSecondNumber = true;
}
// ops.operator = undefined;
ops.operator = undefined;
ops.lastOperator = undefined;
}
function updateDisplay(displayValue) {
@ -110,8 +113,7 @@ function updateDisplay(displayValue) {
}
function roundThreeDecimals(number) {
if (!Number.isInteger(number) && Number.isFinite(number)
) {
if (!Number.isInteger(number) && Number.isFinite(number)) {
return parseFloat(number.toPrecision(3));
} else {
return number;
@ -119,64 +121,62 @@ function roundThreeDecimals(number) {
}
function handleOperatorClick(operatorClicked) {
ops.lastOperator = operatorClicked;
ops.operator = operatorClicked;
// TODO handle ops for = or clear
if (operatorClicked === 'cls') {
if (operatorClicked === "cls") {
ops.clear();
return;
} else if (operatorClicked === '=') {
ops.lastOperator = '=';
} else if (operatorClicked === "=") {
if (ops.lastOperator) {
ops.operator = ops.lastOperator;
}
handleOperation();
return;
} else if (operatorClicked === '+/-') {
} else if (operatorClicked === "+/-") {
negateNumber();
return;
} else if (operatorClicked === '%') {
} else if (operatorClicked === "%") {
percentageNumber();
return;
} else {
if (ops.secondNum && ops.numFlag) {
ops.lastOperator = operatorClicked;
handleOperation();
return;
}
}
ops.operator = operatorClicked;
//TODO fix this conditional should be if numFlag and a check if = or additional ops
if (!ops.onSecondNumber) {
ops.numFlag = true;
if (ops.result !== "") {
console.log('this is running the ops.result');
console.log("this is running the ops.result");
ops.firstNum = ops.result;
ops.onSecondNumber = true;
return
return;
}
}
ops.onSecondNumber = !ops.onSecondNumber;
// ops.onSecondNumber = !ops.onSecondNumber;
}
function calculator(event) {
if (event.target.dataset.ops === "") {
handleOperatorClick(event.target.value);
} else if (event.target.value === '.') {
if (display.textContent.includes('.')) {
return
} else if (event.target.value === ".") {
if (display.textContent.includes(".")) {
return;
} else {
ops.appendNumber(event.target.value)
ops.appendNumber(event.target.value);
}
} else if (event.target.dataset.num === "") {
ops.appendNumber(event.target.value)
ops.appendNumber(event.target.value);
}
}
function negateNumber() {
// returns a negated number
let negated;
if (!ops.numFlag || !ops.onSecondNumber) {
if (!ops.numFlag || ops.secondNum === "") {
negated = Number(ops.firstNum) * -1;
ops.firstNum = String(negated);
} else {
@ -188,7 +188,7 @@ function negateNumber(){
function percentageNumber() {
let percent;
if ((!ops.numFlag || !ops.onSecondNumber)) {
if (!ops.numFlag || !ops.secondNum === "") {
percent = Number(ops.firstNum) / 100;
ops.firstNum = String(percent);
} else if (ops.result !== "") {
@ -200,6 +200,6 @@ function percentageNumber() {
updateDisplay(percent);
}
buttons.forEach(btn => {
btn.addEventListener('click', calculator);
buttons.forEach((btn) => {
btn.addEventListener("click", calculator);
});

BIN
inventory/bun.lockb Executable file

Binary file not shown.

8
inventory/package.json Normal file
View file

@ -0,0 +1,8 @@
{
"dependencies": {
"ejs": "^3.1.10",
"express": "^4.21.2",
"express-validator": "^7.2.1",
"pg": "^8.13.1"
}
}

31
inventory/src/app.js Normal file
View file

@ -0,0 +1,31 @@
const express = require("express");
const path = require("node:path");
const app = express();
const port = 8080;
const { indexRouter } = require("./routes/indexRouter");
const assetsPath = path.join(path.dirname(__dirname), "public");
app.set("views", path.join(__dirname, "views"));
app.set(express.static(assetsPath));
app.use(express.urlencoded({ extended: true }));
app.use("/", indexRouter);
const server = app.listen(port, () => {
console.log(`Server started at ${port}`);
});
const gracefulShutdownHandler = (signal) => {
console.log(`Caught ${signal}, gracefully shutting down`);
server.close(() => {
console.log("shutting down server.");
process.exit();
});
};
process.on("SIGINT", gracefulShutdownHandler);
process.on("SIGTERM", gracefulShutdownHandler);

View file

@ -0,0 +1,9 @@
const { Router } = require("express");
const indexRouter = Router();
indexRouter.get("/", (req, res) => {
res.send("index");
});
module.exports = { indexRouter };