Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   JavaScript and Client-Side Browser Scripting (http://www.programmingforums.org/forum23.html)
-   -   Need help with solution (http://www.programmingforums.org/showthread.php?t=13108)

tromba May 3rd, 2007 5:12 PM

Need help with solution
 
I'm teaching a class in basic computer skills and we're doing a little bit in programming using javascript. In most cases I can find the student's errors with little problems (I'm not a programmer and haven't done very much in javascript). However, I've gotten stumped on this one. In a previous spreadsheet project, students made an invoice for selling ice cream. I've had them put this online in javascript.

I've put her most recent edition here on the class website here.

I have the students put alerts in so they can see that their variables work right, and this student seems to do fine until the TOTAL button. I can't find an error there, so I'm guessing it's somewhere else, but I sure can't locate it.

Any suggestions? I need to look smart and be able to tell the students what they did wrong!!!!:)

Thanks.

DaWei May 3rd, 2007 6:04 PM

You wouldn't happen to be 'her', would you? Just curious about the lapse.

Jessehk May 3rd, 2007 6:23 PM

Quote:

Originally Posted by DaWei (Post 127460)
You wouldn't happen to be 'her', would you? Just curious about the lapse.

I doubt it, especially given this: http://www.programmingforums.org/for...-to-print.html :)

titaniumdecoy May 3rd, 2007 7:06 PM

:

if (icecream == "fudge" || icecream == "rum" || icecream = "apple")
:

if (gallons == 5)s
You might want to check out JSLint.

tromba May 4th, 2007 9:49 AM

Nope. I'm not her. Her actual work is under her name on a different server. If you back up a directory from the link above, this is the class website where I post assignments and that type of thing. There isn't student access to that machine!!

We don't offer a computer science major here, so we don't have any programmers around. My background is entirely in music, and I got this course because I was available and I wanted to do it. When I did it for the first time, I thought the hardest part would be finding programming errors.... I learned JavaScript from the book like the students, but I kept going over it until I understood it all (unlike the students who skim it over and say "huh?".)

But as it turned out last semester, finding their errors wasn't hard at all and was one of the most fun parts. This one, however, has me stumped....

tromba May 4th, 2007 10:06 AM

Quote:

Originally Posted by titaniumdecoy (Post 127464)
:

if (icecream == "fudge" || icecream == "rum" || icecream = "apple")

Since I'm somewhat new at this, would an error like this make the program not run, or would it just be a problem if the Apple button was pushed (still working fine for Fudge or Rum?
Quote:

Originally Posted by titaniumdecoy (Post 127464)
:

if (gallons == 5)s

I'd seen that s before... even removed it didn't seem to matter. I keep working with her most recent version of the program.
Quote:

Originally Posted by titaniumdecoy (Post 127464)
You might want to check out JSLint.

Now that's a nice site!!!! so here's a question on an error. To help make their code more individual (translated as can't copy code as easily] they have to comment what all parts of the program do. For these <input .... onClick -- it WORKS this way (or I should say displays correctly since the program doesn't work)
:

              <td><input type=button value="1" onClick ='gallons = 1; alert ("gallons is" + gallons);
//This sets the variable for gallons and triggers an alert to display the number of gallons chosen.
'>

but JSLint says that there's an unclosed string and shows this: " <td><input type=button value="1" onClick ='gallons = 1; alert (..." -- I THINK it's refering to 'gallons but it's not a string but part of the onClick.

The problem may come when the //comment... follows in part of the onClick. I thought that //comment.... made what followed the // be ignored for the rest of the line, and put the '> on a new line. Is this correct? If so, what's the error on that line? JSLint won't run past this line....

titaniumdecoy May 4th, 2007 5:40 PM

Quote:

Originally Posted by tromba (Post 127487)
The problem may come when the //comment... follows in part of the onClick. I thought that //comment.... made what followed the // be ignored for the rest of the line, and put the '> on a new line. Is this correct? If so, what's the error on that line? JSLint won't run past this line....

Carriage returns within the code snippet cause that JSLint error. Most browsers will probably have no problem interpreting that code, however.

The comment in the example you gave is, in DaWei's words, "trashing up the joint." Anyone with a cursory understanding of JavaScript will understand what an assigment operator and an alert statement do. As such, short code snippets enclosed in HTML attribute tags should not be commented in most cases. If you need to perform a more complex action (that may need commenting) you should use a function defined elsewhere, i.e., inside <script> tags. For example, onclick="doSomething();".

tromba May 4th, 2007 6:20 PM

Quote:

Originally Posted by titaniumdecoy (Post 127499)
Carriage returns within the code snippet cause that JSLint error. Most browsers will probably have no problem interpreting that code, however.

OK. The alerts and comments were added after the fact... in other words before they were added it still didn't work.

The reason for having a long list of what happens on the TOTAL button (or other onClick) is that this is the way the example in the book did it. That example in the book, from which the student adapted their program is this one.

tromba May 4th, 2007 6:38 PM

Quote:

Originally Posted by titaniumdecoy (Post 127499)
Carriage returns within the code snippet cause that JSLint error. Most browsers will probably have no problem interpreting that code, however.

I tried to take out the carriage returns, but that's WAY too much trouble, given the model (see message above) that the students are working with.

Given that, JSLint seems to not be a possibility with this project.

So I'm still stuck on why the TOTAL button doesn't work from the original....

Arevos May 4th, 2007 9:39 PM

Quote:

Originally Posted by tromba (Post 127502)
I tried to take out the carriage returns, but that's WAY too much trouble, given the model (see message above) that the students are working with.

Carriage returns are not the cause of the error. JSLint is a good tool, but it is very strict about what it considers to be valid. Any browser will be far more forgiving. For the purposes of teaching, I'd hesitate to use JSLint, as you'll likely spend more time trying to satisfy JSLint's exacting demands, than you will actually teaching the subject.

I personally would recommend Firebug. It's an add-on for the Firefox web browser, and is something I consider essential for debugging any piece of Javascript. If it is possible to use it (I know not all computers have Firefox installed on them), then I'd advise doing so.

Quote:

Originally Posted by tromba (Post 127502)
So I'm still stuck on why the TOTAL button doesn't work from the original....

This is a common problem for those beginning programming. To understand the problem, you need to understand the difference between assignment and equality.

An example of assignment:
:

x = 10
You can think of this as putting the number 10 in a box labelled "x".

An example of equality:
:

x == 10
You can think of this as testing to see whether the value in "x" is equal to the number 10.

The code does not work because the student has written:
:

icecream == "fudge" || icecream == "rum" || icecream = "apple"
Instead of:
:

icecream == "fudge" || icecream == "rum" || icecream == "apple"
(The part that causes an error is highlighted in red.)

In other words, the student is trying to put the value "apple" into the variable "icecream" (assignment). What they want to do is test whether the variable "icecream" has the value "apple" inside of it (equality).

Does that make sense?


All times are GMT -5. The time now is 2:09 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC