Today's challenge was to create a function that returns a timed out version of another function. Given a number of milliseconds, your function will either complete in time or it will return the string "Timeout Limit Exceeded". There are multiple ways to do this, but in the Leetcode editorial I learned about the very useful Promise.race() function. Promise.race accepts an array of promises, and it returns a promise that will resolve or reject with the first promise in the array to resolve or reject. Using this, the problem can be solved easily with a race between your function with its arguments, and a setTimeout function that rejects after the specified number of milliseconds.
Wednesday, May 17, 2023
Tuesday, May 16, 2023
130: Sleep
Today's problem involved asynchronous programming, and writing a function that sleeps for a given number of milliseconds. Asynchronous programming is one of the major utilities of Javascript that makes it distinct from other languages. I solved this problem using a Promise. The promise accepts a callback function which executes some resolution function after waiting the given number of milliseconds. In this process I gained a stronger understanding of how promises work, along with their callback functions and resolution and rejection methods.
129: Currying
Currying is a process in functional programming that allows you to partially apply a function given some of the arguments. A curried function is called multiple times and accumulates arguments through each call. This can have various implementations such as improving code readability and allowing for multi-step API calls. Today's Leetcode challenge was to create a function that curries another function. This can be accomplished with a recursive solution. The code checks if the number of passed arguments is greater than or equal to the number of arguments for the function. If so, the function is called with all the arguments. If not, the recursive curry function is called again with new arguments accumulated on.
Sunday, May 14, 2023
128: Memoization
Today's JS challenge is about Memoization. They ask you to write a function that accepts another function, and it caches the result of the function calls. If the result has already been computed, we return the cached value without having to run the function again. If no value is stored, we compute the value, store it, and return it. I accomplished this by declaring an object in my outer function to serve as a the cache. We then return a function that accepts an arbitrary number of arguments. I chose to use the JSON.stringify() method to turn the arguments into a usable object key. We check if the key is in our cache, and if so, return its value. Otherwise we compute its value based on the function and arguments, add it to our cache, and return.
Saturday, May 13, 2023
127: JS Challenge 8
Today we had to write a function that takes another function as an argument, and returns a new function that can be called only one and returns undefined on all subsequent calls. This can be accomplished with closure by declaring a boolean variable. The variable is checked, and if it is the first call we return the function and change the boolean, otherwise we return undefined.
Friday, May 12, 2023
126: JS Challenge 7
This challenge was about function composition. Given an array of functions, compose them all together and return a function with all the others nested within. I accomplished this by iterating through the array in reverse, and reassigning my variable to equal to the current function called on itself.
Wednesday, May 10, 2023
125: JS Challenge 6
Today's challenge was to hand-write a function that reduces an array. This function accepts a callback function and applies it to the elements of the array, and combines them into an accumulator variable. I accomplished this by declaring a variable, iterating over the array with a (for, of) loop, and summing their values into the accumulator to return. The built-in array method to achieve this is called Array.reduce. Reduce is a powerful method that can combine the functionality of both filter() and map().
Tuesday, May 9, 2023
124: JS Challenge 5
Today's Leetcode challenge involved filtered an array using a given function. This can be done manually, first by declaring a new array to store your return value. Then you increment through your given array, and if the current value satisfies the function you push it on to the new array and eventually return it. However, it is much more elegant to use the built in Array.filter() method. Here you simply pass in your function, and the handy filter method does the rest.
Monday, May 8, 2023
123: JS Challenge 4
Today's challenge involved transforming an array. Leetcode asked you to write a function that accepts an array and a transform function, and return an array who's elements have all been transformed accordingly. In my opinion the cleanest way to do this is with the Array.map() method. The map method is passed a function, which is applied to each element of the array. The problem can be solved in a single line of code, with "return arr.map(fn)"
Sunday, May 7, 2023
122: JS Challenge 3
Today's Leetcode challenge asked you to make a function that returns an object. This object has three simple methods that increment, decrement, and reset a variable that is passed as an argument when the object is instantiated. I chose to do this by declaring the three functions within the body of the outer function, along with a counter variable initially set to equal the initializer variable. An object is then returned, with its keys defined as the names of the functions and the values set to those functions. Objects in JS can be returned using the curly brace notation.
Saturday, May 6, 2023
121: Javascript Challenge 2
The second problem went deeper into the concept of closures. A function has access to any variable declared within its scope as well as any outer scope, which altogether constitute its lexical environment. By having a function that returns another function (a "factory function") this allows you to encapsulate data and can serve as a sort of state for your function. Function closure can achieve many of the same things as classes, but they are stored differently in memory.
120: JavaScript Challenge 1
LeetCode has began a 30 day JavaScript challenge. Today I solved the first one, which was a trivial problem where you create a function which returns another function which returns Hello World. The problem itself is simple, but it touches on important concepts like closure. When a function is created, it has access to the variables declared around it. This is called a closure, and is similar in concept to scope.
190: Sablier
The CodeHawks platform has an upcoming audit on the Sablier protocol, so I decided to read through the docs and familiarize myself with the...
-
The CodeHawks platform has an upcoming audit on the Sablier protocol, so I decided to read through the docs and familiarize myself with the...
-
The ZKasino project recently exit scammed over 33 million dollars worth of Ethereum. he recent debacle surrounding ZKasino offers valuable...
-
In addition to its native SDK, Hedera also supports smart contracts in a platform compatible with the Ethereum Virtual Machine. This allows ...