I had to do a simple calculator using HTML and JS and since I've never programmed on JS I have a question in order to get the logic right. Here is the code:
I have a function which has to write on the screen of my calculator;
function writeToScreen(a) { var elem = document.getElementById('screen'); switch (a.value) { case '+': { if (firstOperation) { lastOperation = '+'; firstNumber = elem.value; elem.value = ""; firstOperation = 0; } else { lastOperation = '+'; firstNumber += elem.value; } } break; case '-': { if (firstOperation) { lastOperation = '-'; firstNumber = elem.value; elem.value = ""; firstOperation = 0; } else { lastOperation = '-'; firstNumber -= elem.value; } } break; case '*': { if (firstOperation) { lastOperation = '*'; firstNumber = elem.value; elem.value = ""; firstOperation = 0; } else { lastOperation = '*'; firstNumber *= elem.value; } } break; case '/': { if (firstOperation) { lastOperation = '/'; firstNumber = elem.value; elem.value = ""; firstOperation = 0; } else { lastOperation = '/'; firstNumber /= elem.value; } } break; case '=': { if (firstOperation) { } else { switch (lastOperation) { case '+': { elem.value = (firstNumber + elem.value); } break; case '-': { elem.value = firstNumber - elem.value; } break; case '/': { elem.value = firstNumber / elem.value; } break; case '*': { elem.value = firstNumber * elem.value; } break; default: { } break; } firstOperation = 1; lastOperation = ''; } } break; case 'C': { elem.value = ""; } break; case '1': { elem.value += a.value; } break; case '2': { elem.value += a.value; } break; case '3': { elem.value += a.value; } break; case '4': { elem.value += a.value; } break; case '5': { elem.value += a.value; } break; case '6': { elem.value += a.value; } break; case '7': { elem.value += a.value; } break; case '8': { elem.value += a.value; } break; case '9': { elem.value += a.value; } break; case '0': { elem.value += a.value; } break; default: { } break; } } Here is the screen of my calculator:
<td colspan=3 align="center"><input id="screen" type=text disabled></input></td> and here is one button of my calc:
<td align="center"><button type="button" onclick="writeToScreen(this)" value=4>4</button></td> Now to the question and to check whether my logic is right. Basically when I press on the button, the function writeToScreen is called, giving as parameter (this) the value 4 for the current button. The number 4 is now displayed in the screen of my calculator, but I don't see where the displaying is coming from. Is it from the getElementById('screen'). Is this function checking if 'screen' id is of input type and then displaying it? Where exactly is the displaying happening?
Thanks!
writeToScreenfunctionthisis the element clicked. You pass it to the function, so now that element is referenced by theaparameter. When you doa.value, get fetches the current value of the element, which is4for that button. Theelemvariable refers to thescreeninput. You assign to its.valueproperty in order to update the display. So everyelem.value = "...whatever..."is updating the value of thescreenelement, and therefore its display.getElementById(). That's just a method for fetching an element from the DOM. Updating the.valueof an input element will update its display. Note that most elements do not have a.value. Most other ones, like a<div>will have its content updated using.appendChild()or.textContentor other properties and methods.