Feedback on Calculator

Feedback on Calculator
0

#1

Hi there!
Just finished my calculator. Any feedback would be appreciated.


#2

Functionally speaking, it seems to work brilliantly :slight_smile: well done

The next thing I would work on is the readability of the javascript. Code should ideally read like English. Ask yourself, if you returned to this code in a years time, could you easily remember what all the code is for and why you did certain things?

Take for example the code below:

if (val == ‘AC’) {
exp.innerHTML=0;
dotCount = 0;
isNewExpression = true;
}
if (val == ‘CE’) {
if (exp.innerHTML.length == 1 || exp.innerHTML.includes(’=’)) {
exp.innerHTML=0;
dotCount = 0;
isNewExpression = true;
return;
}

This could be replaced with:

function ClearDisplay() {
exp.innerHTML=0;
dotCount = 0;
isNewExpression = true;
}

function ExpressionContainsEquals() {
return exp.innerHTML.includes(’=’);
}

if(val == ‘AC’) {
ClearDisplay();
}
if(val == ‘CE’) {
if(exp.innerHTML.length == 1 || ExpressionContainsEquals()) {
ClearDisplay();
return;
}
}

Adding in the function firstly makes it clearer what this code does, it clears the display. It also removes the duplication of code.

Hope that helps and happy coding!


#3

Thank you so much for such an in depth feedback. I realize that my code’s readability is its weak point, but did not think of how it could be made better. Your feedback clearly gives me direction to work in. Thanks again!
PS: One thing I wanted to clear: why would you name functions starting with uppercase? As far as I know that is the convention for constrauctor functions (functions that create objects) and regular functions should be named starting with lowercase, as any other variable.