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 (2015) Introduction to Methods Understanding this

Modify this object so it uses two properties firstName and lastName and remove their variable declarations

I'm confused by this question,

The videos gave a simple example of:

var dice = { // Very simple object sides: 6, roll: function () { var randomNumber = Math.floor(Math.random() * this.sides) + 1; console.log(randomNumber); } }

How should I approach this challenge?

object.js
var contact = {
  fullName: function() {
    var firstName = "Andrew";
    var lastName = "Chalkley";
    console.log(firstName + " " + lastName);
  }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

To convert the function variables you could move them from inside the function and make them peers of "fullname" (like "sides" is to "roll" in the dice object).

The syntax for properties uses a colon instead of an equal sign to separate the names from the values, and the "var" keyword is not needed. Also, the complete property name/value pairs are separated by commas instead of semicolons.

So..

  1. Re-position the function below firstName and lastName
  2. Change the = to : on firstName and lastName - Remove the var keyword on them both
  3. End firstName and lastName with "commas" not "semicolons
var contact ={
      firstName: "Andrew",
      lastName: "Chalkley",
    fullName: function() {
      console.log(firstName + " " + lastName);
   }
}
Steven Parker
Steven Parker
231,269 Points

Looks good. :+1: That should get you past task 1.

Thanks a lot Steven!

That's the first time I've ever moved a function around. Onwards and upwards at this point!