SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: such a strict language
-
Dec 1, 2003, 19:52 #1
such a strict language
hey everone. would someone mind looking at the code and perhaps telling me what i might be doing wrong?
Code:<script language="JavaScript" type="text/JavaScript"> function checkForm() { var is_checked = document.forms["submitscore"].forfeit.checked; if (is_checked == false) var r1h = parseFloat(document.forms["submitscore"].r1h.value); var r2h = parseFloat(document.forms["submitscore"].r2h.value); var r1a = parseFloat(document.forms["submitscore"].r1a.value); var r2a = parseFloat(document.forms["submitscore"].r2a.value); var totalHome = parseFloat(r1h + r2h); var totalAway = parseFloat(r1a + r2a); var totalScore = totalHome + totalAway; if (totalScore != 24) { alert('The Round 1 and Round 2 scores do not add up to an even 24 rounds.'); return false; } return true; } return true; } </script>
-
Dec 1, 2003, 20:25 #2
- Join Date
- Jul 2002
- Location
- Dallas, TX
- Posts
- 2,900
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, offhand what I see you're missing is an opening brace after your first if statement. However, you can make your code quite a bit more efficient by storing the form as a reference, a refactoring some of your variables out of existence.
Code:function checkForm() { var f = document.forms["submitscore"]; if ( !f.forfeit.checked ) { var r1h = parseFloat( f.r1h.value ); var r2h = parseFloat( f.r2h.value ); var r1a = parseFloat( f.r1a.value ); var r2a = parseFloat( f.r2a.value ); var totalScore = r1h + r2h + r1a + r2a; // already numbers, no need to run parseFloat on total if ( totalScore != 24 ) { alert('The Round 1 and Round 2 scores do not add up to an even 24 rounds.'); return false; } } return true; }
-
Dec 1, 2003, 20:28 #3
Thank you for your help.
-
Dec 1, 2003, 21:18 #4
- Join Date
- Sep 2002
- Location
- Bournemouth, South UK
- Posts
- 1,551
- Mentioned
- 1 Post(s)
- Tagged
- 0 Thread(s)
If you want good error reporting, install NS. The latest Netscape has superb error reporting, and will point you to the exact problem at the line itself (creates a link to problem line). I use it all the time, to check my site and to do ALL error reporting. It also allows the embedding of WMP objects, with all controls. which is cool.....
LiveScript: Putting the "Live" Back into JavaScript
if live output_as_javascript else output_as_html end if
Bookmarks