In section three when we make a game of rock, paper scissors, I made code different then the code instructed in a video in last.
I just want to know what is the difference between these two codes.? and which approach is better when writing the code .? These two codes show pretty much same results.
because this approach is not defined in control flow statement lesson. I know we can write if else statement in another control flow statement but what if we write code using my approach.
The video help code:
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'The Game was a Tie!';
}
if (userChoice === 'rock') {
if(computerChoice === 'paper') {
return 'The Computer Won!';
} else {
return 'The User Won!'
}
}
if (userChoice === 'paper') {
if(computerChoice === 'scissors') {
return 'The Computer Won!';
} else {
return 'The User Won!'
}
}
if (userChoice === 'scissors') {
if(computerChoice === 'rock') {
return 'The Computer Won!';
} else {
return 'The User Won!'
}
}
}
and my code:
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'The Game was a Tie!';
} else if (userChoice === 'rock' && computerChoice === 'paper') {
return 'The Computer Won!';
} else if(userChoice === 'paper' && computerChoice === 'scissors') {
return 'The Computer Won!';
} else if(userChoice === 'scissors' && computerChoice === 'rock') {
return 'The User Won!';
} else if (userChoice === 'paper' && computerChoice === 'rock') {
return 'The User Won!';
} else {
return 'Invalid Input';
}
}