Challenge: Solution Review
This lesson will explain the solution to the problem from the previous coding challenge.
We'll cover the following...
Solution
Press + to interact
// old interfaceclass TruthAndDare {constructor(){this.turn = Math.floor(Math.random() * 2) + 1;}Getturn(){if(this.turn == 1){this.turn = 2}else{this.turn = 1}return this.turn}playGame(playerOnename,playerTwoname){if(this.Getturn() == 1){return`${playerOnename}'s turn`}else{return `${playerTwoname}'s turn`}}}// new interfaceclass NewTruthAndDare {constructor(randomValue){this.turn = randomValue}newplayGame(playerOnename,playerTwoname){if((this.turn % 2) == 0){return `${playerOnename}'s turn`}else{return `${playerTwoname}'s turn`}}}// Adapter Classclass Adapter {constructor(randomValue) {const newGame = new NewTruthAndDare(randomValue)this.playGame = function(playerOnename,playerTwoname) {return newGame.newplayGame(playerOnename,playerTwoname)};}}const obj = new Adapter(6) //pass even/odd values here to see varying resultsconsole.log(obj.playGame("Ross","Chandler"))
Explanation
In the code above, we have a TruthAndDare
class, which has a ...