Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript Object Basics Filling Out the Play Method

Carlos Campos
Carlos Campos
6,425 Points

not sure how to pass this

cant figure it out

object.js
const player1 = { 
  name: 'Ashley', 
  color: 'purple', 
  isTurn: true, 
  play: function(){ 
    // write code here. 
    if (this.isTurn) {
   this.name = ('is now playing!')
    }; 
  }
}

2 Answers

Steven Parker
Steven Parker
231,269 Points

The instructions say to return a string equal to the value of the name property followed by the string " is now playing!"

  • you won't need to make an assignment (=)
  • but you do need to "return" the combined string
  • you need to create a string with the name and message put together
  • don't forget to put a space in the string (as shown in the instructions) to separate the name from "is"
Carlos Campos
Carlos Campos
6,425 Points

. if (this.isTurn) { var name ('is now playing!'); };

In addition to what Steven has posted

1) For the name you are to use bracket notation. The syntax is object['property'] where for task 2 object is this and property is name. Don't forget the quotes.

2) To combine two strings you can use the + operator. For example:

let combined = "Hello" + " world!"
console.log(combined)

will log

Hello world!

3) To return a string use the keyword return followed by the string

function myFunction() {
  let combined = "Hello" + " world!";
  return combined;
}

console.log(myFunction())

will log

Hello world!

You could eliminate the variable and produce the same thing

function myFunction() {
  return  "Hello" + " world!";
}
Steven Parker
Steven Parker
231,269 Points

This is not related to this challenge, but if you ever have two literals being concatenated, just eliminate the operator and make a single combined literal:

  return "Hello world!";