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 interface
class 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 interface
class NewTruthAndDare {
constructor(randomValue){
this.turn = randomValue
}
newplayGame(playerOnename,playerTwoname){
if((this.turn % 2) == 0){
return `${playerOnename}'s turn`
}else{
return `${playerTwoname}'s turn`
}
}
}
// Adapter Class
class 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 results
console.log(obj.playGame("Ross","Chandler"))

Explanation

In the code above, we have a TruthAndDare class, which has a ...