(sqrt) √ function: Javascript/HTML help

root

Site Team
Staff member
Messages
8,181
Location
UK
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

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.
 

root

Site Team
Staff member
Messages
8,181
Location
UK
User interface,
this is the simplest part of the calculator really because it's largely already standardised, I'm going to group numbers 1-9 into a 3x3 pad, and put a zero and decimal point underneath those number,

I have four basic functions, + - * / so I'll stick these down the side all in 1 column, then I have a space space underneath the 3 so I'll just jam the equals button in there.

Obviously I had thought about this before I just banged a load of code out.
I didn't draw it out.

You *may* find it beneficial to have a design, whether that's a rough design on a napkin, or something in paint, something you've commissioned a graphic design agency to do, or something that your little brother or sister just really wants.

it just helps to know where you are going with something.



you may chose a different design, and that's fine you can put the buttons anywhere you like and in any order, they
 

jcrew

Solid State Member
Messages
13
Location
US
Root,

This information you gave me is absolutely brilliant. I am going to study the code you wrote and "back track" and to determine the exact steps I took to build the calculator. Your comments are helpful in figuring out what needs to be done, where I need to call on a function and what parts of code are missing.
The way you mapped out this code is excellent. I see a flow and a step-by-step format and using the advanced functions later. The comments area is understandable(I need to use it more)because it helps me understand why I wrote a specific code and how the code functions in human language. The part where you said "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." That seems to be theme of what's going on with this sqrt root function.

Thanks for referring me to the color coded text editor Turbopad. I am going to download it. It is going to be more useful than notepad++.
However, I am not going to depend on color-coded text as a crutch either.
I can see that my "thinking" was all over the map when building this calculator. I was putting the cart before the horse in many instances. For example, making it work correctly before making it pretty with colors.

The format and information you gave me is invaluable and I'm going to refer and study your posts again. Until then I will keep persisting.....
 
Top Bottom