8 kyu

"this" is a problem

3,686 of 12,518priyankaherur

Description:

We want to create a constructor function 'NameMe', which takes first name and last name as parameters. The function combines the first and last names and saves the value in "name" property.

We already implemented that function, but when we actually run the code, the "name" property is accessible, but the "firstName" and "lastName" is not accessible. All the properties should be accessible. Can you find what's wrong with it? A test fixture is also available

function NameMe(first, last) {
    this.firstName = first;
    this.lastName = last;
    return {name: this.firstName + ' ' + this.lastName};
}

var n = new NameMe('John', 'Doe');
n.firstName //Expected: John
n.lastName //Expected: Doe
n.name //Expected: John Doe
public class NameMe {
    private String firstName;
    private String lastName;
    private String fullName;

    public NameMe(String first, String last) {
        this.firstName = first;
        this.lastName = last;
   }
 }

NameMe nameMe = new NameMe("John", "Doe");
nameMe.getFirstName(); //Expected: John
nameMe.getLastName(); //Expected: Doe
nameMe.getFullName(); //Expected: John Doe

Fundamentals
Language Features
Object-oriented Programming

Stats:

CreatedDec 1, 2014
PublishedDec 1, 2014
Warriors Trained17864
Total Skips463
Total Code Submissions38887
Total Times Completed12518
JavaScript Completions8985
Java Completions3686
CoffeeScript Completions66
Total Stars131
% of votes with a positive feedback rating74% of 1297
Total "Very Satisfied" Votes843
Total "Somewhat Satisfied" Votes237
Total "Not Satisfied" Votes217
Ad
Contributors
  • priyankaherur Avatar
  • jhoffner Avatar
  • JoshSchreuder Avatar
  • Stargator Avatar
  • user3029010 Avatar
  • KayleighWasTaken Avatar
Ad