diff --git a/kata/8 kyu/this-is-a-problem/index.test.ts b/kata/8 kyu/this-is-a-problem/index.test.ts new file mode 100644 index 00000000..fc7abaf7 --- /dev/null +++ b/kata/8 kyu/this-is-a-problem/index.test.ts @@ -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'); + }); +}); diff --git a/kata/8 kyu/this-is-a-problem/index.ts b/kata/8 kyu/this-is-a-problem/index.ts new file mode 100644 index 00000000..bc278870 --- /dev/null +++ b/kata/8 kyu/this-is-a-problem/index.ts @@ -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; diff --git a/kata/8 kyu/this-is-a-problem/readme.md b/kata/8 kyu/this-is-a-problem/readme.md new file mode 100644 index 00000000..5de1b3dd --- /dev/null +++ b/kata/8 kyu/this-is-a-problem/readme.md @@ -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