One of the problems in the chapter on Arrays and Collections in Modern JavaScript for the Impatient is Implement a function that works exactly like the from function of the Array class. Pay careful attention to missing elements. What happens with objects that have keys whose numeric values are ≥ the length property? With properties that are not index properties? This was a good way to try a few things in JavaScript.
Read MoreDebit or Credit?
I always wondered why when you use a card for a purchase you are asked on the screen “Debit or Credit?” Doesn’t the machine know? Isn’t it encoded in the card? This nifty article by Patrick McKenzie explains that and more about how ATMs work. It’s much more complex than I had thought but makes a lot of sense. Essentially debit and credit use two different networks for payment so the question is really about which network you want to use.
Read MoreFun with Unicode Regex
One of the problems in the chapter on Strings and Regular Expressions in Modern JavaScript for the Impatient is Write a function that, given a string, produces an escaped string delimited by ’ characters. Turn all non-ASCII Unicode into \u{. . .}. Produce escapes \b, \f, \n, \r, \t, \v, ', \. This was a pretty interesting thing to try. Working with Unicode in Regular Expressions is a little weird and it took me a while to figure out what would match non-ASCII Unicode and replace it with a encoding.
Read MoreFind it with class
I recently covered the very basics of regular expression syntax. Now I’ll describe a little bit about how regular expressions work in JavaScript. The simplest method on the RegExp object is test which returns true if the string contains the specified regular expression. /[A-Z]+/.test('Foo') // true The exec method returns an array with the first match or null if there was no match. /[a-z]+.exec('foo bar') // returns Array['foo'] That array also contains the index of where the match was found (0 in this case) and the input string (‘foo bar’).
Read MoreFind it
Regular expressions are a very handy and flexible way of finding string patterns. In a regex, characters are searched as they are entered except for the reserved characters: . * + ? { | ( ) [ \ ^ $ The string foo only matches the string foo. The . matches any single character so that .oo matches poo and foo. Pretty simple. The * means that the preceding character is repeated 0 or more times.
Read MoreDiseconomies of scale
The Register has an interesting article on the underlying economics of big tech. The Amazons and Googles of the world have grown huge by offering “free” products like Alexa and Gmail. As we all know by now, if it’s free, you are the product. Has this model run out of steam at huge scale? These huge free services are expensive to run. The most recent results from Amazon show how difficult it can be to make money on a free service.
Read MoreNumbers
All JavaScript numbers are double-precision and stored in the IEEE 754 floating point standard. Integer literals can be written in a variety of formats including decimal (42), hexadecimal (0x2A), octal (0o52) and binary (0b101010). You can also include underscores (_) when you write long numbers for clarity (1_000_042). To read an integer from a string, the parseInt function can be used. Some examples: const foo = parseInt('42'); const fooHex = parseInt('0x2A', 16); Floats work similarly:
Read MoreGPT says
I don’t know why I bother. I’ve been following advances in AI with interest and it’s getting scary good. A big story recently is about how uncannily good ChatGPT is at writing text that is essentially indistiguishable from what a person might write. So, I tried it by generating some text on a subject like yesterday’s post: what is JavaScript spread syntax? The ChatGPT response? JavaScript spread syntax is a way to expand an array or an object into a list of items or key-value pairs, respectively.
Read MoreMore Arguments
Spread syntax is a way of calling a JavaScript object with any number of arguments. So, you can make function like this: const avg = (first = 0, ...others) { let sum = first; for (const value of others) { sum += value; } return sum / (1 + others.lenght); } This can be called with any number of arguments. One thing to note is that if the argument is an array, the result will be NaN.
Read MoreArguments
Arguments are a mere suggestion to call a function in JavaScript. If you supply more arguments than required, they are simply ignored. If you supply less, the missing ones are set to undefined. To handle the case of fewer arguments than the function specified, you can either check for undefined or provide default arguments. Both of these work: const avg = (x, y) => y === undefined ? x : (x + y) / 2; const avg = (x, y = x) => (x + y) / 2; I prefer the latter but your mileage may vary.
Read More