-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemon-ash.js
51 lines (48 loc) · 1.2 KB
/
pokemon-ash.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
/**
* @param steps String with commands
*/
const catchPokemons = (steps) => {
let coordinateCurrent = { latitude: 0, longitude: 0 }
let route = new Set(['0,0'])
steps = steps.toUpperCase().split('')
// route loop
steps.forEach(step => {
const motion = coordinates(coordinateCurrent, step)
if (Object.keys(motion).length > 0) {
route.add(`${motion.latitude},${motion.longitude}`)
coordinateCurrent = motion;
}
});
return route.size
}
/**
*
* @param {latitude, longitude} coordinate object cordenate
* @param {String} input motion
* @return {coordinate} coordinate object changed
*/
const coordinates = (coordinate, input) => {
const movements = {
'N': function () {
return coordinate.latitude -= 1
},
'S': function () {
return coordinate.latitude += 1
},
'E': function () {
return coordinate.longitude -= 1
},
'O': function () {
return coordinate.longitude += 1
}
}
if (movements[input]) {
movements[input]()
return coordinate
}
return {};
}
module.exports = {
catchPokemons,
coordinates
}