-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctionalProgramming.js
45 lines (40 loc) · 1.52 KB
/
functionalProgramming.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
// build an HTML list of items in an array. You can first enclose each of the items in a li element
const items = ['Green eggs', 'ham', 'Mr & Mrs T Bloody Mary mix'];
const enclose = (tag, contents) => `<${tag}>${contents}</${tag}>`
const listItems=items.filter(i=>i.trim()!=='').map(i=>enclose('li', i))
console.log(listItems)
const list = enclose('ul', listItems
.filter(i=>i.trim()!=='')
.map(i=>enclose('li', i))
.join(''))
console.log(list)
// set timeout executes an action in future after some milliseconds
setTimeout(() => console.log('Goodbye'), 1000)
function sayLater(text, when) {
const task = () => console.log(text) ;// closure, variables(text) declared outside fn, params, block of code
setTimeout(task, when)
}
sayLater('Hi there', 2000) // first sayLater returns, then task executes
//Objects + function values + closures = Object oriented
function createAccount() {
let balance = 0;
return {
deposit: amount => {
balance += amount
},
withdraw: amount => {
if(amount<=balance) {
balance -= amount
}
},
getBalance: ()=>{
return balance;
}
}
}
const harrysAccount = createAccount();
const sallysAccoiunt = createAccount();
sallysAccoiunt.deposit(500)
harrysAccount.withdraw(500)
console.log(sallysAccoiunt.getBalance())
console.log(`Harry account balance: ${harrysAccount.getBalance()}`)