Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: '"this" is a problem' #359

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions kata/8 kyu/this-is-a-problem/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import NameMe from '.';

describe('nameMe', () => {
it('should return the full name', () => {
expect.assertions(3);

const n = new (NameMe as any)('John', 'Doe');

expect(typeof n.firstName).toBeDefined();
expect(n.firstName).toBe('John');

expect(typeof n.lastName).toBeDefined();
expect(n.lastName).toBe('Doe');

expect(typeof n.name).toBeDefined();
expect(n.name).toBe('John Doe');
});
});
13 changes: 13 additions & 0 deletions kata/8 kyu/this-is-a-problem/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
interface Me {
firstName: string;
lastName: string;
name: string;
}

function NameMe(this: Me, first: string, last: string) {
this.firstName = first;
this.lastName = last;
this.name = `${this.firstName} ${this.lastName}`;
}

export default NameMe;
47 changes: 47 additions & 0 deletions kata/8 kyu/this-is-a-problem/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# ["this" is a problem ](https://www.codewars.com/kata/547c71fdc5b2b38db1000098)

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

```javascript
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
```

```java
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


```

---

## Tags

- Fundamentals
- Language Features
- Object-oriented Programming