If the logHello function is written as an arrow function and is called via fn.call(this, ...args) inside runOnce, what will happen to the context this? What needs to be changed in the logHello function for the context to be correctly passed?
Frontend
Write a function to convert a string from camelCase to snake_case. Do not use regular expressions, character codes, or hardcoded character lists; the solution should be universal. Evaluate the time and space complexity.
Why does the log about 1 second appear first, and then about 2 seconds, when calling sleep(2000) and sleep(1000) twice? Explain the operation of the Event Loop, micro-tasks, and macro-tasks.
/* We have a set of tickets of the following form: [ { from: 'London', to: 'Moscow' }, { from: 'NY', to: 'London' }, { from: 'Moscow', to: 'SPb' }, ... ] From these tickets, a single, continuous route can be constructed. There are no loops or repetitions in the route. You need to write a program that returns these same ticket objects in the order of the route. */ function getRoute(tickets = [], startCity) { // your code here } console.clear() console.log(getRoute([ { from: 'London', to: 'Moscow' }, { from: 'NY', to: 'London' }, { from: 'Moscow', to: 'SPb' }, ], 'NY')); /* [ { from: 'NY', to: 'London' }, { from: 'London', to: 'Moscow' }, { from: 'Moscow', to: 'SPb' }, ] */
Knowledge of databases and working with APIs.
/** * The function `asyncAuth(callback)` accepts a callback, * which can receive an error (as the first argument) and data * from the backend (as the second argument). * asyncAuth((error, data) => {}); * * You need to implement the `auth()` function, * which calls `asyncAuth()`, but returns a Promise. * * @returns {Promise} */ function auth() { // asyncAuth((error, data) => {}); } /** * The function `tryAuth()` uses `auth()` and, in case of an error, * makes N additional attempts. * If all attempts fail - return the last error. * * @returns {Promise} */ function tryAuth(n) { }
What else have you written in your professional experience besides TypeScript and JavaScript?
/** * The function 'asyncAuth(callback)' takes a callback, * which can be passed an error (as the first argument) and * data from the backend (as the second argument). * asyncAuth((error, data) => {}); * * You need to implement the 'auth()' function, * which calls 'asyncAuth()' but returns a Promise. * * @returns {Promise} */ function auth() { return new Promise((resolve, reject) => { asyncAuth((error, data) => { if (error) { reject(error) } else { resolve(data) } }); }) } /** * The 'tryAuth()' function uses 'auth()' and, in case of an error, * makes N additional attempts. * If all attempts fail, it returns the last error. * * @returns {Promise} */ function tryAuth(n) { return new Promise((resolve, reject) => { function attempt(attemptsLeft) { auth().catch((error) => { if (n <= 0) { // Attempts are over — throw the last error return Promise.reject(error); } // There are still attempts — call recursively with n - 1 return tryAuth(n - 1); }); } }) }
If we need to consider fractional numbers, what should be added to the 'times' method?
/* Write a polyfill for Array.prototype.some Parameters - callback - a function to check each element, takes three arguments: - element - the current array element being processed. - index (optional) - the index of the current element. - array (optional) - the array being traversed. - thisArg (optional) - the value to use as this when executing callback. Return value true if the check function returns a truthy value for at least one element, otherwise false. */ Array.prototype.some = function (callback, thisArgs) { const array = this; for (let i = 0; i < array.length; i++) { if (!(i in array)) continue; const result = callback.call(thisArgs, array[i], i, array); if (result) return true; } return false; }
Which team do you work in and on which project?
Is it possible to solve the promise summation task without using static Promise methods? How does the execution of promises inside a loop differ from Promise.all — parallel or sequential?
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'].
Implement a decorator callLimit(fn, limit, callback) that limits the number of function calls. It takes: the function to decorate, the maximum number of calls, a callback, called on the last call (optional). The returned function should have a reset method to reset the counter to the initial state.
Determine the order of output to the console and the measured time values in the example with sleep, timers, Promise, and task queues; explain why the actual delay may be longer than the set delay.
What is preferable for storing data by key: Map or a regular object? What are the advantages of Map?
/** * Write an asynchronous function * that will "sleep" for a specified number of milliseconds, * and then complete successfully */ function sleep(duration) { } // Example const startTime = Date.now(); console.log("Start sleeping..."); sleep(2000).then(() => { console.log("Woke up after 2 seconds!"); console.log("Time passed: ", Date.now() - startTime); }); sleep(1000).then(() => { console.log("Woke up after 1 second!"); console.log("Time passed: ", Date.now() - startTime); });
Extend Array.prototype with a method that groups array elements by a key computed by a given function, and returns an object where each key corresponds to an array of elements. Explain what will happen if the key computation function returns a value that is not a string or a number.
Implement a runOnce wrapper function that takes a function and returns a new function. The new function can only be called once, and all subsequent calls return undefined. The wrapped function can take arguments and return a result.
// 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.')); // =>