From 02e29ce6721d6ed976f414ad3b1887183e493da1 Mon Sep 17 00:00:00 2001 From: Lakshmi Date: Tue, 24 Oct 2017 20:05:25 -0700 Subject: [PATCH] Submitting first JS assignment --- README.md | 5 +++ Roulette.js | 100 ++++++++++++++++++++++++++++++++++++++++++ scripts.js | 113 ++++++++++++++++++++++++++++++++++++++++++------ scripts_spec.js | 3 +- 4 files changed, 206 insertions(+), 15 deletions(-) create mode 100644 Roulette.js diff --git a/README.md b/README.md index 4f9c999..ee46f31 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Roulette.js b/Roulette.js new file mode 100644 index 0000000..36b37c1 --- /dev/null +++ b/Roulette.js @@ -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"); \ No newline at end of file diff --git a/scripts.js b/scripts.js index bbbb844..ec91b77 100644 --- a/scripts.js +++ b/scripts.js @@ -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 - }, -} \ No newline at end of file + 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; + } +}; \ No newline at end of file diff --git a/scripts_spec.js b/scripts_spec.js index 9b9b19e..49cba12 100644 --- a/scripts_spec.js +++ b/scripts_spec.js @@ -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)); @@ -101,4 +101,3 @@ var tester = { $(document).ready( function(){ tester.init( sprintFunctions )}); -