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

Practice w/Javascript and Roulette #138

Open
wants to merge 1 commit 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
156 changes: 156 additions & 0 deletions roulette.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
//in the console, roulette is an instance of the Roulette class
//to play, use roulette.spin(your bet, what you're betting on)
//to bet on 00, use the string '00'
//to see your money, use roulette.bankroll();
//to add money, use roulette.buyIn(extra money)


class Roulette {
constructor(startingMoney){
this.money = startingMoney;
this.money =this.money || 100;
this.bankroll = function(){
console.log("You now have $"+this.money);
};
this.payOuts= {
"00":36,
Even: 2,
Odd: 2,
"1 to 18": 2,
"19 to 36": 2,
"1st 12": 3,
"2nd 12": 3,
"3rd 12": 3,
"number":36
};
this.spin= function(betSize, betOn){
var valid = this.validInput(betSize, betOn);
if (!valid[0]){
console.log(valid[1]);
return undefined;
}

var outcome = this.determineOutcome(this.land(), betOn);
this.money = this.money - betSize;
if (outcome[0]){
console.log("You win, the spin was "+outcome[1]);
if (Number.isInteger(betOn)){
betOn = 'number';
}
this.money += betSize * this.payOuts[betOn];
}
else{
console.log("You lose, the spin was "+outcome[1]);
}
this.bankroll();
};

this.buyIn = function(extraMoney){
this.money += extraMoney;
console.log("You bought in $"+extraMoney);
this.bankroll();
};

this.land = function(){
var stop = Math.floor(Math.random()*38);
if (stop ===37){
stop = '00';
}
return stop;
};

this.determineOutcome = function(stop, betOn){
var win = false;
if (Number.isInteger(betOn)){
if (betOn == stop){
win = true;
}
}
else{
switch (betOn) {
case "00":
if (stop === "00"){
win= true;
}
break;
case "Even":
if (((stop%2)==0) && stop>0){
win = true;
}
break;
case "Odd":
if (((stop+1)%2==0) && stop<37){
win=true;
}
break;
case "1 to 18":
if ((stop>0)&&(stop<19)){
win=true;
}
break;
case "19 to 36":
if ((stop>18)&&(stop<37)){
win=true;
}
break;
case "1st 12":
if ((stop>0)&&(stop<13)){
win=true;
}
break;
case "2nd 12":
if ((stop>12)&&(stop<25)){
win=true;
}
break;
case "3rd 12":
if ((stop>24)&&(stop<37)){
win=true;
}
break;
}
}
var outcome = [win, stop];
return outcome;
}

this.validInput = function(betSize, betOn){
var valid= false;
var message="";
if (!Number.isInteger(betSize)){
message= "You have to enter an Integer bet";
}
if (betSize > this.money){
message= "You don't have enough money for this bet";
}
if (betSize < 0){
message= "You can't bet negative money";
}

if ((typeof betOn !== "string") && (typeof betOn !== "number")){
message= "You have to bet on a number or a string";
}

if ((typeof betOn == "number")&&((betOn<0) || (betOn > 36))) {
message= "You have bet on a number between 0 and 36 inclusive";
}

if ((typeof betOn == "string")&&(!this.payOuts[betOn])) {
message= "The string you entered doesn't corrsepond to a possible bet";
}
if (message===""){
valid = true;
}

var outcome = [valid, message];
return outcome;

}

}


}


var roulette = new Roulette();
98 changes: 90 additions & 8 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,114 @@
// FILL IN THE FUNCTIONS BELOW

var sprintFunctions = {
largestEl: function(){
largestEl: function(array){
// your code here
var largest = array[0];
array.forEach(function(element){
if (element > largest){
largest = element;
}
});
return largest;
},

reversed: function(){
reversed: function(string){
// your code here
var charArray=string.split("");
var newString=[];
charArray.forEach(function(element){
newString.unshift(element);
});
newString = newString.join("");
return newString;

},

loudSnakeCase: function(){
loudSnakeCase: function(sentence){
// your code here
var sentenceArray=sentence.split("");
sentenceArray = sentenceArray.filter(function(element){
if (element.match(/[a-zA-Z ]/)){
return true;
}

});
var strippedSentence=sentenceArray.join("");
var wordArray = strippedSentence.split(/ +/g);

wordArray = wordArray.map(function(element){
element=element.split("");
element[0]=element[0].toUpperCase();
element=element.join("");
return element;
});
return wordArray.join("_");

},

compareArrays: function(){
compareArrays: function(array1, array2){
// your code here (replace the return)
return "Finish compareArrays first!"
if (array1.length!=array2.length){
return false;
}
var same=true;
for (var i=0;i<array1.length;i++){
if (array1[i]!==array2[i]){
same=false;
}
}
return same;
},

fizzBuzz: function(){
fizzBuzz: function(number){
// your code here
var array=[];
for (var i=1;i<(number+1);i++){
array.push(i);
}
array=array.map(function(element){
var output="";
if (element%3==0){
output+="FIZZ";
}
if (element%5==0){
output+="BUZZ";
}
if (output==""){
output=element;
}
return output;
});
return array;
},

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

primes: function(){
primes: function(number){
// your code here
var numberArray=[];
var primesArray=[];
for (var i=2;i<=number;i++){
numberArray.push(i);
}

numberArray.forEach(function(num){
var isPrime = true;
primesArray.forEach(function(prime){
if ((num%prime)===0){
isPrime=false;
}
});
if (isPrime ===true){
primesArray.push(num);
}
});
return primesArray;
},
}