FAQ: Functions - Return

hello
Please i need help with this;

const getUserChoice = userInput => { 

  userInput.toLowerCase()

 if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' ) {

   return userInput;

 } else {

   console.log('Error!');

 }

}

console.log(getUserChoice('eraser'));

when the obvious parameters are inputted, expected messages are logged but when the error message logs, undefined comes along too.

That’s because you are returning the correct value and logging the error. Just return the error, as well.

1 Like
const getComputerChoice = () => { randomNumber = Math.floor(Math.random() * 3); if (randomNumber === 0) { return 'rock'; } else if(randomNumber === 1) { return 'scissors'; } else if (randomNumber === 2) { return 'paper' } else { return 'Unknown!' } } console.log(getComputerChoice(4));

Please look at this and tell me whats wrong

console.log(getComputerChoice(4));

If we look at the function, it takes no arguments. Since we control the output there is no default needed.

Recall that random * 3 is a value less than 3, so we should multiply by 4.

Math.floor(Math.random() * 4)

The intention was to see if the error message would be logged bcos when the function is called with the number arguments === randomNumber, it doesn’t log the expected return values. Why?

The error message won’t log because of the controlled inputs (random 0, 1 or 2). The default case is never raised.

but how come (0, 1 or 2) do not return the expected message when inputted as arguments for the function call?

Because they are still computed in the function. There is no parameter. Either use the parameter and remove the random function, or stick with no parameter and use the random function to create and initialize your variable.

Thank you for taking the time to explain this but may I have more explanation, if possible, with examples. Thanks

1 Like

Fair enough. Let’s start with no parameter, such as you have.

const foo = function () {
    return Math.floor(Math.random() * 3)
}

console.log(foo(), foo(), foo(), foo())
​ 1 1 2 1
console.log(foo(), foo(), foo(), foo())
​ 0 2 2 2
console.log(foo(), foo(), foo(), foo())
​ 2 2 2 0
console.log(foo(), foo(), foo(), foo())
​ 1 0 1 1
console.log(foo(), foo(), foo(), foo())
​ 1 1 1 1
console.log(foo(), foo(), foo(), foo())
​ 0 1 1 2

We don’t know exactly what we will get, but we know the nature of what we will get. The function generated values within our prescribed range. The parameter is not needed.

Bring user input into the picture and the parameter must adhere to that, and ensure it is a valid argument.

const bar = function (u) {
    return [0, 1, 2][u] || 'foobar!'

console.log(bar(0), bar(1), bar(2), bar(3))
​ foobar! 1 2 foobar!

The user input controls the output.

I think I understand now. Not all heroes wear capes. Thanks a lot.

thank you for sharing the thought on this. I think i will find this coding-way useful in the near future.

Does anyone know why nothing is being returned to the console ?? I cant seem to figure it out ?


function monitorCount(rows, columns) {
  const numOfMonitors = monitorCount(5, 4);
  console.log(numOfMonitors);
  return rows * columns;
};

That is an infinite recursion (a function calling itself). It should have crashed at some point with a stack overflow or out of memory error.

First of all thankyou for the help!!!

I input the code into the console on dev tools and i got this message that you said “Uncaught RangeError: Maximum call stack size exceeded” (I have no idea what this means)

I guess my question now i know the solution is, why does the code below throw an error with a stack overflow ??

function monitorCount(rows, columns) {
  const numOfMonitors = monitorCount(5, 4);
  console.log(numOfMonitors);
  return rows * columns;
};

But this code (which is the solution) returns 20 to the console? my guess is it has something to do with the way code is read by the computer but i cant make sense of why the code above wouldn’t run having all the code inside the function body ??

function monitorCount(rows, columns) {
  return rows * columns
}
  const numOfMonitors = monitorCount(5, 4);
  console.log(numOfMonitors)

I hope this makes sense!
Thanks for the help again!

One will need to hold off until recursive functions comes up, or do a quick search for ‘recursion’ or ‘recursive functions’ and read some of the results.

In general terms, a recursive function is one that calls itself from inside the body of the function, such as your example. This creates a call stack which is basically a list of all the calls from earliest to latest that are waiting to be called. The complete function does not execute, only gets added to the stack until such time as a base case comes up that terminates the calls whereupon the stack is then wound down as each call is completed. We can see that each recursive call is going to take up memory, which in the case above is all the available memory resulting in the stack overflow error.

Note that in your second example (the one that works correctly) the call to the function is not inside the body of the function, but in the global namespace, where it should be.

Bottom line, unless you are intentionally writing a recursive function that has a base case, don’t call a function from within its own body. It is tantamount to,

10 GOTO 10

See how circular that is? It will only end when it is interrupted from the keyboard (Ctrl+C, for instance) or the computer is shut off.

Here is an example of a recursive factorial calculator:

const factorial = function (n) {
    if (n === 0 || n === 1) {
        return 1;
    }
    return n * factorial(n - 1)
}
 > factorial(7)
<- 5040
2 Likes

Thank you so much that makes sense! will look into recursive functions! :grinning:

1 Like