Third Pass

I noticed the if statement in the document.querySelectorAll block only has an else if but no else which I thought was necessary in most cases as a last resort condition. Since the activation of this function only comes from pressing one of the buttons in the html, and those buttons only have either a data-value or data-action property, then I suppose there's no third option, but I wonder if in a real environment it would be necessary to add something for security. Like I mentioned before, the value and action in the conditions come from the data-value and data-action in the HTML button tags repectively.

Somehow the switch statement always feels to me a little anachronistic, out of place, like people just teach it because it's there, but rarely use it, at least in their tutorials. And I had the impression that it reminded me a bit of older code, the way the syntax is organized, no curly braces; it just "looks" old.

But my impression was wrong actually. The if/else statement has a deeper history, since FORTRAN (1957) while the switch statement comes from C language (1972). And it makes sense, since in it's most basic form the if statment is used to evaluate a boolean, but it's also more flexible, it allows far more complex conditions; on the other hand, the switch statement really shines when you have a clear and straight-forward enumeration of conditions.

The try/catchstatement is something I haven't used very much before... so I'm quickly approaching the end of my familiarity with the language here... But anyway, I can infer what it does, it's similar to an if/else but it "catches" an error before it would crash the program or mess with the functionality. So if the operation is valid it performs the calculation using a the builtin function eval(): const result = eval(currentInput), if it is not, it displays "Error" on the display by changing the variable currentInput to the string "Error" before performing the operation, or performs the operation, but if it throws an error, it just goes to the catch statement rather than letting the invalid operation crash the program.

What sort of operation makes it throw an error? Try doing something like "4+/6" and you'll see "Error" in the display. And what would happen if we didn't have this try/catch, if the case statement just went straight to calculate the operation? You can test it by erasing the whole try/catch block and the 4 lines inside the try, so like this:

case "calculate":
	const result = eval(currentInput);
	lastResult = result;
	lastResult = String(result);
	currentInput = String(result);
    break;

It actually doesn't crash in this case, instead if you press 5+/6 and then = it just doesn't do anything and you can keep adding to the input.

After evaluating the operation, it updates the display. The function updateDisplay() is pretty straight-forward, it has only one line; it grabs the constant display which connects to the <div class="display" id="display">0</div> line from the HTML and changes it's property textContent, i.e. the text between the opening and closing tags, to the string value currently in the currentInput variable. But it's a very important part of the prorgam, without it, all calculations may be there, the variables update theirr value, but you'll never see it in the screen.