Skip to content
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

Javascript basic functions and roulette #154

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="scripts.js"></script>
<script src="scripts_spec.js"></script>
<script src="roulette.js"></script>
<style>
h1,h2,h3,p{
text-align: center;
Expand Down
120 changes: 120 additions & 0 deletions roulette.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Roulette game

alert("In this game you will be able to place bets on a roulette wheel. We have taken the courtesy of giving you a starting bank roll of £100.")

// function roulette(num) {
var player = {
balance: 100,
buyIn: 0,
bet: 0
}

while(true) {

//Ask the player if they want to add to their bank roll and record these for later.
if (confirm("Would you like to add to you bank roll?") === true) {
while (true){

var bought = parseInt(prompt("How much would you like to borrow to add to your bankroll?"));

if(bought > 0) {
player.balance += bought;
player.buyIn += bought;
break;
}
else{
alert("Please enter an amount more than 0");
}
}
}

while(true){
alert("You now have £" + player.balance);
player.bet = parseInt(prompt("How much would you like to bet?"));

if(player.bet > player.balance){
alert("You haven't got enough funds to place that bet");
}
else if(player.bet > 0){
player.balance -= player.bet
break;
}
else{
alert("Please input a valid amount to bet");
}

}

// Spin
var spin = Math.floor((Math.random() * 36)) + 1;
var betMultiple = 0;

while(true){
var type = prompt("What type of bet do you want to place?");
// 1: 0 - (35:1)
// 2: 00 - (35:1)
// 3: Even - (1:1)
// 4: Odd - (1:1)
// 5: 1 to 18 - (1:1)
// 6: 19 to 36 - (1:1)
// 7: 1 to 12 - (2:1)
// 8: 13 to 24 - (2:1)
// 9: 25 to 36 - (2:1)

// 1: 0 -> (35:1)
if(type === 1 && spin === 35){
betMultiple = 35;
}
// 2: 00 -> (35:1)
else if(type === 2 && spin === 37){
betMultiple = 35;
}
// 3: Even -> (1:1)
else if(type === 3 && spin % 2 === 0){
betMultiple = 1;
}
// 4: Odd -> (1:1)
else if(type === 4 && spin % 2 !== 0){
betMultiple = 1;
}
// 5: 1 to 18 -> (1:1)
else if(type === 5 && (spin >= 1 && spin <= 18)){
betMultiple = 1;
}
// 6: 19 to 36 -> (1:1)
else if(type === 6 && (spin >= 19 && spin <= 36)){
betMultiple = 1;
}
// 7: 1 to 12 -> (2:1)
else if(type === 7 && (spin >= 1 && spin <= 12)){
betMultiple = 2;
}
// 8: 13 to 24 -> (2:1)
else if(type === 8 && (spin >= 13 && spin <= 24)){
betMultiple = 2;
}
// 9: 25 to 36 -> (2:1)
else if(type === 9 && (spin >= 25 && spin <= 36)){
betMultiple = 2;
}

var amount = (player.bet * betMultiple)

if(betMultiple > 0) {
player.balance += amount
alert("You Win "+amount+" the spin was "+spin+"!");
break;
}
else{
// player.balance -= amount;
alert("You Lose the spin was "+spin+"!");
break;
}
}

alert("You have "+player.balance+"!");

if (confirm("Would you like to continue playing?") === false){
break;
}
}
84 changes: 68 additions & 16 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,84 @@
// FILL IN THE FUNCTIONS BELOW

var sprintFunctions = {
largestEl: function(){
// your code here
largestEl: function(arr){
var current = arr[0]
for(i = 1; i< arr.length; i++){
if(arr[i] > current){
current = arr[i];
}
}
return current;
},

reversed: function(){
// your code here
reversed: function(str){
reversedStr = "";
for(i = str.length-1 ; i>= 0; i--){
reversedStr += str.charAt(i);
}
return reversedStr;
},

loudSnakeCase: function(){
// your code here
loudSnakeCase: function(sentence){
sentence = sentence.replace(/\b\w/g, function(l){ return l.toUpperCase(); })
sentence = sentence.replace(/\s+/g, "_")
return sentence.replace(/[!?.]/g, "")
},

compareArrays: function(){
// your code here (replace the return)
return "Finish compareArrays first!"
compareArrays: function(arr1, arr2){
res = true

for(i=0; i< arr1.length; i++){
if(arr1[i] != arr2[i]){
res = false;
break;
}
}
return res;
},

fizzBuzz: function(){
// your code here
fizzBuzz: function(num){
arr = []
for(i=1; i<=num; i++){
if(i%3 === 0 && i%5 === 0) {
arr.push("FIZZBUZZ");
}
else if(i%3 === 0 ){
arr.push("FIZZ");
}
else if(i%5 === 0 ){
arr.push("BUZZ");
}
else{
arr.push(i);
}
}
return arr
},

myMap: function(){
// your code here
myMap: function(arr, func){
newArr = []
for(i=0; i<arr.length; i++){
newArr.push(func(arr[i]))
}
return newArr;
},

primes: function(){
// your code here
primes: function(num){
arr =[]

for(i=2; i<num; i++){
is_prime = true
for(j=i-1; j>=2; j--){
if(i%j === 0){
is_prime = false
}
}

if(is_prime){
arr.push(i);
}
}
return arr
},
}
}