-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalg20-makeAPerson.js
86 lines (73 loc) · 2.61 KB
/
alg20-makeAPerson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const assert = require('assert');
/*
Source: <https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/make-a-person>
Make a Person
Fill in the object constructor with the following methods below:
getFirstName()
getLastName()
getFullName()
setFirstName(first)
setLastName(last)
setFullName(firstAndLast)
Run the tests to see the expected output for each method. The methods that take an argument must accept only one argument and it has to be a string. These methods must be the only available means of interacting with the object.
*/
const Person = function(firstAndLast) {
// Only change code below this line
// Complete the method below and implement the others similarly
const [first, last] = firstAndLast.split(' ');
let firstName = first;
let lastName = last;
let fullName = firstAndLast;
this.getFirstName = () => firstName;
this.getLastName = () => lastName;
this.getFullName = () => `${firstName} ${lastName}`;
this.setFirstName = (first) => firstName = first;
this.setLastName = (last) => lastName = last;
this.setFullName = (firstAndLast) => {
const [first, last] = firstAndLast.split(' ');
fullName = firstAndLast;
this.setFirstName(first);
this.setLastName(last);
}
};
const bob = new Person('Bob Ross');
assert.strictEqual(Object.keys(bob).length, 6);
assert.strictEqual(bob instanceof Person, true);
assert.strictEqual(bob.firstName, undefined);
assert.strictEqual(bob.lastName, undefined);
assert.strictEqual(bob.getFirstName(), 'Bob');
assert.strictEqual(bob.getLastName(), 'Ross');
assert.strictEqual(bob.getFullName(), 'Bob Ross');
bob.setFirstName("Haskell");
assert.strictEqual(bob.getFullName(), 'Haskell Ross');
bob.setLastName("Curry");
assert.strictEqual(bob.getFullName(), 'Haskell Curry');
bob.setFullName("Haskell Curry");
assert.strictEqual(bob.getFullName(), 'Haskell Curry');
assert.strictEqual(bob.getFirstName(), 'Haskell');
assert.strictEqual(bob.getLastName(), 'Curry');
/*
Get a help > Get a hint <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-make-a-person/16020>
*/
//Solution 1
var Person = function(firstAndLast) {
var fullName = firstAndLast;
this.getFirstName = function() {
return fullName.split(" ")[0];
};
this.getLastName = function() {
return fullName.split(" ")[1];
};
this.getFullName = function() {
return fullName;
};
this.setFirstName = function(name) {
fullName = name + " " + fullName.split(" ")[1];
};
this.setLastName = function(name) {
fullName = fullName.split(" ")[0] + " " + name;
};
this.setFullName = function(name) {
fullName = name;
};
};