Search code examples
javascriptloggingcallbackreturn

Strange RETURN behavior in Javascript logging to the console


I am trying to return an output inside an if() statement in my function and have it logged to the console; however, the returned output is not being displayed in the console.

I recently watched Numberphile's video on Multiplication Persistence https://www.youtube.com/watch?v=Wim9WJeDTHQ&t=675s and I thought it would be fun to try and write the function that he uses in the video to calculate the the persistence for a given number.

I am coding in Javascript on replit.

while doing so I found a strange behavior to my function and I was hoping someone could explain why this is happening?

to explain: you can log the return statement of a function by calling the function inside console.log

function logger () {
return "this string will be logged"
}

console.log(logger())

// this method logs the return statement of calling the logger function 

here I have written an if statement inside my function and called my function inside console.log; however, the returned statement is not being logged.


function logger (number) {

  if (number === 1) {

    console.log(number)
    return "this string should be logged when the function finishes"

  }

  console.log(number)

  number --

// pass the new number back into the function 

  logger(number)

}


console.log(logger(5))

// this is NOT logging the string to the console. and I don't understand why

so you can see the function checks if the number is equal to 1 and if not subtracts one and then calls the function again on the new number. if you pass the number 1 into the function (or whatever parameter is necessary so that the if statement evaluates to true on the first round) then the return statement will be logged to the console. example

console.log(logger(1)) =>

1 "this string should be logged when the function finishes"

however if you pass something into the function that does not resolve to true on the first round the returned statement IS NOT logged.

console.log(logger(5)) => 5 4 3 2 1 undefined

I am expecting this to log the string at the end, but it does not. so it's something to do with the if statement and calling the function within itself that is causing this to happen.

my actual code for the multiplication persistence is this. which is essentially the same as above. it multiplies the digits and logs them, but does not log the final return statement unless you enter a single digit number.


function persistence (number,steps) {

  // create and increment the steps argument
  if (steps === undefined) {
    var steps = 0
  } else {
    steps++
  }
  
  // if number only has one digit exit the function
  if (number.toString().length === 1) {
    console.log(number)
    console.log(`number of steps: ${steps}`)
    return "return this when the function finishes"
  } 

 
  console.log(number)

  // reduce the current number with *=
  var result = Number(
    number
    .toString()
    .split('')
    .reduce((acc, current) => acc *= current))
  
  // call the function on the new number
  persistence(result,steps);
}


console.log(persistence(5428))


/*
5428
320
0
number of steps: 2
undefined
*/

// instead of logging undefined at the end it should log "return this when the function finishes"


it works, try the number from the video.

console.log(persistence(277777788888899))

anyways, just thought I'd ask, seems strange to me. thanks in advance.


Solution

  • This is happening because the function call directly inside this console.log call: console.log(logger(5)) does not actually return anything!

    The function call that does return the string you'd like to log is the eventual logger(1) that's buried multiple recursive calls deep. However, since logger(2) does nothing with what logger(1) returned to it, logger(1)'s returned value is lost.

    The fix is simply adding a "return" at the recursive call: return logger(number), that way the string is passed from that last recursive call all the way up to the first call, and then to the console.log.