const flatten = arr => [].concat(...arr.map(n => Array.isArray(n) ? flatten(n) : n));
// или
function flatten(arr) {
const result = [];
for (const n of arr) {
if (n?.constructor === Array) {
[].push.apply(result, flatten(n));
} else {
result.push(n);
}
}
return result;
}
function flatten(arr) {
const result = [];
const stack = [];
for (let i = 0; i < arr.length || stack.length; i++) {
if (i === arr.length) {
[ i, arr ] = stack.pop();
} else if (arr[i] instanceof Array) {
stack.push([ i, arr ]);
[ i, arr ] = [ -1, arr[i] ];
} else {
result.push(arr[i]);
}
}
return result;
}
// или
function flatten([...arr]) {
for (let i = 0; i < arr.length; i++) {
const n = arr[i];
if (n?.[Symbol.iterator] && typeof n !== 'string') {
arr.splice(i--, 1, ...n);
}
}
return arr;
}
const makeCensored = (str, words, replacement = '***') =>
str
.split(' ')
.map(n => words.includes(n) ? replacement : n)
.join(' ');
arr[i] % 0 +`${s}`+ 0
function getSameParity(arr){
const newArr = [];
let s = arr[0] % 2;
for(let i = 0; i < arr.length; i += 1){
if(arr[i] % 2 === s){
newArr.push(arr[i]);
}
}
return newArr;
}
console.log(getSameParity([3, 6, 7, 8])) // [3, 7]
console.log(getSameParity([4, 6, 7, 8])) // [4, 6, 8]
const getSameParity = arr => arr.filter(v => v % 2 === arr[0] % 2)