-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.ts
88 lines (72 loc) · 2.5 KB
/
functions.ts
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
// The parameters 'x' and 'y' have the type number
let myAdd: (baseValue: number, increment: number) => number = function(x, y) {
return x + y;
};
console.log(myAdd(5, 12));
////////////////////////////////////////////////////////////////////////////////
function buildName(
firstName: string,
lastName?: string,
title: string = 'Mr.',
) {
if (lastName) return title + ' ' + firstName + ' ' + lastName;
else return title + ' ' + firstName;
}
console.log(buildName('Santos'));
console.log(buildName('Santos', 'Jiménez'));
console.log(buildName('Pepa', 'Pérez', 'Mrs.'));
console.log(buildName('Pepa', undefined, 'Mrs.'));
////////////////////////////////////////////////////////////////////////////////
function buildName2(firstName: string, ...restOfName: string[]) {
return firstName + ' ' + restOfName.join(' ');
}
let employeeName = buildName2('Joseph', 'Samuel', 'Lucas', 'MacKinzie');
console.log(employeeName);
///////////////////////////////////////////////////////////////
/*let deck = {
suits: ["hearts", "spades", "clubs", "diamonds"],
cards: Array(52),
createCardPicker: function() {
return function() {
let pickedCard = Math.floor(Math.random() * 52);
let pickedSuit = Math.floor(pickedCard / 13);
return {suit: this.suits[pickedSuit], card: pickedCard % 13};
}
}
}*/
/*let deck = {
suits: ["hearts", "spades", "clubs", "diamonds"],
cards: Array(52),
createCardPicker: function() {
// NOTE: the line below is now an arrow function, allowing us to capture 'this' right here
return () => {
let pickedCard = Math.floor(Math.random() * 52);
let pickedSuit = Math.floor(pickedCard / 13);
return {suit: this.suits[pickedSuit], card: pickedCard % 13};
}
}
}*/
interface Card {
suit: string;
card: number;
}
interface Deck {
suits: string[];
cards: number[];
createCardPicker(this: Deck): () => Card;
}
let deck: Deck = {
suits: ['hearts', 'spades', 'clubs', 'diamonds'],
cards: Array(52),
// NOTE: The function now explicitly specifies that its callee must be of type Deck
createCardPicker: function(this: Deck) {
return () => {
let pickedCard = Math.floor(Math.random() * 52);
let pickedSuit = Math.floor(pickedCard / 13);
return { suit: this.suits[pickedSuit], card: pickedCard % 13 };
};
},
};
let cardPicker = deck.createCardPicker();
let pickedCard = cardPicker();
console.log('card: ' + pickedCard.card + ' of ' + pickedCard.suit);