diff --git a/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/app.css b/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/app.css new file mode 100644 index 0000000..180f52d --- /dev/null +++ b/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/app.css @@ -0,0 +1,89 @@ +* { + box-sizing: border-box; + font-family: 'Roboto', open-sans; + margin: 0; + padding: 0; + box-sizing: border-box; + } + + header { + background: #023d6d; + color: white; + + padding: 1rem; + + text-align: center; + + box-shadow: 0 2px 8px rgba(0, 0, 0, 0.26); + + width: 100%; + } + + #results, + #calculator { + margin: 2rem auto; + width: 40rem; + max-width: 90%; + border: 1px solid #023d6d; + border-radius: 10px; + padding: 1rem; + + color: #023d6d; + } + + #results { + text-align: center; + } + + #calculator input { + font: inherit; + font-size: 3rem; + + border: 2px solid #023d6d; + width: 10rem; + padding: 0.15rem; + margin: auto; + + display: block; + + color: #023d6d; + + text-align: center; + } + + #calculator input:focus { + outline: none; + } + + #calculator button { + font: inherit; + + background: #023d6d; + color: white; + + border: 1px solid #023d6d; + padding: 1rem; + + cursor: pointer; + } + + #calculator button:focus { + outline: none; + } + + #calculator button:hover, + #calculator button:active { + background: #084f88; + border-color: #084f88; + } + + #calc-actions button { + width: 4rem; + } + + #calc-actions { + margin-top: 1rem; + + text-align: center; + } + \ No newline at end of file diff --git a/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/app.js b/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/app.js new file mode 100644 index 0000000..6b5735b --- /dev/null +++ b/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/app.js @@ -0,0 +1,89 @@ +const defaultResult = 0; +let currentResult = defaultResult; +let logEntries = []; + +// Gets input from input field +function getUserNumberInput() { + return parseInt(usrInput.value); +} + +// Generates and writes calculation log +function createAndWriteOutput(operator, resultBeforeCalc, calcNumber) { + const calcDescription = `${resultBeforeCalc} ${operator} ${calcNumber}`; + outputResult(currentResult, calcDescription); // from vendor file +} + +function writeToLog( + operationIdentifier, + prevResult, + operationNumber, + newResult +) { + const logEntry = { + operation: operationIdentifier, + prevResult: prevResult, + number: operationNumber, + result: newResult + }; + logEntries.push(logEntry); + console.log(logEntries); +} + +function calculateResult(calculationType) { + const enteredNumber = getUserNumberInput(); + if ( + calculationType !== 'ADD' && + calculationType !== 'SUBTRACT' && + calculationType !== 'MULTIPLY' && + calculationType !== 'DIVIDE' + ) + return; + + // if ( + // calculationType === 'ADD' || + // calculationType === 'SUBTRACT' || + // calculationType === 'MULTIPLY' || + // calculationType === 'DIVIDE' + // ) { + + const initialResult = currentResult; + let mathOperator; + if (calculationType === 'ADD') { + currentResult += enteredNumber; + mathOperator = '+'; + } else if (calculationType === 'SUBTRACT') { + currentResult -= enteredNumber; + mathOperator = '-'; + } else if (calculationType === 'MULTIPLY') { + currentResult *= enteredNumber; + mathOperator = '*'; + } else if (calculationType === 'DIVIDE') { + currentResult /= enteredNumber; + mathOperator = '/'; + } + + createAndWriteOutput(mathOperator, initialResult, enteredNumber); + writeToLog(calculationType, initialResult, enteredNumber, currentResult); + // } +} + +function add() { + calculateResult('ADD'); +} + +function subtract() { + calculateResult('SUBTRACT'); +} + +function multiply() { + calculateResult('MULTIPLY'); +} + +function divide() { + calculateResult('DIVIDE'); +} + +addBtn.addEventListener('click', add); +subtractBtn.addEventListener('click', subtract); +multiplyBtn.addEventListener('click', multiply); +divideBtn.addEventListener('click', divide); diff --git a/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/vendor.js b/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/vendor.js new file mode 100644 index 0000000..5e6f44d --- /dev/null +++ b/tasks-by-users/mmusk/02-unconventional-calc-if-else/assets/vendor.js @@ -0,0 +1,13 @@ +const userInput = document.getElementById("input-number"); +const addBtn = document.getElementById("btn-add"); +const subtractBtn = document.getElementById("btn-subtract"); +const multiplyBtn = document.getElementById("btn-multiply"); +const divideBtn = document.getElementById("btn-divide"); + +const currentResultOutput = document.getElementById("current-result"); +const currentCalculationOutput = document.getElementById("current-calculation"); + +function outputResult(result, text) { + currentResultOutput.textContent = result; + currentCalculationOutput.textContent = text; +} diff --git a/tasks-by-users/mmusk/02-unconventional-calc-if-else/index.html b/tasks-by-users/mmusk/02-unconventional-calc-if-else/index.html new file mode 100644 index 0000000..295d68b --- /dev/null +++ b/tasks-by-users/mmusk/02-unconventional-calc-if-else/index.html @@ -0,0 +1,36 @@ + + + + + + + Calculator + + + + +
+

The Unconventional Calculator

+
+ +
+ +
+ + + + +
+
+
+

0

+

Result: 0

+
+ + + + +