Last active
April 25, 2017 12:49
-
-
Save deneuv34/a899f7c93d856a13358c20d2818c29ac to your computer and use it in GitHub Desktop.
Excercise javascript cand and Pretify number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // var num = Math.round(Math.random() * 12) + 1; | |
| // var card = Math.round(Math.random() * 3) + 1; | |
| class deckOfCards { | |
| constructor(cards, nums) { | |
| this.nums = nums; | |
| this.card = cards; | |
| this.deck = (t, n) => { | |
| let arr = []; | |
| for(let i = 0; i < type.length; i++) { | |
| for(let j = 0; j < number.length; j++) { | |
| arr.push({ type: type[i], number: number[j]}); | |
| } | |
| } | |
| return arr; | |
| }; | |
| } | |
| suffleDeck() { | |
| let arr = this.deck(); | |
| let i = arr.length; | |
| let temp = ''; | |
| while(0 !== i){ | |
| i -= 1 | |
| let randIndex = Math.round(Math.random() * i); | |
| temp = arr[i] | |
| arr[i] = arr[randIndex]; | |
| arr[randIndex] = temp; | |
| } | |
| return arr; | |
| } | |
| pickOneCardRandom() { | |
| let arr = this.deck(); | |
| let random = Math.round(Math.random() * (arr.length - 1)); | |
| return arr[random]; | |
| } | |
| pickOneCard(type, number) { | |
| let arr = this.deck(); | |
| let i = 0; | |
| for(i; i < 52; i++) { | |
| if(arr[i].number === number && arr[i].type === type) { | |
| return { type: arr[i].type , number: arr[i].number }; | |
| break; | |
| } else { | |
| } | |
| } | |
| // console.log(arr[2]) | |
| } | |
| notPickOneCard(type, number, picked) { | |
| let arr = this.deck(); | |
| let i = 0; | |
| for(i; i < 52; i++) { | |
| if(arr[i].number === number && arr[i].type === type) { | |
| if(arr[i].number === picked.number && arr[i].type === picked.type) { | |
| return 'card is has been picked before'; | |
| break; | |
| } else { | |
| return { type: arr[i].type , number: arr[i].number }; | |
| break; | |
| } | |
| } else { | |
| } | |
| } | |
| // console.log(arr[2]) | |
| } | |
| } | |
| const type = ['Spade', 'Hearts', 'Spades', 'Cube']; | |
| const number = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']; | |
| let cards = []; | |
| const cardSelect = { | |
| type: 'Hearts', | |
| number: 'Ace' | |
| } | |
| const deck = new deckOfCards(); | |
| console.log(deck.suffleDeck()); | |
| console.log('======================'); | |
| console.log('RANDOM CARD: ', deck.pickOneCardRandom()); | |
| console.log('======================'); | |
| console.log('PICK ONE: ',deck.pickOneCard(cardSelect.type, cardSelect.number)); | |
| var pickedCard = deck.pickOneCard(cardSelect.type, cardSelect.number); | |
| console.log('======================'); | |
| console.log(deck.notPickOneCard('Hearts', '10', pickedCard)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function convert(n) { | |
| let x; | |
| if(n < 100) { | |
| return n.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]; | |
| } | |
| if((n / 1000) < 1000 && (n / 1000 ) > 0) { | |
| x = (n / 1000); | |
| if((''+x).split('')[2] > 0) { | |
| x = x.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]; | |
| x += 'k' | |
| } else { | |
| x += ' k' | |
| } | |
| } else if((n / Math.pow(10, 6)) < Math.pow(10 ,3) && (n / Math.pow(10, 6)) > 0) { | |
| x = (n / Math.pow(10, 6)); | |
| if((''+x).split('')[2] > 0) { | |
| x = x.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]; | |
| x = x + ' M'; | |
| } else { | |
| x += ' M' | |
| } | |
| } else if((n / Math.pow(10, 9)) < 1000 && (n / Math.pow(10, 9)) > 0) { | |
| x = (n / Math.pow(10, 9)); | |
| if((''+x).split('')[2] > 0) { | |
| x = x.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]; | |
| x = x + ' B'; | |
| } else { | |
| x += ' B' | |
| } | |
| } | |
| return x; | |
| } | |
| console.log(convert(1238.5)); | |
| console.log(convert(Math.pow(12,6))); | |
| console.log(convert(12.999)); | |
| console.log(convert(1999)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment