Last active
July 6, 2022 03:43
-
-
Save natafaye/daf47ebc8b65e09f5ee073dd2fffaa90 to your computer and use it in GitHub Desktop.
JS Week 6 - 7/5/22 class
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
| class Die { | |
| // letters = array of strings of letters on this die | |
| constructor(letters) { | |
| this.allLetters = letters; | |
| this.currentLetter = letters[0] | |
| } | |
| reroll() { | |
| const randomIndex = Math.floor(Math.random() * this.allLetters.length) | |
| this.currentLetter = this.allLetters[randomIndex]; | |
| } | |
| } | |
| class Board { | |
| constructor() { | |
| this.dice = []; | |
| for(let i = 0; i < 9; i++) { | |
| this.dice.push( new Die(["A", "R", "C", "D", "E", "N"]) ) // all dice will have the same sides | |
| } | |
| } | |
| shuffle() { | |
| // shuffle die locations in array | |
| this.dice.sort(() => (Math.random() > 0.5) ? 1 : -1) | |
| // reroll each die | |
| for(const die of this.dice) { | |
| die.reroll(); | |
| } | |
| } | |
| } | |
| class Game { | |
| constructor() { | |
| this.board = new Board(); | |
| } | |
| start() { | |
| this.board.shuffle() | |
| } | |
| } | |
| const game = new Game(); | |
| game.start(); |
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 doItManyTimes() { | |
| // for(let i = 0; i < 3; i++) { | |
| // doSomething() | |
| // } | |
| // } | |
| // function doSomething() { | |
| // for(let i = 0; i < 3; i++) { | |
| // console.log('something') | |
| // } | |
| // } | |
| // doItManyTimes(); | |
| class Team { | |
| constructor(name) { | |
| this.name = name; | |
| this.players = []; | |
| } | |
| addPlayer = (player) => { | |
| if(player instanceof Player) { | |
| this.players.push(player); | |
| } else { | |
| throw new Error('You can only add an instance of a Player.') | |
| } | |
| } | |
| describe = () => { | |
| return `${this.name} has ${this.players.length} players.`; | |
| } | |
| } | |
| class Player { | |
| constructor(name, position) { | |
| this.name = name; | |
| this.position = position; | |
| } | |
| describe = () => { | |
| return `${this.name} plays ${this.position}.`; | |
| } | |
| } |
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
| const expect = chai.expect; | |
| describe("Team Class", function() { | |
| it("should save the name to the name property", function() { | |
| // Arrange | |
| const newTeamName = "Lakers"; | |
| // Act | |
| const newTeam = new Team(newTeamName) | |
| // Assert | |
| expect(newTeam.name).to.equal(newTeamName) | |
| }) | |
| it("should add a valid player", function() { | |
| // Arrange | |
| const newTeam = new Team("Lakers") | |
| // Act | |
| newTeam.addPlayer(new Player("Mia", "defense")) | |
| // Assert | |
| expect(newTeam.players.length).to.equal(1) | |
| expect(newTeam.players[0].name).to.equal("Mia") | |
| expect(newTeam.players[0].position).to.equal("defense") | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment