I’ve been messing around with a rock, paper scissors project that codecademy gets you to try, I’m pretty happy with it but I have one problem! When the result is a tie, it logs the tie message (perfect!) but also logs the win message linked with the result! How do I get it to ONLY log the tie message? Here’s the code:
JavaScript
x
72
72
1
const getUserChoice = userInput => {
2
userInput = userInput.toLowerCase();
3
if (userInput === "rock" || userInput === "paper" || userInput === "scissors" || userInput === "gun") {
4
return userInput;
5
} else {
6
console.log(`Sorry! But ${userInput} is an illegal weapon in the bloodythirsty sport of Rock, Paper, Scissors!`)
7
}
8
}
9
10
const getComputerChoice = () => {
11
const ranNum = Math.floor(Math.random() * 3);
12
switch (ranNum) {
13
case 0:
14
return "rock";
15
case 1:
16
return "paper";
17
case 2:
18
return "scissors";
19
}
20
};
21
22
const determineWinner = (getUserChoice, getComputerChoice) => {
23
if (getComputerChoice === getUserChoice) {
24
console.log("It seems it's a tie! You've matched wits!");
25
}
26
if (getUserChoice === "rock") {
27
if (getComputerChoice === "paper") {
28
return "The computer wins! It has gift wrapped your weapon.";
29
} else {
30
return "You beat the computer! They immediately begin crafting bigger scissors.";
31
}
32
}
33
34
if (getUserChoice === "paper") {
35
if (getComputerChoice === "scissors") {
36
return "The computer wins! They claim arts and crafts time as a reward."
37
} else {
38
return "You beat the computer! Their puny stone was no match for your paper aeroplane to the eye!"
39
}
40
}
41
42
if (getUserChoice === "scissors") {
43
if (getComputerChoice === "rock") {
44
return "The computer wins! You won't be cutting straight lines any time soon..."
45
} else {
46
return "You beat the computer! You cause emotional damage by destroying their robot child's drawing. You're a monster."
47
}
48
}
49
50
if (getUserChoice === "gun") {
51
if (getComputerChoice === "rock" || getComputerChoice === "paper" || getComputerChoice === "scissors") {
52
return "You win. But at what cost?"
53
}
54
}
55
if (getUserChoice === undefined) {
56
return "Come back when you're ready to take this game seriously."
57
}
58
}
59
//Enter your choice below in getUserChoice brackets
60
const playGame = () => {
61
let userChoice = getUserChoice("rock");
62
let computerChoice = getComputerChoice();
63
if (userChoice !== undefined) {
64
console.log(`You have chosen ${userChoice} as your weapon.`);
65
}
66
if (userChoice !== undefined) {
67
console.log(`The computer has brought ${computerChoice} to a ${userChoice} fight.`);
68
}
69
console.log(determineWinner(userChoice, computerChoice))
70
}
71
72
playGame();
Advertisement
Answer
Instead of logging the tie message, you should return it, leaving the logging to the caller:
JavaScript
1
4
1
if (getComputerChoice === getUserChoice) {
2
return "It seems it's a tie! You've matched wits!";
3
}
4