I've just created a fully working calculator for my portfolio, and I would like to get a code review. Does that code looks good, or maybe I should refactor it? Is it good enough to be a part of a portfolio of Junior Front-end Developer?
Here is the working page with it: https://prmk01.github.io/Calculator (There is an option to use the keyboard instead of clicking on a buttons)
The whole project is available on my GitHub. I would appreciate reviewing my whole project. But I'm posting most of the Javascript here, because every post have to contain at least 3 lines of code:
const activatedButtons = document.querySelectorAll('.inactive');
const topRow = document.querySelector('.screen-top-part');
const bottomRow = document.querySelector('.screen-bottom-part');
const basicOperators = /\+|-|÷|×/;
let expression = '';
let bottomRowActive = true;
let memory = '';
function memoryUpdate (target) {
bottomRowActive = false;
for (button of activatedButtons) {
button.classList.remove('inactive');
}
switch (target.innerText) {
case 'MC': {
memory = '';
for (button of activatedButtons) {
button.classList.add('inactive');
}
break
} case 'MR': {
clearEntry();
bottomRow.innerText = memory;
break
} case 'M+': {
memory = eval(+memory + +bottomRow.innerText).toString();
break
} case 'M-': {
memory = eval(+memory - +bottomRow.innerText).toString();
break
} case 'MS': {
memory = bottomRow.innerText;
break
}
}
}
function appendNumber (target) {
if (topRow.innerText.includes('=')) {
topRow.innerText = '';
expression = '';
}
if (bottomRowActive) {
if (bottomRow.innerText.length === 1 && bottomRow.innerText == '0') {
bottomRow.innerText = target.innerText;
} else if (bottomRow.innerText.length >= 15) {
} else {
bottomRow.innerText = bottomRow.innerText + target.innerText;
}
return
}
bottomRowActive = true;
bottomRow.innerText = target.innerText;
}
function clearAll () {
bottomRowActive = false;
topRow.innerText = '';
bottomRow.innerText = '0';
expression = '';
}
function clearEntry () {
bottomRowActive = false;
if (topRow.innerText.includes('=')) {
clearAll();
} else {
bottomRow.innerText = '0';
}
}
function deleteLast () {
if (bottomRowActive) {
if (bottomRow.innerText == '0') {
return
} else if ((bottomRow.innerText.length === 3 && bottomRow.innerText.includes('.') && bottomRow.innerText.includes('-')) || (bottomRow.innerText.length === 2 && bottomRow.innerText.includes('-')) || bottomRow.innerText.length === 1) {
bottomRow.innerText = '0';
} else {
bottomRow.innerText = bottomRow.innerText.slice(0, bottomRow.innerText.length - 1)
}
}
}
function basicOperation (target) {
if (bottomRowActive) {
expression = `(${expression}(${bottomRow.innerText}))`;
topRow.innerText = evaluate() + ' ' + target.innerText;
expression += target.dataset.value;
bottomRowActive = false;
} else {
if (basicOperators.test(topRow.innerText.slice(-1))) {
topRow.innerText = topRow.innerText.slice(0, -1) + target.innerText;
expression = expression.slice(0, -1) + target.dataset.value;
} else {
topRow.innerText = evaluate() + ' ' + target.innerText;
expression += target.dataset.value;
}
}
}
function equals () {
bottomRowActive = false;
if (topRow.innerText.includes('=')) {
return
}
if (bottomRow.innerText.includes('-')) {
topRow.innerText = `${topRow.innerText} (${bottomRow.innerText}) =`;
} else {
topRow.innerText = `${topRow.innerText} ${bottomRow.innerText} =`;
}
expression = `(${expression}(${bottomRow.innerText}))`;
bottomRow.innerText = evaluate();
}
function decimalPoint () {
if (!bottomRowActive) {
bottomRow.innerText = '0.';
bottomRowActive = true;
} else if (bottomRow.innerText.length >= 14) {
} else if (!bottomRow.innerText.includes('.')) {
bottomRow.innerText += '.';
}
}
function minus () {
if (bottomRow.innerText == '0') {
return
} else if (bottomRow.innerText.includes('-')) {
bottomRow.innerText = bottomRow.innerText.slice(1, bottomRow.innerText.length);
} else {
bottomRow.innerText = '-' + bottomRow.innerText;
}
}
function evaluate () {
let result = eval(expression);
let resultRounded = +parseFloat(result).toFixed(8);
if (resultRounded.toString().length >= 18) {
resultRounded = resultRounded.toExponential();
}
resultRounded = resultRounded.toString();
if (resultRounded.includes('e')) {
let number = resultRounded.split('e')[0];
let numberRounded = (+parseFloat(number).toFixed(8)).toString();
resultRounded = numberRounded + 'e' + resultRounded.split('e')[1];
}
return resultRounded;
}