Repository navigation
Expand file tree
/
Copy pathbuild_a_Person.js
More file actions
148 lines (112 loc) · 4.2 KB
/
Copy pathbuild_a_Person.js
File metadata and controls
148 lines (112 loc) · 4.2 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// // Create a JavaScript object called person Cont.. JS Fundamentals Part III
var person = { name : "Kelda", distance_traveled : 0,
statusUpdate : function(){
console.log(`${person.name} has a distance traveled of ${person.distance_traveled}`);
return person;
},
say_something : function(spokenWords){
console.log(`${person.name} says: ${spokenWords}`);
return person;
},
walk : function(){
console.log(`${person.name} is walking!`);
person.distance_traveled += 3;
console.log(person.distance_traveled);
return person;
},
run : function(){
console.log(`${person.name} is running!`);
person.distance_traveled += 10;
console.log(person.distance_traveled);
return person;
},
crawl : function(){
console.log(`${person.name} is crawling!`);
person.distance_traveled += 1;
console.log(person.distance_traveled);
return person;
}
}
person.say_something('I am cool').walk().run().crawl().statusUpdate();
var ninja = {
name: 'Mrs. Anderson',
cohort : "MEAN JS",
ninjaConstructor : function(){
var beltRand = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
var beltColor = 'yellow';
if (beltRand === 1) {
beltColor = "Yellow"
}else if (beltRand === 2) {
beltColor = "Fire Olpal"
}else if (beltRand === 3) {
beltColor = "Black Olpal"
}
console.log(`${ninja.name} ${ninja.cohort} ${beltColor}`);
}
}
ninja.ninjaConstructor();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// using "this" and "new"
//
function Person(firstName, lastName, distance_traveled) {
this.firstName = firstName;
this.lastName = lastName;
this.distance_traveled = distance_traveled;
}
var personOne = new Person('Nelly', 'Walters', 0);
var personTwo = new Person('Kelly', 'Larsen', 5);
var personThree = new Person('Kelda', 'Anderson', 20);
Person.prototype.statusUpdate = function() {
console.log(`${this.firstName} ${this.lastName} has a distance traveled of ${this.distance_traveled}`);
return this;
}
Person.prototype.say_something = function(spokenWords) {
console.log(`${this.firstName} says ${spokenWords}`);
return this;
}
Person.prototype.walk = function() {
console.log(`${this.firstName} is walking`);
this.distance_traveled += 3;
console.log(this.distance_traveled);
return this;
}
Person.prototype.run = function() {
console.log(`${this.firstName} is running`);
this.distance_traveled += 10;
console.log(this.distance_traveled);
return this;
}
Person.prototype.crawl = function() {
console.log(`${this.firstName} is crawling`);
this.distance_traveled += 1;
console.log(this.distance_traveled);
return this;
}
personOne.say_something('I am cool').walk().run().crawl().statusUpdate();
personTwo.say_something('I am cooler').walk().run().crawl().statusUpdate();
personThree.say_something('I am cool').walk().run().crawl().statusUpdate();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function ninjaConstructor(name, cohort, beltLevel) {
this.name = name;
this.cohort = cohort;
this.beltLevel = beltLevel;
}
var ninja_first = new ninjaConstructor('Mr. Lee', 'Python', 'yellow-belt')
var ninja_second = new ninjaConstructor('Mrs. Anderson', 'MEAN JS', 'yellow-belt')
console.log(`Our Ninja's are: ${ninja_first.name} & ${ninja_second.name}`)
ninjaConstructor.prototype.levelUp = function(){
var beltRand = Math.floor(Math.random() * (3 - 1 + 1)) + 1;
console.log(beltRand);
var beltColor = 'yellow';
if (beltRand === 1) {
beltColor = "Yellow"
}else if (beltRand === 2) {
beltColor = "Fire Olpal"
}else if (beltRand === 3) {
beltColor = "Black Olpal"
}
console.log(`${this.name}'s new level for ${this.cohort} is ${beltColor}`)
}
ninja_first.levelUp();
ninja_second.levelUp();