Skip to content

Commit c015b53

Browse files
committed
solved areLeavesEqual problem
1 parent 718ae63 commit c015b53

File tree

2 files changed

+106
-3
lines changed

2 files changed

+106
-3
lines changed

codetests/iFit.js

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
function tripleThreat(a) {
2-
for (const i = 0; i < a.length - 2; i++) {
2+
for (let i = 0; i < a.length - 2; i++) {
33
if ((a[i] + 1 === a[i + 1]) && (a[i + 1] + 1 === a[i + 2])) return 1;
44
}
55
return 0;
66
}
77

8-
console.log(tripleThreat([0, 4, 5, 6, 10, 12])) // 1
8+
// console.log(tripleThreat([0, 4, 5, 6, 10, 12])) // 1
99

1010

1111

@@ -31,4 +31,74 @@ function createPackage(small, big, goal) {
3131
return goal;
3232
}
3333

34-
console.log(createPackage(9, 2, 9)); // 4
34+
// console.log(createPackage(9, 2, 9)); // 4
35+
36+
// check if leaves are equal
37+
38+
const tree = {
39+
a: 1,
40+
b: 1,
41+
c: {
42+
c1: 1,
43+
c2: 2,
44+
c3: 1,
45+
c4: {
46+
c41: 1,
47+
c42: 1
48+
}
49+
},
50+
d: 1
51+
}
52+
53+
const tree2 = {
54+
a: 1,
55+
b: 1,
56+
c: {
57+
c1: 1,
58+
c2: 1,
59+
c3: 1,
60+
c4: {
61+
c41: 1,
62+
c42: 1
63+
}
64+
},
65+
d: 1
66+
}
67+
68+
const tree3 = {
69+
a: 1,
70+
b: 1,
71+
c: {
72+
c1: 1,
73+
c2: 2,
74+
c3: 1,
75+
c4: {
76+
c41: 1,
77+
c42: 2
78+
}
79+
},
80+
d: 1
81+
}
82+
83+
function areLeavesEqual(tree) {
84+
let initialValue = null;
85+
86+
for (const key in tree) {
87+
const branch = tree[key];
88+
if (typeof branch === 'number') {
89+
if (initialValue === null) initialValue = branch;
90+
if (initialValue) {
91+
if (branch !== initialValue) return false;
92+
}
93+
} else if (typeof branch === 'object') {
94+
return areLeavesEqual(branch);
95+
}
96+
}
97+
98+
return true;
99+
}
100+
101+
102+
console.log(areLeavesEqual(tree)); // false
103+
console.log(areLeavesEqual(tree2)); // true
104+
console.log(areLeavesEqual(tree3)); // false

hatchwaysHelp/aecio1-10-2020.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// "getTwoNumber"
2+
// g e t_t w
3+
// 0 1 2
4+
// "get_two_number"
5+
6+
function toSnakeCase(str) {
7+
let newStr = "";
8+
for (let i = 0; i < str.length; i++) {
9+
const currentChar = str[i];
10+
if (i !== str.length - 1) {
11+
const nextChar = str[i + 1];
12+
// if the next character is capitalized => add _ and the capitalized number
13+
// add the character
14+
if (nextChar === nextChar.toUpperCase()) {
15+
newStr += `${currentChar}_`;
16+
} else {
17+
newStr += currentChar.toLowerCase();
18+
}
19+
} else {
20+
newStr += currentChar.toLowerCase();
21+
}
22+
23+
}
24+
return newStr;
25+
}
26+
27+
28+
29+
30+
// {keyA:1, keyB: {keyC:'a', keyD:[]}}
31+
//{key_a:1, key_b: {key_c:'a', key_d:{}}}
32+
33+
console.log(toSnakeCase("getTwoNumbeR"))

0 commit comments

Comments
 (0)