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:


    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=[];
        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]) {
            if (dummy==1) {break;}
            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>");



<input type="button" value="Run" onclick="happyNum3()">