www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] Program running but returns unwanted "undefined"

  1. #1
    Join Date
    Nov 2013
    Posts
    3

    resolved [RESOLVED] Program running but returns unwanted "undefined"

    Hello everyone. I am very new to JavaScript and I have made a little program. It seems to run well, but it returns unwanted "undefined" and I can't locate the mistake in the code.

    Any help on this would be appreciated. The code:

    Code:
    <html>
    <head>
    <script>
    
        function sumdig(number){
            var sumdigit=0; 
            //var num=Number(prompt("Give a number to sum it's digits"));
            for (var i=0;i<String(number).length;i++) {sumdigit+=Math.pow(parseInt(String(number).charAt(i)),2);
             
            }
               return sumdigit;
        }
    
    /************************************************************************/
    
    
    
    function happyNum2(number){
            //var number=Number(prompt("Give a number to see if it's happy or not"));
            var arr=[];
            arr[0]=sumdig(number);
            var i=0;
            var dummy=0;
            
            while (arr[i]!==1) {
                if (i>0) {
                    for (var j=0;j<i;j++) {
                        if (arr[j]==arr[i]) {
                            dummy=1;
                            break;
                        }
                        
                    }
                    
                    
                }
                
                if (dummy==1) {break;}
                arr[i+1]=sumdig(arr[i]);
                i++;
            }
                if (arr[0]==1||sumdig(arr[0])==1) {
                 document.write(number+": happy number");
                
            } else if ((i>0 && arr[i-1]==1)||(i>0 && arr[i]==1)) {
                 document.write(number+": happy number");
                }
                
                
             else if (dummy==1) {
                 document.write(number+": unhappy number");
                }
                
      }  
    
    
    /*********************************************************/
    
    
    function happyNum3(){
    var inp=Number(prompt("Up to which number should we check for happy numbers?"));
    
    for (var l=1;l<=inp;l++){
    var hn=happyNum2(l); document.write(hn+"<br>");
    }
    }
    
    </script>
    </head>
    
    <body>
    
    <input type="button" value="Run" onclick="happyNum3()">
    </body>
    </html>

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    Your function happyNum2 is not returning any value so hn variable is undefined.

  3. #3
    Join Date
    Nov 2013
    Posts
    3
    ok. I got it and modified it. Now it works just fine. Thank you very much!

  4. #4
    Join Date
    Nov 2013
    Posts
    1
    Hello. Sorry because this is not a reply to the thread. But the tittle is suitable and I canīt open a new thread.

    I am trying with functions expressions. Despite I read people recommend using functions declarations (is this better?) i saw them in several examples and source codes.

    I tried with this example :
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    var a = function (a,b) {
      alert("a="+a+", b="+b);
    }
    
    alert(a(1,2));
    </script>
    </head>
    <body>
    </body>
    </html>
    And the result is 2 alerts.
    The first one (and the expected one) with the legend "a=1, b=2"
    And a second one with the legend "undefined";

    Why this second alert?

  5. #5
    Join Date
    Nov 2013
    Posts
    3
    Code:
    alert(a(1,2));
    should be
    Code:
    a(1,2);
    As it is, you have an alert inside an alert.

  6. #6
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    247
    Quote Originally Posted by gonza8888 View Post
    Hello. Sorry because this is not a reply to the thread. But the tittle is suitable and I canīt open a new thread.

    I am trying with functions expressions. Despite I read people recommend using functions declarations (is this better?) i saw them in several examples and source codes.

    I tried with this example :
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    var a = function (a,b) {
      alert("a="+a+", b="+b);
    }
    
    alert(a(1,2));
    </script>
    </head>
    <body>
    </body>
    </html>
    And the result is 2 alerts.
    The first one (and the expected one) with the legend "a=1, b=2"
    And a second one with the legend "undefined";

    Why this second alert?
    Function declarations are read and available in an execution context before any code is exectued, whereas function expressions aren't executed until the execution reaches that line of code. JS engine pulls function declarations to the top of the source tree through a process known as function declaration hoisting.

    This example shows difference between function declarations and function expressions:

    Code:
    test(10,10); // function runs fine
    function test(a,b) {
    alert(a + " " + b);
    return;
    }
    
    test(10,10); // this one produces an error 
    var test = function(a,b) {
    alert(a + " " + b);
    return;
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles