Bolat Tleubayev
Bolat Tleubayev

Reputation: 1855

Why I get "undefined" in JavaScript

Could you please help, why do I get this undefined value instead of returning a?

var a = 0;
var m = 888;
 
function sevens(m, a) {   
      if (m == 0) {
          document.write("Amount of 8's is "+a+"<br>");
          return a;
      } else {
         if(Math.floor(m % 10) == 8) {
             a++;
             sevens(Math.floor(m / 10), a);
         } else {
             sevens(Math.floor(m / 10), a);
         }
     }
 }
 
 document.write("in "+m + " " + "it is" + " " + sevens(m, a));

Thank you in advance.

Upvotes: 2

Views: 69

Answers (4)

Aral Roca
Aral Roca

Reputation: 5889

You forget the return, otherwise, I refactored a little bit the sevens function and you can run in code snippet.

var a = 0;
var m = 888;

function sevens(m, a) {
  if (m === 0) {
    document.write("Amount of 8's is " + a + "<br>");
    return a;
  } 
  if (Math.floor(m % 10) === 8) {
    a += 1;
  }
  return sevens(Math.floor(m / 10), a);
}

document.write("in " + m + " " + "it is" + " " + sevens(m, a));

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386522

Maybe you change the logic a bit, because you don't need an else part if in the then part the function is finished with return.

The other change is suspending Math.floor in combination with the remainder operator %. It returns always an integer value.

The third part is to move the call of sevens outside of the if statement, because it is anyway called.

var a = 0,
    m = 888;

function sevens(m, a) {
    if (m == 0) {
        document.write("Amount of 8's is " + a + "<br>");
        return a;                                          // exit function, rest 
    }                                                      // of function is else part
    if (m % 10 == 8) {                                     // % --> int
        a++;
    }
    return sevens(Math.floor(m / 10), a);                  // return result of call
}

document.write("in " + m + " " + "it is" + " " + sevens(m, a));

Upvotes: 2

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67187

You are not returning the function call while doing a recursion,

if(Math.floor(m % 10) == 8){
  a++;
  return sevens(Math.floor(m / 10), a);
}else{
  return sevens(Math.floor(m / 10), a);
}

If you do not return anything inside a function, by default it will return undefined. Not in all the cases. That depends on the way you call the particular function.

Upvotes: 3

millerbr
millerbr

Reputation: 2961

Function needs to return something in the else statement. Like this:

function sevens(m, a){   
              if(m == 0){
                  document.write("Amount of 8's is "+a+"<br>");
                  return a;
              }else{
                 if(Math.floor(m % 10) == 8){
                     a++;
                     return sevens(Math.floor(m / 10), a);
                 }else{
                     return sevens(Math.floor(m / 10), a);
                 }
             }
         }

Upvotes: 4

Related Questions