skip to Main Content

When I run the following code, it only prints to the console "Checkpoint X" but not the return statement:

function match(player, computer){
    
    console.log(`Player choice is ${player} and computer choice is ${computer}`);

    if (player === computer){
        console.log("Checkpoint 1")
       return "It's a draw!";
    } else if (player == 'Rock' && computer == 'Paper'){
        console.log("Checkpoint 2")
        return "Computer wins";
    } else if (player == 'Rock' && computer == 'Scissors'){
        console.log("Checkpoint 3")
        return "Player wins";
    } else if (player == 'Paper' && computer == 'Rock'){
        console.log("Checkpoint 4")
        return "Player wins";
    } else if (player == 'Paper' && computer == 'Scissors'){
        console.log("Checkpoint 5")
        return "Computer wins";
    } else if (player == 'Scissors' && computer == 'Rock'){
        console.log("Checkpoint 6")
        return "Computer wins";
    } else if (player == 'Scissors' && computer == 'Paper'){
        console.log("Checkpoint 7")
        return "Player wins";
    } else {
        return "Match error!";
    }
}

However, when I enter in the console directly match(player,computer); it prints to the console both "Checkpoint X" and the associated return statement.

Does anyone know why this is?

FOR ADDITIONAL CONTEXT:
This is what I see when I go to console!

enter image description here

2

Answers


  1. You need to explicitly return the console.log statement

    You can also return the same statement and it will be returned from the function where you can use it in other parts of your code.

    ...
    
          else {
            console.log(`Match error!`)
            return "Match Error!"
          }
    
    
    Login or Signup to reply.
  2. When you run a function it stores the return statement as the response of the function but we are not printing it. When you run code in the console, it is considered as
    console.log(match(1,1))

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search