Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // Not working - example of spread
- const gameOver = results => console.log(`Game Over - ${results}`)
- // function to log the results when game is over
- const rollAgain = point =>
- console.log(`The point is ${point}, try again`)
- const craps = (roll, point) => new Promise((gameOver, rollAgain) => {
- //if roll is not sent as a number between 2 and 12, rollAgain
- if (!roll || typeof roll !== "number" || roll <2 || roll >12){
- rollAgain("to roll a number")
- }
- // If point is not set, this must be the first roll, the come out roll
- else if (!point){
- if (roll ===7 || roll === 11){
- gameOver("You win by natural")
- } else if (roll ===2 || roll === 3){
- gameOver("You have lost, crapped out")
- }else {
- rollAgain(roll)
- }
- }else if (roll === point){
- gameOver("You win, you hit the point")
- }else {
- if (roll === 7) {
- gameOver ("you loose, craps")
- }else {
- rollAgain(point)
- }
- }
- })
- craps("foo").then(gameOver, rollAgain ) // The point is to roll a number, try again
- craps(7).then(gameOver, rollAgain ) // Game Over - You win by natural
- craps(2).then(gameOver, rollAgain ) // Game Over - You lose, crapped out
- craps(8).then(gameOver, rollAgain ) // The point is 8, try again
- craps(5,8).then(gameOver, rollAgain ) // The point is 8, try again
- craps(7,8).then(gameOver, rollAgain ) // Game over - You lose, craps
- craps(8,8).then(gameOver, rollAgain ) // Game over - You win, you hit the point
- )
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// Not working - example of spread
- const gameOver = results => console.log(`Game Over - ${results}`)
- // function to log the results when game is over
- const rollAgain = point =>
- console.log(`The point is ${point}, try again`)
- const craps = (roll, point) => new Promise((gameOver, rollAgain) => {
- //if roll is not sent as a number between 2 and 12, rollAgain
- if (!roll || typeof roll !== "number" || roll <2 || roll >12){
- rollAgain("to roll a number")
- }
- // If point is not set, this must be the first roll, the come out roll
- else if (!point){
- if (roll ===7 || roll === 11){
- gameOver("You win by natural")
- } else if (roll ===2 || roll === 3){
- gameOver("You have lost, crapped out")
- }else {
- rollAgain(roll)
- }
- }else if (roll === point){
- gameOver("You win, you hit the point")
- }else {
- if (roll === 7) {
- gameOver ("you loose, craps")
- }else {
- rollAgain(point)
- }
- }
- })
- craps("foo").then(gameOver, rollAgain ) // The point is to roll a number, try again
- craps(7).then(gameOver, rollAgain ) // Game Over - You win by natural
- craps(2).then(gameOver, rollAgain ) // Game Over - You lose, crapped out
- craps(8).then(gameOver, rollAgain ) // The point is 8, try again
- craps(5,8).then(gameOver, rollAgain ) // The point is 8, try again
- craps(7,8).then(gameOver, rollAgain ) // Game over - You lose, craps
- craps(8,8).then(gameOver, rollAgain ) // Game over - You win, you hit the point
- )</script></body>
- </html>
Add Comment
Please, Sign In to add comment