-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.js
74 lines (68 loc) · 2.06 KB
/
classes.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
class Player {
constructor(name, hitsPerMinute) {
this.name = name;
this.hitsPerMinute = hitsPerMinute;
this.balloonCount = 100;
}
status() {
console.log(`Player: ${this.name} -- Balllons Left: ${this.balloonCount}`)
}
}
const balloonAttack = (player1, player2) => {
if (player1.hitsPerMinute > player2.hitsPerMinute) {
return player1.name;
}else if (player2.hitsPerMinute > player1.hitsPerMinute)
return player2.name
return 'Tie'
}
const p1 = new Player('p1', 5);
const p2 = new Player('p2', 2);
console.log(balloonAttack(p1,p2))
class ShiftCipher {
constructor(shift){
this.shift = shift ;
}
encrypt(string){
let capitalizedString = string.toUpperCase();
let arr = capitalizedString.split('')
let arr2 = arr.map(e=> e.codePointAt(0))
let arr3 = arr2.map(e => {
if (e>= 65 && e <= 90) {
if ( e + this.shift > 90 ) {
e = (e-26) + this.shift
return e
} else {
return e + this.shift
}
} else {
return e
}
})
let arr4 = arr3.map(e => String.fromCharCode(e))
let result = arr4.join('')
return result
}
decrypt(string){
let lowerCaseString = string.toLowerCase();
let ar = lowerCaseString.split('')
let ar2 = ar.map(e=> {
return e.codePointAt(0)})
let ar3 = ar2.map(e => {
if (e>= 97 && e <= 122) {
if ( e - this.shift < 97 ) {
e = (e+26) - this.shift
return e
} else {
return e - this.shift
}
} else {
return e
}
})
let ar4 = ar3.map(e => String.fromCharCode(e))
let resul = ar4.join('')
return resul
}}
const hamza = new ShiftCipher(2);
console.log(hamza.decrypt('K <3 OA RWRRA'))
console.log(hamza.encrypt('hamz3a'))