What is preferable for storing data by key: Map or a regular object? What are the advantages of Map?
Frontend
/* * Implement the times method for a numeric object. * The function should accept a callback and call it * a specified number of times with the current iteration index. */ Number.prototype.times = function (args) { const n = Number(this); } // Examples console.clear(); (3).times(console.log) // 0 // 1 // 2
What do you like and dislike about working at the company?
/** * It is necessary to write a function strjoin, * which joins strings with a separator. */ function strjoin() { // code here } console.log(strjoin('a', 'b', 'c')) // 'a.b.c' console.log(strjoin('a', 'b', 'c', 'd', 'e', 'f')) // 'a-b-c-d-e-f'
There is a tree with squirrels and crows sitting on it. Write a function that finds all the squirrels on the tree and returns their names. Expected result: ['Acorn', 'Sirsalty', 'Macadamia', 'Kernel'].
/* Given three sections of code. Write the output of each console.log. */ var n = 1; function f(n) { n = 3; } f(n); console.log(n); // ?? // ---------------- var obj = { a: 1 }; function f1(o) { o.a = 5; } f1(obj); console.log(obj); // ?? // ---------------- var obj = { a: 1 }; function f2(o) { o = { hello: 1 }; } f2(obj);
How will the asymptotic complexity and memory consumption change in the task camelCase → snake_case if the result is collected by concatenating strings instead of an array of characters?
/* Two sorted lists with user online presence intervals during the day are given. The start of the interval is strictly less than the end. Calculate the intervals when both users were online. Intervals are given in hours, from 0 to 24. */ intersection( [[8, 12], [17, 22]], [[5, 11], [14, 18], [20, 23]] )// [[8, 11], [17, 18], [20, 22]] intersection( [[9, 15], [18, 21]], [[10, 14], [21, 22]] )// [[10, 14]] function intersection(user1, user2) { // your code here }
How long have you been working at the company?
Rewrite the strjoin function without using rest parameters, using the arguments object instead.
Implement the Array.prototype.groupBy method, which returns a structured version of an array of objects, where each key is the result of the passed function call, and the value is an array of elements corresponding to this key.
Why is for...of highlighted in red in TypeScript, but a regular for is not?
// It is necessary to implement the groupBy method, extending the standard array methods. // The method should return a grouped version of the array - an object, // in which each key is the result of executing the passed function fn(arr[i]), // and each value is an array containing all elements of the original array with this key. // code here // Example 1 const array1 = [ { id: 1 }, { id: 1 }, { id: 2 } ]; const fn = (item) => item.id; console.log(array1.groupBy(fn)); // { // 1: [{ id: 1 }, { id : 1 }], // 2: [{ id: 2 }] // } // Example 2 const array2 = [1, 2, 3]; console.log(array2.groupBy(String)); // { // "1": [1], // "2": [2], // "3": [3] // } // Example 3 const array3 = [3.3, 0.5, 1.4]; console.log(array3.groupBy(Math.round)); // { // 3: [3.3], // 1: [0.5, 1.4] // }
In the prototype of the ad sales network, the sale of ad spaces is organized as follows: buyers specify their price in advance, and for each ad space, they respond whether they are willing to buy it or not. It is necessary to implement a function that, before selling an ad space, waits for approval or rejection from buyers with high bid prices, and then sells the ad space to the buyer with the highest bid among those who agreed to buy. The function should return the index of the buyer as quickly as possible.
/** * Implement a function any that works like Promise.any(). * The function takes an array of promises (assumed non-empty) and returns a promise. * If any of the promises resolve (successfully), * then the returned promise resolves with that value. * If multiple promises succeed, * then the returned promise resolves * with the first successful value, regardless of the order in the array. * If all promises reject, * then the returned promise rejects with an AggregateError, * which groups all errors, considering the order of promises. * * AggregateError can be created as follows: * new AggregateError(errors, 'No Promise in any was resolved') */ function any(promises) { // your code here }
Write a sleep function that returns a Promise and completes after a specified number of milliseconds.
Why can't an arrow function be used for groupBy? What will be 'this' in the arrow?
Write a function tries(n) that calls the function aus up to n times: returns the first successful result, and if all attempts fail, throws the last error.
// You are given a string consisting of Latin letters, spaces, and punctuation marks. // A string is called a pangram if it contains each of the 26 letters at least once. Determine if the string is a pangram. const LETTERS = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']; function isPangram(text) { // your code here } console.log(isPangram(`A pangram or holoalphabetic sentence is a using every letter of a given alphabet at least once.`)); // => console.log(isPangram('Waltz, bad nymph, for quick jigs vex.')); // =>
// each value is an array containing all elements of the original array with this key. // code here // Example 1 const array1 = [ { id: 1 }, { id: 1 }, { id: 2 } ]; const fn = (item) => item.id; console.log(array1.groupBy(fn)); // { // 1: [{ id: 1 }, { id: 1 }], // 2: [{ id: 2 }] // } // Example 2 const array2 = [1, 2, 3]; console.log(array2.groupBy(String));