2

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.

4 Answers 4

4

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);
                 }
             }
         }
Sign up to request clarification or add additional context in comments.

1 Comment

oh I see now, I have to add "return" in front of recursions, thank you.
3

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.

Comments

2

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));

Comments

1

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.