Skip to content

Commit b2ac0b5

Browse files
homemade-5 fixed recurcion whit reduce
1 parent 83fac4b commit b2ac0b5

File tree

1 file changed

+8
-15
lines changed
  • js-core/homeworks/homework-5/src

1 file changed

+8
-15
lines changed

js-core/homeworks/homework-5/src/main.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ capMe(['jo', 'nelson', 'jurie'])
122122

123123
function random(arr) {
124124
let lastElem = arr[arr.length - 1];
125-
let firstElem = arr[0];
125+
let firstElem = arr[0];
126126
let step = (lastElem - firstElem)/arr.length;
127127
for(let i = 0;i<arr.length;i++){
128128
let elem = arr[i];
129129
let nextElem = arr[i+1];
130+
if(nextElem){
131+
return lastElem - arr[arr.length -2];
132+
}
130133
if(elem + step != nextElem){
131134
return elem + step
132135
}
@@ -151,22 +154,12 @@ console.log(random([4, 6, 8, 10]));
151154
*/
152155

153156
function openBraces(arr) {
154-
let newArr = [];
155157
if(Array.isArray(arr)){
156-
// console.log(arr)
157-
for(let i=0;i<arr.length;i++){
158-
let elem = arr[i];
159-
if(typeof elem == 'number'){
160-
newArr.push(elem)
161-
} else if(Array.isArray(elem)){
162-
newArr.push(elem);
163-
openBraces(elem);
164-
}
165-
}
166-
return `${newArr}`;
158+
return arr.reduce(function(result, current){
159+
return result.concat(openBraces(current))
160+
}, [])
167161
}
162+
return arr
168163
};
169-
170-
171164
console.log(openBraces([25,10,[10,[15]]]));
172165
console.log(openBraces([[1,2],[3,[4]],5, 10]));

0 commit comments

Comments
 (0)