返回语句未打印到控制台?

问题描述 投票:0回答:1

当我运行以下代码时,它仅打印到控制台“Checkpoint X”,但不打印返回语句:

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!";
    }
}

但是,当我直接进入控制台时

match(player,computer);
,它会将“Checkpoint X”和相关的返回语句打印到控制台。

有谁知道这是为什么吗?

javascript if-statement return console.log
1个回答
0
投票

您需要显式返回

console.log
语句

您还可以

return
相同的语句,它将从函数返回,您可以在代码的其他部分使用它。

...

      else {
        console.log(`Match error!`)
        return "Match Error!"
      }

© www.soinside.com 2019 - 2024. All rights reserved.