-
Notifications
You must be signed in to change notification settings - Fork 12
home work Rock, Paper, Scissors #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: Umit-Ilhan
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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']; | ||
| 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}`}`; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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")){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overall, creative code, Great Job!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you all suggest. I will consider next time. |
||
|
|
||
There was a problem hiding this comment.
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