With Sari Morninghawk!
Sari walks us through CSS best practices to help you keep your styles neat, tidy, and easy to maintain. If you couldn’t make it, check out the video below and make sure to sign up for our next workshop via Meetup!
Your partners in accelerated digital transformation
With Sari Morninghawk!
Sari walks us through CSS best practices to help you keep your styles neat, tidy, and easy to maintain. If you couldn’t make it, check out the video below and make sure to sign up for our next workshop via Meetup!
Code challenge (in js, obvi).
Write a one-liner that outputs (int) 10 using only the following symbols: “+”, “[” and “]”
Oh, and you have to explain your answer.
Extra credit: Same restrictions, output (string) 20.
Now if you want to play around a little bit and try and figure out the answer – feel free! Make sure to drop your answer or logic in the comments below. Let us know if you want to see more challenges on the blog as well.
But for those of you who want to dive right into it – here is our answer explained!
+[] // 0
Int 0. Int-eresting, it looks like javascript automatically type casts our array into an int.
Now, using some basic cs principles we can have some fun. Let’s try outputting int 1. We know that we can get 0 from +[] so let’s try to increment it
++[] // Invalid left-hand side expression
Well… that sucks. However, we’ve got another trick up our sleeve. We can shove our outputted 0 into an array, access index[0] and then increment that.
Huh?
Just watch, you’ll get it.
[+[]]
That’s our array[0], now let’s get to that precious 0 it’s keeping safe for us.
[+[]][0]
Bingo, but we can do better. We know +[] is zero, so we can access the 0 index using +[] instead
[+[]][+[]]
Bang, we’ve got our 0 again. But we’ve already had a 0, what’s the difference? I’ll tell you. Nothing… except that this 0 won’t throw an invalid left handed expression error when we try to increment it.
++[+[]][+[]]
And there’s our int 1.
Let’s try and get a string 20. Seems pretty easy. We can take our newly created int 1 and increment that to a 2 then just add a string 0. But how do we get a string 0. Simple. We just add two arrays together. That makes sense, right?
[]+[] // empty string
Now, what happens when you add a string and an int? It gets cast as a string.
[++[[++[+[]][+[]]+[]]][+[]]]+[+[]] === "20" // true
Have fun wrapping your head around that one.