-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadding-words.js
More file actions
105 lines (99 loc) · 2.4 KB
/
adding-words.js
File metadata and controls
105 lines (99 loc) · 2.4 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
/*
Title:
Adding words - Part I
Description:
This is the first part of this kata series. Second part is here and third part is here
Add two English words together!
Implement a class Arith (struct struct Arith{value : &'static str,} in Rust) such that
//javascript
var k = new Arith("three");
k.add("seven"); //this should return "ten" //c++
Arith* k = new Arith("three");
k->add("seven"); //this should return string "ten"
Input - Will be between zero and ten and will always be in lower case
Output - Word representation of the result of the addition. Should be in lower case
Kata Link:
https://www.codewars.com/kata/adding-words-part-i
Discuss Link:
https://www.codewars.com/kata/adding-words-part-i/discuss
Solutions Link:
https://www.codewars.com/kata/adding-words-part-i/solutions
*/
// Long Solution
/*
class Arith {
constructor(initialWordNumber) {
this.numberToWordDictionary = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
eleven: 11,
twelve: 12,
thirteen: 13,
fourteen: 14,
fifteen: 15,
sixteen: 16,
seventeen: 17,
eighteen: 18,
nineteen: 19,
twenty: 20,
}
this.wordToNumberDictionary = Object.keys(this.numberToWordDictionary)
this.initialNumber = this.getNumberFromWord(initialWordNumber)
}
getNumberFromWord(word) {
return this.numberToWordDictionary[word]
}
getWordFromNumber(number) {
return this.wordToNumberDictionary[number]
}
add(newNumber) {
const sum = this.initialNumber + this.getNumberFromWord(newNumber)
return this.getWordFromNumber(sum)
}
}
*/
// Short Solution
class Arith {
get dictionary() {
return [
'zero',
'one',
'two',
'three',
'four',
'five',
'six',
'seven',
'eight',
'nine',
'ten',
'eleven',
'twelve',
'thirteen',
'fourteen',
'fifteen',
'sixteen',
'seventeen',
'eighteen',
'nineteen',
'twenty',
]
}
constructor(num) {
this.number = this.dictionary.indexOf(num)
}
add(num) {
return this.dictionary[this.number + this.dictionary.indexOf(num)]
}
}
// Function Export
module.exports = Arith