-
Notifications
You must be signed in to change notification settings - Fork 64
/
Baby Magpies.js
50 lines (40 loc) · 1.47 KB
/
Baby Magpies.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
/*
Magpies are my favourite birds
Baby ones even more so...
It is a little known fact^ that the black & white colours of baby magpies differ by at least one place and at most two places from the colours of the mother magpie.
So now you can work out if any two magpies may be related.
...and Quardle oodle ardle wardle doodle the magpies said
Kata Task
Given the colours of two magpies, determine if one is a possible child or grand-child of the other.
Notes
Each pair of birds being compared will have same number of colour areas
B = Black
W = White
Example
Given these three magpies
Magpie 1 BWBWBW
Magpie 2 BWBWBB
Magpie 3 WWWWBB
You can see:
Magpie 2 may be a child of Magpie 1 because there is only one difference
Magpie 3 may be child of Magpie 2 because there are two differences
So Magpie 3 may be a grand-child of Magpie 1
On the other hand, Magpie 3 cannot be a child of Magpie 1 because there are three differences
DM :-)
^ This fact is little known because I just made it up
*/
var child = function(bird1, bird2) {
if (bird1===bird2) return false
return ( getDifference(bird1, bird2) < 3 ? true : false );
}
var grandchild = function(bird1, bird2) {
if (bird1.length===1&&bird2.length===1&&bird2!==bird1) return false
return ( getDifference(bird1, bird2) <=4 ? true : false );
}
function getDifference(bird1,bird2) {
let dif = 0;
for(let i=0; i<bird1.length; i++) {
if(bird1.slice(i,i+1)!==bird2.slice(i,i+1)) dif++;
}
return dif;
}