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

Submitting first JS assignment #144

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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ assignment_js_sprint
====================

while(1){ go() };

Hi! This is Lakshmi Kameswari.
Roulette.js file contains code and test case for the Roulette game.
The code in the Roulette.js file can be pasted in any online text editor like repl.it and tested.
scripts.js contains solutions for the all the given coding problems.
100 changes: 100 additions & 0 deletions Roulette.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
function Roulette(amt){
this.amt = amt;
this.spin_no = 0;
this.payout_ratio = 35;
this.amt_won = 0;
this.win_lose = function(won){
if (won === true){
console.log(`You Win $ ${this.amt_won}, the spin was ${this.spin_no}!!!`);
console.log(`You now have $ ${this.amt}`);
}else{
console.log(`You Lose, the spin was ${this.spin_no} :(`);
console.log(`You now have $ ${this.amt}`);
}
};
this.spin = function(bet_amt, bet_num){
var won = false;
this.spin_no = Math.floor((Math.random())*36+1+2);
var amt_won = 0;
if(this.spin_no === 37){this.spin_no = "0"}
if(this.spin_no === 38){this.spin_no = "00"}
if(this.spin_no === bet_num){
won = true;
this.payout_ratio = 35;
this.amt_won = bet_amt*this.payout_ratio;
this.win_lose(won);
this.amt = this.amt + amt_won;
}
else if ((bet_num === "Even") && ((this.spin_no%2 === 0))){
won = true;
this.payout_ratio = 1;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else if ((bet_num === "Odd") && ((this.spin_no%2 === 1))){
won = true;
this.payout_ratio = 1;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else if ((bet_num === "1 to 18") && (this.spin_no>= 1 && this.spin_no <=18)){
won = true;
this.payout_ratio = 1;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else if ((bet_num === "19 to 36") && (this.spin_no >= 19 && this.spin_no <= 36)){
won = true;
this.payout_ratio = 1;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else if ((bet_num === "1st 12") && (this.spin_no>=1 && this.spin_no<=12)){
won = true;
this.payout_ratio = 2;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else if ((bet_num === "2nd 12") && (this.spin_no>=12 && this.spin_no <= 24)){
won = true;
this.payout_ratio = 2;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else if ((bet_num === "3rd 12") && (this.spin_no>= 24 && this.spin_no <= 36)){
won = true;
this.payout_ratio = 2;
this.amt_won = bet_amt*this.payout_ratio;
this.amt = this.amt + this.amt_won;
this.win_lose(won);
}else{
won = false;
this.amt = this.amt - bet_amt;
this.win_lose(won);
}
this.bankroll = function(){
console.log(`You now have $ ${this.amt}`);
};
this.buyIn = function(money){
this.amt = this.amt + money;
console.log(`You now have $ ${this.amt}`);
}
}
}


//Testing the Roulette game
var roul = new Roulette(100);
roul.spin(10, "00");
roul.spin(50, "1st 12");//win = 200; lose = 50
roul.spin(10, "Even");
roul.spin(20, "Odd");
roul.spin(30, "1 to 18");
roul.spin(10, "2nd 12");
roul.bankroll();
roul.spin(20, "19 to 36");
roul.spin(10, "3rd 12");
roul.buyIn(1000);
roul.bankroll();
roul.spin(10, "0");
roul.spin(10, "00");
113 changes: 100 additions & 13 deletions scripts.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,119 @@
// FILL IN THE FUNCTIONS BELOW

var sprintFunctions = {
largestEl: function(){
largestEl: function(arr){
// your code here
var idx = 0;
largest = 0;
while (idx < arr.length){
if (arr[idx] > largest)
{
largest = arr[idx];
}
idx++;
}
return largest;
},
reversed: function(){

reversed: function(str){
// your code here
var str_array = str.split("");
var n = str_array.length-1
var x = 0;
while(x < (Math.floor(str.length/2)))
{
temp = str_array[x];
str_array[x] = str_array[n-x];
str_array[n-x] = temp;
x++;
}
str_reversed = str_array.join("");
return str_reversed;
},

loudSnakeCase: function(){
loudSnakeCase: function(str){
// your code here
loud_array = [];
words_arr = str.split(" ");
for(var i = 0; i < words_arr.length; i++)
{
if (words_arr[i] == ""){
continue
}else{
words_arr[i] = words_arr[i].replace(/[^0-9A-Za-z]/gi, '');
words_arr[i] = words_arr[i].charAt(0).toUpperCase()+words_arr[i].slice(1);
loud_array.push(words_arr[i]);
}
}
var str_loud = loud_array.join("_");
return str_loud;
},

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

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

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

primes: function(){
// your code here
},
}
factors: function(num){
factors_arr = [];
limit = Math.floor(num/2);
for(var x = 1; x <= limit; x++){
if (num%x == 0){
factors_arr.push(x);
factors_arr.push(num/x);
}
}
return factors_arr;
},
primes: function(num){
primes_arr = [];
for(var n = 2; n < num; n++){
factor_arr = this.factors(n);
if (factor_arr.length <= 2){
primes_arr.push(n);
}
}
return primes_arr;
}
};
3 changes: 1 addition & 2 deletions scripts_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var tester = {

displayResults: function(){
console.log("Displaying results...");
console.log(this.results)
console.log(this.results);
$("#largest-el").html(String(this.results.largestEl));
$("#reversed").html(String(this.results.reversed));
$("#loud-snake-case").html(String(this.results.loudSnakeCase));
Expand All @@ -101,4 +101,3 @@ var tester = {


$(document).ready( function(){ tester.init( sprintFunctions )});