Function won't log

I apologize in advance for the length here, but i can’t seem to figure out why this won’t log to the console. Neither return nor console.log() seem to be doing anything, but I’m also not getting any error messages.

function luhnAlg(arr){

const reversedArray = arr.reverse();

for (let i = 0; i < arr.length; i++){

  arr.filter(i => i % 2);

  if(i > 9){

    let difference = i - 9;

    return difference;

  } else {

    return i;

  };

  let j = i + i;

  if( j % 10 === 0){

    return true;

    console.log('valid');

  } else{

    return false;

    console.log('invalid');

    };

  };

}

return luhnAlg(valid1);

The console.log() will never be called as they are after the return statement (it’s called unreachable code).

If you want them to be executed, you need to put them before the return.

3 Likes

Oh! Thank you! I’m still struggling to understand the practical difference between return and console.log, but this helped me understand a little better. :grinning:

So, console.log() will only display a text in the output of your work environment, you cannot do anything with this text.
For example, if you are running your code in a browser, it will be displayed in the console of the browser.

return will return information to the code which calls the function.
And the return information will be used (or not) later.

Example:

function myFunction(number) {
  return number * 2;
}

If I call this function (ex. var result = myFunction(3)) the variable on the left will contain what the function has returned (in this case 6 - the result of number * 2)

1 Like