Skip to content

Commit fd748af

Browse files
author
ashfaq
committed
properties and Chainable codes
1 parent 1f82270 commit fd748af

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

Greetr.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,75 @@
22

33
var Greetr = function (firstName, lastName, language) {
44
return new Greetr.init({ firstName, lastName, language })
5+
};
6+
var supportedLanguages = ['en', 'es'];
7+
var greetings = {
8+
en:'Hello',
9+
es:'Hola'
10+
};
11+
12+
var formalGreetings = {
13+
en:'Greetings',
14+
es:'Saludos'
15+
};
16+
17+
var logMessages = {
18+
en:'LoggedIn',
19+
es:'iniciar sesión'
520
}
621

722
Greetr.prototype = {
23+
fullName: function(){
24+
return `${this.firstName} ${this.lastName}`;
25+
},
26+
validate: function(){
27+
if(!supportedLanguages.includes(this.language)){
28+
throw new Error('unsupported language');
29+
}
30+
},
831

9-
}
32+
greeting: function(){
33+
return `${greetings[this.language]} ${this.firstName} !`;
34+
},
35+
36+
formalGreetings: function(){
37+
return `${formalGreetings[this.language]} ${this.fullName()}`;
38+
},
39+
40+
greet: function(formal){
41+
var msg;
42+
if(formal){
43+
msg = this.formalGreetings()
44+
}else{
45+
msg = this.greeting()
46+
}
47+
if(console){
48+
console.log(msg);
49+
}
50+
return this;
51+
},
52+
53+
log: function(){
54+
if(console){
55+
console.log(`${logMessages[this.language]}: ${this.fullName()}`);
56+
}
57+
return this;
58+
},
59+
60+
setLanguage : function(newlanguage){
61+
this.language = newlanguage;
62+
this.validate();
63+
return this
64+
}
65+
};
1066

1167
Greetr.init = function ({ firstName = '', lastName = '', language = 'en' }) {
1268
var self = this;
1369
self.firstName = firstName;
1470
self.lastName = lastName;
1571
self.language = language;
1672
return self
17-
}
73+
};
1874

1975
Greetr.init.prototype = Greetr.prototype;
2076

app.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
var g = G$('firstname', 'lastname', 'language');
1+
var g = G$('ashfaq', 'ansari', 'en');
22

3-
console.log(g);
3+
g.greet();
4+
5+
// g.setLanguage("SDSD"); // unsupported language
6+
7+
g.greet().greet(); // chanable functions
8+
9+
10+
g.setLanguage('es');
11+
12+
g.greet().log();

0 commit comments

Comments
 (0)