Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
"browser": true,
"es2021": true,
"mocha": true,
"request": true
"node": true
},
"extends": "eslint:recommended",
"overrides": [
Expand Down
5 changes: 3 additions & 2 deletions api/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exports.calculate = function(req, res) {
'subtract': function(a, b) { return a - b },
'multiply': function(a, b) { return a * b },
'divide': function(a, b) { return a / b },
'modulo': function(a, b) { return a % b },
};

if (!req.query.operation) {
Expand All @@ -29,13 +30,13 @@ exports.calculate = function(req, res) {
}

if (!req.query.operand1 ||
!req.query.operand1.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
!req.query.operand1.match(/^(-)?[0-9.]+(e(-)?[0-9]+)?$/) ||
req.query.operand1.replace(/[-0-9e]/g, '').length > 1) {
throw new Error("Invalid operand1: " + req.query.operand1);
}

if (!req.query.operand2 ||
!req.query.operand2.match(/^(-)?[0-9\.]+(e(-)?[0-9]+)?$/) ||
!req.query.operand2.match(/^(-)?[0-9.]+(e(-)?[0-9]+)?$/) ||
req.query.operand2.replace(/[-0-9e]/g, '').length > 1) {
throw new Error("Invalid operand2: " + req.query.operand2);
}
Expand Down
42 changes: 41 additions & 1 deletion public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,38 @@ var operand1 = 0;
var operand2 = 0;
var operation = null;

// Theme management
const themes = ['default', 'dark', 'light', 'colorful'];
let currentThemeIndex = 0;

function initializeTheme() {
const savedTheme = localStorage.getItem('calculatorTheme');
if (savedTheme && themes.includes(savedTheme)) {
currentThemeIndex = themes.indexOf(savedTheme);
applyTheme(savedTheme);
} else {
applyTheme(themes[0]);
}

// Add theme toggle event listener
document.getElementById('themeToggle').addEventListener('click', toggleTheme);
}

function toggleTheme() {
currentThemeIndex = (currentThemeIndex + 1) % themes.length;
const newTheme = themes[currentThemeIndex];
applyTheme(newTheme);
localStorage.setItem('calculatorTheme', newTheme);
}

function applyTheme(theme) {
if (theme === 'default') {
document.documentElement.removeAttribute('data-theme');
} else {
document.documentElement.setAttribute('data-theme', theme);
}
}

function calculate(operand1, operand2, operation) {
var uri = location.origin + "/arithmetic";

Expand All @@ -33,6 +65,9 @@ function calculate(operand1, operand2, operation) {
case '/':
uri += "?operation=divide";
break;
case '%':
uri += "?operation=modulo";
break;
default:
setError();
return;
Expand Down Expand Up @@ -138,7 +173,7 @@ document.addEventListener('keypress', (event) => {
numberPressed(event.key);
} else if (event.key == '.') {
decimalPressed();
} else if (event.key.match(/^[-*+/]$/)) {
} else if (event.key.match(/^[-*+/%]$/)) {
operationPressed(event.key);
} else if (event.key == '=') {
equalPressed();
Expand Down Expand Up @@ -198,3 +233,8 @@ function setLoading(loading) {
buttons[i].disabled = loading;
}
}

// Initialize theme when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
initializeTheme();
});
Loading