however that's still wrong,
you'll notice that now, we start of with 0 inside register_a, then when we press a number (1 for example) we end up with 01 rather than just the 1 that we were hoping for.
What we need to do is check if the value of register_a is a zero, and if it is just put the input data into that variable rather than concatenating it.
so we can change that function to
so that's a very simple way of getting data onto the screen...
and yet it's still not quite right, because user testing is going to show you that the original behaviour wasn't wrong for all cases.
when you use a calculator, if you just turned it on and there is a zero displayed on the screen.
If you type .5 you want 0.5 to be displayed, that was working and this fix broke that.
Still at the beginning I said that you needed to understand how a calculator worked, and I haven't really gone over that.
the point this far was to show that, even though you can approach things sensibly, if you don't have a design then you end up testing and finding a whole bunch of stuff that doesn't work.
all these things are being found AS they are coded because the program seems pretty simple, it's easy to just save that javascript and press a button and you get an instant hit on the results.
but if you were writing a more complicated program, or had not been able to test as you go, then you could find that a really small slip up actually really ruins stuff at the end!
if makes sense to break the program down into sections and to design each section.
you'll notice that now, we start of with 0 inside register_a, then when we press a number (1 for example) we end up with 01 rather than just the 1 that we were hoping for.
What we need to do is check if the value of register_a is a zero, and if it is just put the input data into that variable rather than concatenating it.
so we can change that function to
Code:
function input(num) {
if (register_a > 0) {
register_a = "" + register_a + num;
}
else {
register_a = num;
}
document.getElementById("screen").innerHTML = register_a;
}
so that's a very simple way of getting data onto the screen...
and yet it's still not quite right, because user testing is going to show you that the original behaviour wasn't wrong for all cases.
when you use a calculator, if you just turned it on and there is a zero displayed on the screen.
If you type .5 you want 0.5 to be displayed, that was working and this fix broke that.
Still at the beginning I said that you needed to understand how a calculator worked, and I haven't really gone over that.
the point this far was to show that, even though you can approach things sensibly, if you don't have a design then you end up testing and finding a whole bunch of stuff that doesn't work.
all these things are being found AS they are coded because the program seems pretty simple, it's easy to just save that javascript and press a button and you get an instant hit on the results.
but if you were writing a more complicated program, or had not been able to test as you go, then you could find that a really small slip up actually really ruins stuff at the end!
if makes sense to break the program down into sections and to design each section.