isPrime()
function, which returns true
or false
, indicating whether the number passed to it is simple. isPrime(0) // false isPrime(1) // false isPrime(17) // true isPrime(10000000000000) // false
factorial()
function, which returns the factorial of the number passed to it. factorial(0) // 1 factorial(1) // 1 factorial(6) // 720
fib()
, which returns the n-th Fibonacci number . fib(0) // 0 fib(1) // 1 fib(10) // 55 fib(20) // 6765
isSorted()
function, which returns true
or false
depending on whether the numeric array passed to it is sorted. isSorted([]) // true isSorted([-Infinity, -5, 0, 3, 9]) // true isSorted([3, 9, -3, 10]) // false
filter()
function. filter([1, 2, 3, 4], n => n < 3) // [1, 2]
reduce()
function. reduce([1, 2, 3, 4], (a, b) => a + b, 0) // 10
reverse()
function that reverses the order of the characters of the string passed to it. Do not use the built-in reverse()
function. reverse('') // '' reverse('abcdef') // 'fedcba'
indexOf()
function for arrays. indexOf([1, 2, 3], 1) // 0 indexOf([1, 2, 3], 4) // -1
isPalindrome()
function, which returns true
or false
depending on whether the string passed to it is a palindrome (the function is case insensitive and there are no spaces in the string). isPalindrome('') // true isPalindrome('abcdcba') // true isPalindrome('abcd') // false isPalindrome('A man a plan a canal Panama') // true
missing()
function that takes an unsorted array of unique numbers (that is, numbers do not repeat in it) from 1 to some number n , and returns a number that is not in the sequence. There may be either one missing number, or they may not be at all. missing([]) // undefined missing([1, 4, 3]) // 2 missing([2, 3, 4]) // 1 missing([5, 1, 4, 2]) // 3 missing([1, 2, 3, 4]) // undefined
sBalanced()
function that takes a string and returns true
or false
, indicating whether the curly braces in the string are balanced. isBalanced('}{') // false isBalanced('{{}') // false isBalanced('{}{}') // true isBalanced('foo { bar { baz } boo }') // true isBalanced('foo { bar { baz }') // false isBalanced('foo { bar } }') // false
fib2()
. It is similar to the function fib()
from the previous group of tasks, but supports numbers up to 50. Hint: use memoization. fib2(0) // 0 fib2(1) // 1 fib2(10) // 55 fib2(50) // 12586269025
isBalanced2()
function. It is similar to the isBalanced()
function from the previous task group, but it supports three types of brackets: curly {}
, square []
, and round ()
. When passing a function to a string with invalid parentheses, the function must return false
. isBalanced2('(foo { bar (baz) [boo] })') // true isBalanced2('foo { bar { baz }') // false isBalanced2('foo { (bar [baz] } )') // false
uniq()
function, which takes an array of numbers and returns the unique numbers found in it. Can a function solve this problem in O (N) time ? uniq([]) // [] uniq([1, 4, 2, 2, 3, 4, 8]) // [1, 4, 2, 3, 8]
intersection()
function, which takes two arrays and returns their intersection. Can you get the function to solve this problem in O (M + N) time , where M and N are the lengths of the arrays? intersection([1, 5, 4, 2], [8, 91, 4, 1, 3]) // [4, 1] intersection([1, 5, 4, 2], [7, 12]) // []
sort()
function sort()
, which sorts a numeric array in O (N Ă— log (N)) time . sort([]) // [] sort([-4, 1, Infinity, 3, 3, 0]) // [-4, 0, 1, 3, 3, Infinity]
includes()
function, which returns true
or false
depending on whether the number passed to it occurs in the sorted array passed to it. Can a function solve this problem in O (log (N)) time ? includes([1, 3, 8, 10], 8) // true includes([1, 3, 8, 8, 15], 15) // true includes([1, 3, 8, 10, 15], 9) // false
assignDeep()
function, which is similar to Object.assign()
, but performs a deep union of objects. In order not to complicate the task, one can proceed from the assumption that objects can contain only numbers and other objects (they cannot contain arrays, strings, and so on). assignDeep({ a: 1 }, {}) // { a: 1 } assignDeep({ a: 1 }, { a: 2 }) // { a: 2 } assignDeep({ a: 1 }, { a: { b: 2 } }) // { a: { b: 2 } } assignDeep({ a: { b: { c: 1 }}}, { a: { b: { d: 2 }}, e: 3 }) // { a: { b: { c: 1, d: 2 }}, e: 3 }
reduceAsync()
function, which is similar to the reduce()
function from a group of simple tasks, but works with functions that return promise objects, each of which must be resolved before proceeding to the next. let a = () => Promise.resolve('a') let b = () => Promise.resolve('b') let c = () => new Promise(resolve => setTimeout(() => resolve('c'), 100)) await reduceAsync([a, b, c], (acc, value) => [...acc, value], []) // ['a', 'b', 'c'] await reduceAsync([a, c, b], (acc, value) => [...acc, value], ['d']) // ['d', 'a', 'c', 'b']
seq()
function using the same approach as when working on the reduceAsync()
function. This function should take an array of functions that return promise objects and resolve them one by one. let a = () => Promise.resolve('a') let b = () => Promise.resolve('b') let c = () => Promise.resolve('c') await seq([a, b, c]) // ['a', 'b', 'c'] await seq([a, c, b]) // ['a', 'c', 'b']
permute()
function, which returns an array of strings containing all permutations of the specified string. permute('') // [] permute('abc') // ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']
debounce()
function. let a = () => console.log('foo') let b = debounce(a, 100) b() b() b() // a()
LinkedList
class without using embedded JavaScript ( []
) arrays. Your LinkedList
should support only 2 methods: add()
and has()
. class LinkedList {...} let list = new LinkedList(1, 2, 3) list.add(4) // undefined list.add(5) // undefined list.has(1) // true list.has(4) // true list.has(6) // false
HashMap
class without using embedded JavaScript objects ( {}
) or the map()
function. You are given a hash()
function that takes a string and returns a certain number. These numbers are mostly unique, but it is also possible that the same numbers correspond to two different lines. function hash (string) { return string .split('') .reduce((a, b) => ((a << 5) + a) + b.charCodeAt(0), 5381) }
HashMap
implementation should support only 2 methods: get()
and set()
. let map = new HashMap map.set('abc', 123) // undefined map.set('foo', 'bar') // undefined map.set('foo', 'baz') // undefined map.get('abc') // 123 map.get('foo') // 'baz' map.get('def') // undefined
BinarySearchTree
class. It must support 4 methods: add()
, has()
, remove()
, and size()
. let tree = new BinarySearchTree tree.add(1, 2, 3, 4) tree.add(5) tree.has(2) // true tree.has(5) // true tree.remove(3) // undefined tree.size() // 4
let tree = new BinaryTree let fn = value => console.log(value) tree.add(1, 2, 3, 4) tree.bfs(fn) // undefined tree.inorder(fn) // undefined tree.preorder(fn) // undefined tree.postorder(fn) // undefined
hey amy
log, but it displays hey arnold
. Why? function greet(person) { if (person == { name: 'amy' }) { return 'hey amy' } else { return 'hey arnold' } } greet({ name: 'amy' })
0, 1, 2, 3
in the specified order, but it does not (One day you will encounter this error. Some people like to ask this question at interviews). for (var i = 0; i < 4; i++) { setTimeout(() => console.log(i), 0) }
doggo
log, but it displays only undefined
. let dog = { name: 'doggo', sayName() { console.log(this.name) } } let sayName = dog.sayName sayName()
bark()
method of a Dog
object causes an error. Why? function Dog(name) { this.name = name } Dog.bark = function() { console.log(this.name + ' says woof') } let fido = new Dog('fido') fido.bark()
isBig()
returns exactly this result? function isBig(thing) { if (thing == 0 || thing == 1 || thing == 2) { return false } return true } isBig(1) // false isBig([2]) // false isBig([3]) // true
Source: https://habr.com/ru/post/334538/
All Articles