0

In the startGame()method, I am trying to store the selected phrase from the getRandomPhrase() method to the null property. It keeps throwing me an error saying

Game.activePhrase is null

What am I doing wrong?

Game.js:

class Game {

  constructor() {
    this.missed = 0;
    //directly put the phrases in the constructor
    this.phrases = [new Phrase("hello world"),
      new Phrase("Wolf on wall street"),
      new Phrase("Despite making"),
      new Phrase("Karen took the kids"),
      new Phrase("alright about to head out")
    ];
    this.activePhrase = null;
  }

  getRandomPhrase() {
    //returns 5 of the random phrases
    return this.phrases[Math.floor(Math.random() * this.phrases.length)];
  }

  startGame() {
    let hid = document.getElementById('overlay');
    hid.style.display = "none";
    let phrs = this.getRandomPhrase();
    phrs.addPhraseToDisplay();
    return this.activePhrase(phrs); //trying to store it by doing this
  }
}

App.js

const game = new Game();
game.startGame();
console.log(`Active Phrase - phrase: ${game.activePhrase.phrase}`);
1
  • this.activePhrase = phrs; ? Commented Mar 13, 2019 at 7:01

3 Answers 3

1

Assign value to activePhase variable before returning it from startGame method

this.activePhrase = phrs;

Like

  startGame() {
    let hid = document.getElementById('overlay');
    hid.style.display = "none";
    let phrs = this.getRandomPhrase();
    phrs.addPhraseToDisplay();
    this.activePhrase = phrs; //Key to fix your issue
    return this.activePhrase;
  }
Sign up to request clarification or add additional context in comments.

Comments

0

You never assigned to .activePhrase (you tried to call .activePhrase as if it was a function, but it's not). Try assigning phrs to this.activePhrase instead:

this.activePhrase = phrs;

Comments

0

this.activePhrase is not a functoin - therefore this.activePhrase(phrs) will not do anything. Simply assign the value:

this.activePhrase = phrs;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.