Help understanding some JS notation I saw?

Hi folks,

I’m working on the Roman Numeral calculator and have been pretty stumped about a way to actuall do it algorithmically.

In my own solutions I’m getting pretty close and I’m sure I’ll have a breakthrough soon.

But to seek inspiration I went looking for other people’s solutions and ran across this on the web.

It works, but as hard as I try, I can’t quite figure out what to Google to find out how this actually works!

Array(+digits.join("") + 1).join(“M”)

where digits is an array containing the individual digits of the number input for the challenge.

37 broken into [“3”, “7”] for example.

I finally figured out that the + symbol before “digits” is coercing the result to be a Number rather than a String (at least I THINK that’s what it means), but the rest of it just absolutely mystifies me.

The output (if digits were [3,7] as above) would be 37 capital M’s in a row.

I can see that it works, but the notation makes it very difficult to understand how. How does the second join know to use 37 on each of the members? And by the way, if you don’t use the + modifier this basically just spits out the string 38 and if you use anything other than empty quotes in the first join("") it breaks entirely!

Can anyone either walk me through it, or point out a resource to go figure it out?

I get the feeling this is very, very advanced stuff that I might run into someday, but… well, I’m doing this to learn so I might as well understand it now that I’ve seen it. :slight_smile:


Interesting. Let’s start from the inside and go out. join() combines elements into a string using whichever character is passed to it. Doing this to an array of numbers will give us that number as a string. Note that we’re passing an empty string.

[3,7].join(""); // "37"

The + operator is indeed a shorthand for toNumber(), which gives us a number value from the string.

toNumber([3,7].join("")); //37

Array() is a constructor function that takes either a bunch of elements to make an array from, or a number N to create an array of size N.

var newArray = Array(toNumber([3,7].join("")));
newArray.length; //37

The second join is combining an empty array of size 37, but passing the character “M” to join with. This will create a string with one “M” in between each empty array element. This is effectively like writing “M” once for each element, except that .join() stops at element N - 1, meaning that we will have 36 “M” characters instead of the 37 that we want. To fix this, we add 1 to the array size in the constructor.

var newArray = Array(toNumber([3,7].join("")) + 1);
newArray.length; //38
mArray.length; //37

The best way to figure this stuff out is to just search "MDN <function/property>".



It makes perfect sense now that I understand the Array constructor a bit better.

And after going and reading the entire MDN entry on arrays, I realize that just “mastering” arrays is probably going to take days or weeks rather than the mere hours I expected.

Are arrays really common once you have a database to work with?

I would think objects mapped to something like the output of stored procedures would be more practical/common.

Arrays are all over the place. They pop up in map data, results from API endpoints (like Wikipedia provides), and even when you’re working with the DOM (document.getElementsByClassName() returns an array of elements). Strings are actually arrays of characters. The pixels on your display are represented internally as a two-dimensional array of color/brightness values. When you get into functional, and then reactive, programming, everything is treated as an array. Matrices can be represented with arrays, and that’s how computers are able to perform the complex algebra needed for 3D graphics. Databases do often spit out array values for queries as well. On the one hand, arrays are a simple and primitive datatype. On the other, they are fundamental to everything we do with computers. Like chess, they are easy to learn, but require a lifetime for mastery.



That paragraph belongs in a sticky or as a required quote in everyone’s random quote project.

I for one was getting sick to death of arrays because of all the challenges that required them, but that was because I really thought “Eh, this is just beginner stuff. It’ll be dull, and I may never really use it, but it’s worth learning.”

You have completely changed my understanding.

I wish I could give more than just a like for this because I’m going to print it out and put it next to my monitor. :slight_smile: