Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help! function not defined error

    I know you guys can catch my error here. I'm very, very new to JavaScript and don't have the ability to catch simple syntax errors, which I think might be my problem here. I also get a "missing } after function body" error on line 1, which I don't think I actually have.

    Code:
    <!doctype html>
    <html>
     <head>
       <title>Sum of Numbers Below N that are Multiples of 3 or 5</title>
    	<script type="text/javascript">
       function sumOfMultiples(){
       //runs the sum of all natural multiples of 3 and 5 below 1000
       var i = 1, ans = '', sum = 0;
    
    	//generate natural numbers 1 .. 999
    	while(i < 1000){
       		//test: multiple of 3 or 5?
    	    if((i % 3 == 0) || (i % 5 == 0)){
          	sum += i;
          	ans = ans + i + ' ';
       }
       ++i;
    }
    //display result
    alert(sum);
    
     </script>
     </head>
    
     <body>
       <p>
         Contents of the page.
       </p>
       <input type="button" value="Sum of Mults" onclick="sumOfMulitples();">
       <hr>
       <div id="outputDiv">
         Welcome to my site!
       </div>
     </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    sumOfMulitples is not the same as sumOfMultiples (mind the order of the letters)

    And if you count the number of { and the number of } you'll see that you have 3 times { and only 2 times } ... that's a mismatch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •