Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion game.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
// Write all your code here
// Replace the contents of this file with your own code
console.log('Hello, world!');
// console.log('Hello, world!');

function computerPlay(){
const selections = ['rock', 'paper', 'scissors'];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you have selection as a local variable, I should have done that too

return selections[Math.floor(Math.random()*selections.length)];

}

function playRound(player, computer){

if(player === computer){
return `Your selection & Computer Selection are same : ${computer} \nIt's a tie!`;
} else if(player === "ROCK"){
return `${computer === "SCISSORS" ?
`You WIN! \nYour Selection:${player} beats Computer Selection:${computer}` :
`You Lose! \nComputer Selection:${computer} beats Your Selection:${player}`}`;
} else if(player === "PAPER"){
return `${computer === "ROCK" ?
`You WIN! \nYour Selection:${player} beats Computer Selection:${computer}` :
`You Lose! \nComputer Selection:${computer} beats Your Selection:${player}`}`;
} else {
return `${computer === "PAPER" ?
`You WIN! \nYour Selection:${player} beats Computer Selection:${computer}` :
`You Lose! \nComputer Selection:${computer} beats Your Selection:${player}`}`;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's cool that you used the ternary operator as the return value of your function, but I would suggest sticking with select statements only, esp because you have a long string in your ternary operator statements which hinder the readability of your code.

}
}

function game(){
let playerWin = 0;
let computerWin = 0;
let gameRound = 1;
while(gameRound <= 5){
const playerSelection = prompt("Please write your choise").toUpperCase();
const computerSelection = computerPlay().toUpperCase();
if(playerSelection === "ROCK" || playerSelection === "PAPER" || playerSelection === "SCISSORS"){
let result = playRound(playerSelection,computerSelection);
if(result.includes("WIN")){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

others might disagree, but I would say it is not a reliable and the most efficient method to check for the winner.

playerWin++;
gameRound++;
} else if(result.includes("Lose")) {
computerWin++;
gameRound++;
} else {
gameRound++;
}
console.log(`${result}\nPlayer Win:${playerWin}\nComputer Win:${computerWin}`);
} else{
console.log("Come on!!! Please Write only rock or paper or scissors");
}
}
if(playerWin > computerWin){
console.log("-------------------\nYou WIN! Congratulations\n-------------------");
} else if(playerWin < computerWin){
console.log("-------------------\nI am so sorry! Computer WIN!\n-------------------");
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that you are handling this case

console.log("-------------------\nNo one WIN!\n-------------------");
}
}

game();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

overall, creative code, Great Job!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you all suggest. I will consider next time.