From 13ae6eb23eeb3329af0b45df57ea1c1121ccdb27 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sat, 23 Sep 2017 14:14:00 -0500 Subject: [PATCH 1/4] basic problems completed --- scripts.js | 135 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 115 insertions(+), 20 deletions(-) diff --git a/scripts.js b/scripts.js index bbbb844..f44727c 100644 --- a/scripts.js +++ b/scripts.js @@ -1,32 +1,127 @@ // FILL IN THE FUNCTIONS BELOW var sprintFunctions = { - largestEl: function(){ - // your code here - }, - - reversed: function(){ - // your code here + largestEl: function(arr){ + arr = arr.sort(); + return arr[arr.length-1]; }, - loudSnakeCase: function(){ - // your code here + reversed: function(str){ + var result = [] + for (var i = 0; i < str.length; i++) { + result.unshift(str[i]); + } + return result.join(''); }, - compareArrays: function(){ - // your code here (replace the return) - return "Finish compareArrays first!" - }, + loudSnakeCase: function(str){ + var words = str.split(' '), + punctuation = '!,.;:-', + result = [], + result2 = []; - fizzBuzz: function(){ - // your code here - }, + // elminates multiple consecutive spaces + words = words.filter(function(word){ + return word.length > 0; + }); + + + words.forEach(function(word){ + var letters = word.split(''), + temp = []; + + letters.forEach(function(letter,index){ + if (punctuation.includes(letter) === false){ + if (index === 0) { + temp.push(letter.toUpperCase()); + } else { + temp.push(letter.toLowerCase()); + } + } + }); - myMap: function(){ - // your code here + result.push(temp.join('')); + }); + + return result.join('_'); }, - primes: function(){ - // your code here + compareArrays: function(arr1,arr2) { + var answer = true; + + arr1.forEach(function(element,index){ + if (arr1[index] !== arr2[index]) { + answer = false; + } + }); + + return answer; }, -} \ No newline at end of file + + fizzBuzz: function(n) { + var result = []; + + for (i=1; i < n+1; i++) { + if (i % 5 === 0 && i % 3 === 0) { + result.push("FIZZBUZZ"); + } else if (i % 5 === 0) { + result.push("BUZZ"); + } else if (i % 3 === 0) { + result.push("FIZZ"); + } else + result.push(i); + } + + return result; +}, + + myMap: function(arr,func) { + var result = []; + + arr.forEach(function(element) { + result.push(func(element)); + }); + + return result; +}, + + primes: function(n) { + + //helper function to test for primality + var isPrime = function (n){ + if (n < 2) { + return false; + } + + //creating range + var range = []; + for (var i = 2; i < n; i++) { + range.push(i); + } + + var prime = true; + + //checking for primality + range.forEach(function(divisor){ + if (n % divisor === 0) { + prime = false; + } + }); + + return prime; +} + + //checking all numbers from 2 to input parameter + var result = []; + if (n < 2) { + return result; + } else + for (var i = 2; i < n; i++) { + if (isPrime(i)) { + result.push(i); + } + } + + return result; +}, +} From 237117d2d037de4e9b528964b527e1ef29aff8d7 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sun, 24 Sep 2017 15:02:12 -0500 Subject: [PATCH 2/4] roulette game started --- roulette.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 roulette.js diff --git a/roulette.js b/roulette.js new file mode 100644 index 0000000..579c151 --- /dev/null +++ b/roulette.js @@ -0,0 +1,20 @@ +var bankroll = 100; + +var bet = prompt(`Your bankroll is currently $${bankroll}. How much would you like to bet?`); +bet = Number(bet)+0; +//change bet to intgeger + +while (isNaN(bet)) { + bet = prompt(`That is not a valid bet. Please place a bet between $1 and your currrent bankroll of $${bankroll}.`); + bet = Number(bet)+0; +} + + +var lucky_number = prompt(`$${bet} on which number?`); +while (isNaN(lucky_number) || lucky_number < 1 || lucky_number > 36) { + lucky_number = prompt(`That is not a valid bet. Please place a bet on a number between 1 and 36.`); + lucky_number = Number(lucky_number)+0; +} + + +console.log(typeof(Number(bet))); From 29cdeacaa7e2104cf78b421253087d5604ec63b4 Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sun, 24 Sep 2017 17:50:32 -0500 Subject: [PATCH 3/4] step 1, basic game completed --- roulette.js | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/roulette.js b/roulette.js index 579c151..4d03569 100644 --- a/roulette.js +++ b/roulette.js @@ -1,20 +1,44 @@ -var bankroll = 100; +var bankroll = 100, + repeat = true; -var bet = prompt(`Your bankroll is currently $${bankroll}. How much would you like to bet?`); -bet = Number(bet)+0; -//change bet to intgeger - -while (isNaN(bet)) { - bet = prompt(`That is not a valid bet. Please place a bet between $1 and your currrent bankroll of $${bankroll}.`); +while (bankroll > 0 && repeat === true){ + //placing bet + var bet = prompt(`Your bankroll is currently $${bankroll}. How much would you like to bet?`); bet = Number(bet)+0; -} -var lucky_number = prompt(`$${bet} on which number?`); -while (isNaN(lucky_number) || lucky_number < 1 || lucky_number > 36) { - lucky_number = prompt(`That is not a valid bet. Please place a bet on a number between 1 and 36.`); - lucky_number = Number(lucky_number)+0; + while (isNaN(bet)) { + bet = prompt(`That is not a valid bet. Please place a bet between $1 and your currrent bankroll of $${bankroll}.`); + bet = Number(bet)+0; + } + + //spin + var lucky_number = prompt(`$${bet} on which number?`); + while (isNaN(lucky_number) || lucky_number < 1 || lucky_number > 36) { + lucky_number = prompt(`That is not a valid bet. Please place a bet on a number between 1 and 36.`); + lucky_number = Number(lucky_number)+0; + } + + var actual_number = Math.floor(Math.random() * (Math.floor(36) - Math.ceil(1) + 1)) + Math.ceil(1); + + + //settling up + if (lucky_number === actual_number) { + bankroll = bankroll + (bet * 35); + alert(`Congratulations! You won ${bet*35} and your balance is now $${bankroll}.`); + } else + bankroll = bankroll - bet; + alert(`I'm sorry, you lose! Your balance is now $${bankroll}.`); + + repeat = confirm("Would you like to continue?"); + if (repeat === false){ + alert("Thanks for playing!");} + else if (repeat === true && bankroll < 1) { + bankroll = prompt(`I'm sorry, your bankroll is currently $${bankroll}. How much would you like to buy in?`); + } } -console.log(typeof(Number(bet))); + + +console.log(actual_number); From 086428256959f4d9ecbd14e1764d78255dab0bdd Mon Sep 17 00:00:00 2001 From: Hai Nguyen Date: Sun, 24 Sep 2017 21:30:52 -0500 Subject: [PATCH 4/4] additional bets options added, game complete --- roulette.js | 66 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/roulette.js b/roulette.js index 4d03569..0672b0e 100644 --- a/roulette.js +++ b/roulette.js @@ -1,35 +1,56 @@ var bankroll = 100, - repeat = true; + repeat = true, + wheel = []; + for (var i=0; i < 37; i++){ + wheel.push(`${i}`); + } + wheel.push('00'); +var valid_bets = wheel.concat(["Even","Odd","1 to 18", "19 to 36", "1st 12", "2nd 12", "3rd 12"]); -while (bankroll > 0 && repeat === true){ - //placing bet - var bet = prompt(`Your bankroll is currently $${bankroll}. How much would you like to bet?`); - bet = Number(bet)+0; +while (bankroll > 0 && repeat === true){ + //placing bet amount + var bet_amount = prompt(`Your bankroll is currently $${bankroll}. How much would you like to bet?`); + bet_amount = Number(bet_amount)+0; - while (isNaN(bet)) { - bet = prompt(`That is not a valid bet. Please place a bet between $1 and your currrent bankroll of $${bankroll}.`); - bet = Number(bet)+0; + while (isNaN(bet_amount)) { + bet_amount = prompt(`That is not a valid bet. Please place a bet between $1 and your currrent bankroll of $${bankroll}.`); + bet_amount = Number(bet_amount)+0; } - //spin - var lucky_number = prompt(`$${bet} on which number?`); - while (isNaN(lucky_number) || lucky_number < 1 || lucky_number > 36) { - lucky_number = prompt(`That is not a valid bet. Please place a bet on a number between 1 and 36.`); + //placing bet + var lucky_number = prompt(`$${bet_amount} on which number(s)? [0-36, 00, Even, Odd, 1 to 18, 19 to 36, 1st 12, 2nd 12, 3rd 12]`); + while (valid_bets.includes(lucky_number) === false) { + lucky_number = prompt(`That is not a valid bet. Please place a valid bet. [0-36, 00, Even, Odd, 1 to 18, 19 to 36, 1st 12, 2nd 12, 3rd 12]`); lucky_number = Number(lucky_number)+0; } - var actual_number = Math.floor(Math.random() * (Math.floor(36) - Math.ceil(1) + 1)) + Math.ceil(1); - + //spin + var actual_number = wheel[Math.floor(Math.random()*wheel.length)]; + alert(`Roll lands on number ${actual_number} and you bet on ${lucky_number}.`); //settling up if (lucky_number === actual_number) { - bankroll = bankroll + (bet * 35); - alert(`Congratulations! You won ${bet*35} and your balance is now $${bankroll}.`); - } else - bankroll = bankroll - bet; - alert(`I'm sorry, you lose! Your balance is now $${bankroll}.`); - + bankroll = bankroll + (bet_amount * 35); + alert(`Congratulations! You won ${bet_amount*35} and your balance is now $${bankroll}.`); + // 1:1 payouts + } else if (lucky_number === "Even" && actual_number % 2 === 0 || lucky_number === "Odd" && actual_number % 2 !== 0 || + lucky_number === "1 to 18" && wheel.slice(1,19).includes(actual_number) || + lucky_number === "19 to 36" && wheel.slice(19,37).includes(actual_number) ) { + bankroll = bankroll + bet_amount; + alert(`Congratulations! You won ${bet_amount} and your balance is now $${bankroll}.`); + // 2:1 payouts + } else if (lucky_number === "1st 12" && wheel.slice(1,13).includes(actual_number) || + lucky_number === "2nd 12" && wheel.slice(12,25).includes(actual_number) || + lucky_number === "3rd 12" && wheel.slice(25,37).includes(actual_number) ){ + bankroll = bankroll + (bet_amount * 2); + alert(`Congratulations! You won ${bet_amount} and your balance is now $${bankroll}.`); + } else { + bankroll = bankroll - bet_amount; + alert(`I'm sorry, you lose! Your balance is now $${bankroll}.`)} + + + //again? repeat = confirm("Would you like to continue?"); if (repeat === false){ alert("Thanks for playing!");} @@ -37,8 +58,3 @@ while (bankroll > 0 && repeat === true){ bankroll = prompt(`I'm sorry, your bankroll is currently $${bankroll}. How much would you like to buy in?`); } } - - - - -console.log(actual_number);