Following a tutorial on youtube, building a rock-paper-scissors game, but they dont really explain in detail every step. So im asking you lads, why do I have to put parameters in the win function? I did not put any parameters in the lose/tie function and it's still working.
function win(user, computer) {
userScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result_div.innerHTML = "You won!"
}
function lose() {
computerScore++;
userScore_span.innerHTML = userScore;
computerScore_span.innerHTML = computerScore;
result_div.innerHTML = "Computer wins!"
}
function tie() {
result_div.innerHTML = "Game tie!"
}
function rockPaperScissors(userChoice) {
const computerChoice = getComputerChoice()
if (userChoice === 'paper' && computerChoice === 'rock' ||
userChoice === 'rock' && computerChoice === 'scissors' ||
userChoice === 'scissors' && computerChoice === 'paper') {
win(userChoice, computerChoice)
} else if (userChoice === computerChoice) {
tie(userChoice, computerChoice)
} else {
lose(userChoice, computerChoice)
}
}
bonus: does anybody know somebody who teaches people javascript but explains everything crystal clear? im struggling to make myself understand the choices I have to do when building something. thanks!!
Apparently(while we don't have the full code) the userChoice and computerChoice are global, that's why the tutorial is not caring so much for code clarity.
As an advice: we recommend each function do one and only one thing(single responsibility principle) where it uses its arguments directly and don't rely on a global mutable state to not miss things quickly.
For JS I personally recommend reading YDKJS with some help while reading from MDN, and with time and practice of everything you are learning you start to becoming better at this