-2

How can I make the line inside the if statement work?

const player1 = {
    name: "Ashley",
    color: "purple",
    isTurn: true,
    play: function() {
        if (this.isTrue) {
            return `this["name"] is now playing`;
        }
    }
};
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
newtocode
  • 21
  • 1
  • 4
  • Possible duplicate of [How can I do string interpolation in JavaScript?](https://stackoverflow.com/questions/1408289/how-can-i-do-string-interpolation-in-javascript) – Igor Apr 25 '18 at 15:16
  • By using `return`. But I suspect you're actually asking something else. – Dave Newton Apr 25 '18 at 15:17
  • Can you please clarify what you expect the code to do, and if you now are getting any erroneous output, that too. The return statement is itself completely valid. – Sami Hult Apr 25 '18 at 15:19

5 Answers5

2

Replace the string with this:

return `${this["name"]} is now playing`;
Sayegh
  • 1,381
  • 9
  • 17
0

Either make the property as isTrue or make the if statement variable check 'isTurn'.

theAnubhav
  • 535
  • 6
  • 16
0

For a start, the if statement won't be hit as isTrue isn't a vaiable in player1.

However:

const player1 = {
    name: "Ashley",
    color: "purple",
    isTurn: true,
    isTrue: true, // Do you need this?
    play: function() {
        if (this.isTrue) { // Or should this be "inTurn"?
            console.log(this.name + " is now playing");
            return this.name + " is now playing";
        }
    }
};

player1.play();

You can see this running here: https://jsfiddle.net/ohfu2kn6/

Chris Dixon
  • 9,147
  • 5
  • 36
  • 68
0

I think you're just referring to isTrue when you meant to use the property isTurn in your if statement.

const player1 = {
    name: "Ashley",
    color: "purple",
    isTurn: true,
    play: function () {
        if (this.isTurn) {
            return this.name + ' is now playing';
        }
    }
};
bri
  • 2,932
  • 16
  • 17
-1
const player1 = {
name: "Ashley",
color: "purple",
isTurn: true,
play: function() {
    if (this.isTrue) {
        return this.name +" is now playing";
    }
  }
};
odlp
  • 4,984
  • 2
  • 34
  • 45