Skip to content

Instantly share code, notes, and snippets.

@ArtemSemak
ArtemSemak / test.js
Created October 12, 2021 08:46
refactoring
//Сделайте из него массив, состоящий из квадратов этих чисел.
function square(arr) {
const result = arr.map(it => it * it)
return result
}
//Верните массив, состоящий только из уникальных значений(убрать все дубликаты, число в новом массиве не должно повторяться)
function noDuplicates(arr) {
const str = 'Привет123 как де123456ла 656748привет'
const numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
splittedStr = str.split('')
for (let i = 0; i < str.length; i++) {
if (!(splittedStr[i] in numbers)) [
splittedStr[i] = ' '
]
@ArtemSemak
ArtemSemak / test.js
Created October 11, 2021 11:04
words with length > 4
let str = 'Привет как дела привет'
splittedStr = str.split(' ')
result = splittedStr.filter(it => it.length > 4)
console.log(result.length)
@ArtemSemak
ArtemSemak / test.js
Last active October 11, 2021 11:32
only objects
let arr = [1, 2, [3, 4], 5, {a: 7}]
console.log(arr.filter(it => typeof it === "object").filter(it => Array.isArray(it)))
@ArtemSemak
ArtemSemak / test.js
Created October 11, 2021 10:54
sum of numbers
let arr = [11, 2, 2]
let result = arr.reduce(
(acc, rec) => acc + rec,
0
)
console.log(result)
@ArtemSemak
ArtemSemak / test.js
Created October 11, 2021 10:49
only negative
let arr = [11, -2, 2, -33]
console.log(arr.filter(it => it < 0))
@ArtemSemak
ArtemSemak / test.js
Created October 11, 2021 10:47
are there any negative numbers?
let arr = [11, -2, 2, 3]
let flag = true
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
flag = false
break
}
}
console.log(flag)
@ArtemSemak
ArtemSemak / test.js
Created October 11, 2021 10:34
no duplicates
let arr = [1, 2, 2, 3]
console.log(arr.filter(( it, index ) => arr.indexOf(it) === index))
@ArtemSemak
ArtemSemak / test.js
Created October 11, 2021 10:25
nunbers powed
let arr = [1, 2, 3]
console.log(arr.map(it => it * it))