Posts: 1
Joined: Tue Feb 12, 2008 6:28 pm
Occupation: Student

Puzzled Prorammer

Postby cheegrlcraz » Tue Feb 12, 2008 6:32 pm

I am doing the simple calculator writing project and i was wondering if there was any way to take all of the four functions (addition,subtraction, multiplication, and division) and make the correct answer appear in my answer box. I have an add and subtract button, but they both subtract. does anyone know how i could fix this so the functions would be used properly?

Former Expert
Posts: 675
Joined: Thu Feb 03, 2005 3:38 pm

Re: Puzzled Prorammer

Postby davidkallman » Tue Feb 12, 2008 9:02 pm

Hi cheegrlcraz,

I'm assuming the project you're doing is the one at: ... p001.shtml

If you look at that page, you'll notice two items:

1. There is a recommendation to use the firefox browser, rather than internet explorer.
2. There is large set of background items under:
"Terms, Concepts and Questions to Start Background Research."

There is obviously a "bug" (logic error) in your code, since it's not working and the project is doable. Without seeing your code, it's hard to know where the bug is. I'm NOT encouraging, however, that you post your code. First, go through your code using 1. and 2. above before asking for help.

Lastly, are you getting help? A javascript program is difficult for someone in grades K-5


Former Expert
Posts: 20
Joined: Tue Jan 08, 2008 12:49 pm
Occupation: Software Engineer

Re: Puzzled Prorammer

Postby fmcwilliams » Sat Feb 16, 2008 8:19 am

Hi cheegrlcraz,

Like Dave, I assume you started with the "Writing a Simple Calculator Program with JavaScript" project on the Project Ideas page and if this is so then I suggest that you look at the example and then at your code to solve your problem with the add and subtract both doing subtractions. On the example page there is a routine called CalculateSum that does the addition. In your code you might have a similar routine to subtract the numbers and another one to add two numbers together. Obviously, when the subtract button is used the program behaves correctly, but look to see what your program is doing when you pick the add button. It might be calling the subtraction routine. Another possibility is that the routine to perform the addition is written incorrectly and is really subtracting. A close look at the code should allow you to find and fix this bug.

For making your answer always appear in your answer box no matter which operation you choose, I will also ask you to look at the example page. In the CalculateSum routine you will see the instruction "form.Answer.value = A + B;". This instruction is performing and addition of A and B, but it is also directing the program to put that answer into the form.Answer.value text box. You can use this same kind of instruction in all of your routines that perform the four different operations. That way your answer will always be displayed in your answer box.

This sounds like a great project. Have fun with it and good luck.
Frank M

Return to “Grades K-5: Math and Computer Science”