Syntax Video
Debug December | Advent of Typescript | Advent of CSS | Syntax Bytes with CJ | Episode 6
Intro
hello everyone happy holidays welcome to syntax bites the show where I write some code and hang out and have a good time and uh it is currently December so happy holidays and today I'm going to be doing debug December so this are or these are some challenges that are brought to you by Century so it's a series of 24 coding challenges where you get to solve bugs and a new challenge unlocks each day and it's going to be raffled off for swag so anybody that solves challenges has the opportunity to get some cool swag uh from the syntax and centry Shop we've got a lot of cool stuff here we restocked for Black Friday so we have the uh the Web Master tea back in stock we've got the fingerboards um but if you win the giveaway you uh get a code and then you can head over to the centry and uh syntax swag shop and pick anything you'd like for free um and so today I'm going to be doing the first five challenges because it is December 5th and uh
hopefully it'll be a good time now I've heard the first few are pretty easy um but we'll work through it maybe we'll solve it in multiple ways or maybe talk about why we solved it or how we solved it and after that uh if this doesn't take too long I'm going to look into the uh I think it's Advent of typescript it's was created by trash Dev is this it yes if we have any extra time we'll work on this afterwards and this lets you practice your your typescript skills uh with a challenge each day so we got some got some fun stuff planned for you and this is themed so the idea about these challenges is you're trying to help the yeti uh do some holiday shopping or let's read it so it says despite popular belief the yeti does have a family after completing holiday shopping in town the yeti is on his way home to spread some Che okay so we're trying to help him get home a terrible storm has knocked out all the power leaving leaving the yeti completely in the dark with no clues as to which way to go debug the code and restore the light for the Yeti's path if
you're able to help the yeti get home you may received the gift too they are quite generous beings and uh if you haven't heard of these types of things essentially every day A new challenge unlocks and So currently is December 5th so we're going to do the first five and then tomorrow the next one will unlock all the way until December 24th so that's the plan for today welcome in everyone good to see you all um abdan good to see you hello what's up Musa and Mr cloudt from South Africa says let's debug December challenges I'm I'm excited I'm I'm excited to see what they have for us ABD abdan says these are so easy I'm I'm told that they potentially get harder but we'll see what's up Murdoch welcome in and be mind good to see you and thank you to it's not DG for the follow over on Twitch a few days ago but uh let's Jump Right In let's jump right into it oh but I see someone else who is that uh m4x 809 good to see you what's up iito hello hello welcome in all right let's do the first one and I'll say this if you would like to do these yourself head over to debug
december.com and mute the stream don't don't listen to me solve them use your own skills and also potentially learn some things to try and debug these but if you get stuck you can uh come watch and see how I do it and I'm going in completely blind so I haven't done any of these yet you're going to see my my live reactions um but I believe um I don't know if they link it here but they actually do have a let me pull it up they have a Discord in the Sentry Discord there's a whole Forum dedicated to the uh debug December challenges so if you come across any issues you can join the Discord head over to the debug December Channel and then they have a discussion thread for uh for each day so if you if you do need some help they're here for you all right I see a few more people popping up let's say hello to them all and then uh and then we'll get into
it uh what's what's up M who says look smart how do I look smart but I appreciate that thanks thanks for the compliment what's up rast good to see you says love your content appreciate it yeah links in the description uh debug december.com um and then iaito says do you have any plans of doing Advent of code this year I'm going to do it over on the coding Garden channel so I haven't been live in like almost three weeks on the coding Garden Channel but if all goes to plan this weekend I'm going to go live and do the first six Advent of code challenges uh which is something I've done in the past but I've been I've been super busy with family stuff this year uh I used to sit and wait cuz these these challenges unlock at midnight every night and so the past few years I would sit there and like wait till I think it's 10 p.m. my time uh to do the challenge because the earlier you are to do these challenges over on Advent of code the the more points that you get but yeah I haven't done any of them this year uh but I plan to do them over on the the coding Garden
Channel azim says debug de is fire loving The Vibes well thanks azim what's up penov good to see you as well all right all right and it's not DG hello hello all right let's do it day one here we go we're going to see just how easy these are all right uh the yeti is on the way home to family but the lights along the path are all broken due to coating bugs to fix them a series of JavaScript challenges need to be solved the first light won't turn on until two ingredients are correctly combined for the elixir of Eternal snow ensuring both are properly capitalized uh and joined with a Dash Okay so we've got a function Brew potion it takes in two arguments ingredient one ingredient two uh which really the any type I guess we're not really using typescript here that's all right and then we are ensuring that both ingredients are provided so if there is an ingredient one and there is an ingredient two then we throw an error we log them both out and then we need to combine the ingredients with a dash and capitalize them correctly I'm smiling because I already see the issue um all right and we can see some issues right
Debug December
here so unexpected return unhandled the function has returned an unexpected value okay so this is this is classic now um this would have been especially interesting if they didn't already have semicolons in the code because this is like one of the reasons to like not use automatic semicolon insertion but the syntax highlighting in the editor is helping us you may not realize this but if you put a new line after return your function doesn't return anything so the fix likely should just be to put the codes there and then you can see that it lights up um and that should fix it so let's let's try it let's run it we did it day one sweet um but before we move on let's let's review this code could we do it any better um let's see what the chat has to say uh one oh oh pav already did days one through five nice nice yeah um and the thing is I think especially if you're newer to JavaScript this you'll you're definitely going to find some
learnings in here but I I'll say this even not being new to JavaScript I know people that have come across this bug where you like a lot of times especially maybe even like a react app you put the return there and then maybe your your parentheses are on this line and you got some like jsx or something like that that function is going to return undefined because there's nothing on that line there all right uh let's make this code code simpler so there is the chart at method surprisingly I I've so I've taught a lot of people JavaScript and I've reviewed a lot of code people are unaware that you actually can use bracket notation on strings in JavaScript so this is another way to do it we could use bracket notation to grab that first character um and then slice one is removing the first value from the string but there's a few other methods built into JavaScript uh there's like subst and substring that can also uh remove or
like G give you a sub a substring of the string let's let's look up subst I always forget sub is it something like this subst yeah it's this one there's subst and then there oh okay I see it subst is deprecated substring is not okay so don't use this one don't use subst but there is substring which also accepts a single argument just like slice so if you pass in two it's going to start at index two and go from there so that's another thing we could use substring instead
yeah okay it still works cool um and then here's what I'm thinking we've got some code duplication right we are doing the exact same thing twice so what if we wrote a um I guess we could call it like capitalizer and then we could reuse that function so if we have like capitalize function that takes in uh the string and then does this basically take the first argument uppercase it and and then return the rest of the string and so now we'll use input here like this look at that don't forget the semicolons like that and now we'll uh it'll be nice and uh abstract it out capitalize plus Dash plus capitalize of ingredient 2
like that right does it still work nice look at this I'm I'm fixing this code yeah so that's a good that's a good suggestion from m4x is we could use a template literal oh yeah we could use at well at only exists on arrays yeah you're completely right um but that is another built-in method to grab a specific thing so I know that it exists on arrays does it exist on strings because instead of bracket zero or uh Char code at or chart chart at zero um if strings have at we could use that um I don't think they do though oh they do I thought it was only for arrays uh I like I mean I think I I like at it's nice and readable so get the character at zero here's the nice thing about refactoring I only have to change it once now so because I put that code into this function and then I'm calling it here uh I only have to change it once and uh the code should still work sweet
yeah we could do this with the template literal now I I'll I'll backtrack a second you don't have to do this we're technically done with the first challenge um I uh I'm just having some fun with a little bit of uh mixing and matching and code review but yeah we could turn this into a template string like this so pull that in do the dash and then pull this in here and then like so we got ourselves now you could argue on the readability of this I probably whenever I'm concatenating strings I prefer to use plus signs if it's like less than three or four variables because arguably this is like weirder to look at than the than the plus signs um yeah uh Prav says I made it clear by using a variable as well oh yeah like technically instead of calling these in line you could store them in variables and then return return the variables
concatenated for sure and what's up uh SWA good to see you says love your content appreciate that yeah that's the one cool thing about at which cleans up a lot of array access code is if you want the last element of an array you can pass in negative one yeah uh use the spread operator to get the last element oh or no oh so use the spread operator which will give you the first character and the rest that is cool I like this iito we're going to do that we're going to do that what's up rogaev uh who says clar I was watching an old YouTube live stream for a solid 10 minutes before I realized it wasn't the live stream I was looking for um well welcome and good to see you well strings I don't I don't think they're arrays directly they are technically a list of characters um but you can't use array methods on strings unless you convert it into an array um yeah using the Plus or template literal Also may vary on the use case
yeah but check this out if we use destructuring we could grab the first character and then the rest of the characters is rest a reserved word I don't think it is um and destructuring works on strings like this because it's iterable and then our code gets cleaned up here because then we can say take that first characters send it to uppercase and then take the last the rest of the characters and send them to lower case look at that it didn't work rest oh because it's a array yeah that makes sense because uh it's going to put the rest of these into an array we would then need to turn it back into a string like that and then we could call two lowercase yeah so it's not quite as clean it's all right it's all right cool um we could make this more
functional too let's let's do this so what if we had a list of ingredients um like this ingredient one and ingredient two and uh really what we want to check is if any of them are undefined so uh built into arrays there is a su method and also an every method so if some of these ingredients are undefined then we want to throw an error so this gives us the ingredient we could do it like this so now we have the ingredi and for each one if any one of them is undefined this is going to return true and that should still work in the same way or not um oh no we want every we want opposite flip strike that reverse it so we want every one of them to be defined but if every one of them is not defined then we throw an error or not what am I doing wrong here
strings or I could do this if some of them are false there we go you know I I have a hard time with booleans booing Logics but here we go here here's the other thing we could do we could spread the arguments into an array called ingredients like this then we don't need this extra variable cool so that works and then if we want to log them both out um we can just for each over them and then we get the index and then we could do this um but we want to put the index on there so we could do this I + one and then ingredient right here cool that should log it out in the same way and
then we're destroying this code um but check it out what if we returned um each ingredient capitalized so we could map it and pass it into the capitalize function so now we have an array of capitalized ingredients and then we join that whole array together on a dash like that look at us all right I think I'm done with day one but uh there's lots of ways to write JavaScript I I like to to talk about them all use not yeah so if we did every we could do not knot but if we do some we could do a single knot yeah what about passing a Boolean to every that could could work too so uh the Boolean Constructor if you do Boolean like this if it's a falsy value ah this might not work I I I couldn't say this off the top of my head if you pass an empty string into the booing Constructor does it give you false it does and I believe that's not what we want or is
it I don't know maybe it is I'm going to stick with what I have and we're going to keep moving because we got four more days to do a little bit of over engineering exactly Goram that's what we're here to do this is an extremely over engineered solution uh or you could say this is this is senior developer code you could say um yeah what if the array isn't an array what do you mean by that r i maybe you were talking about something I showed earlier yeah am I a manager let's make it more complex um no I I think it's it's fun to talk about all the different ways right cuz like also for those of you that aren't aware of like the sum or the every methods so there's that also I'll I'll talk about this I see in a lot of code reviews uh developers will use map when they're not actually returning anything so like technically we could put a map right here and this would work but if you're not doing anything with the return data you absolutely should use the 4 each method yeah all right next challenge next
challenge all right day two day two all right this is a little bit more code um I potentially won't refactor this but we'll see so a fork in the road presents two paths one leads home and the other leads to a cliff the light post is switched on but no light is being emitted a sneaky bug in the code must be keeping the light from turning on properly the yeti needs help solving the puzzle to avoid tumbling down the mountain all right so we have a method called turn on lights it accepts lights and then we have a list of results I guess the other thing is do we have access to typescript in here will the code still work if I put typescript no okay I put type annotations all right um so lights is a an array we're going to iterate over them and then for each one uh if the light is on continue on to the next one but if the light is off and then there are other lights that
depend on it we're going to find its dependencies make sure that all of its dependent all of the lights that are sorry all the lights that it dependent on are on as well and then um if all of its lights are on then you can turn it on and then we do it we do it again so we're making sure that every light can be turned on and it can only be turned on if its dependent lights are turned on okay from there the current light is going to be the last one that we looked at before using using continue okay I'm going to rewrite this I don't like using continue inside of inside of for continue continue State statements make it really hard to read for Loops uh because you have to think about okay well this code isn't going to execute it's going to go here and then if this code didn't execute or
uh do you even do you even need this maybe you don't even need this line of code I don't know let's let's solve it first then we'll potentially code review it uh um and then we add to the results so we say the specific light got turned on
okay cool okay so this code isn't working that I think that's the main issue um I think we do see an error here that says assignment to a constant variable and we can look at all of our const so we have current light and we're not reassigning it after that line and then we have dependencies and then can turn on ah here's another one that I missed this is probably the fix you you cannot uh you cannot reassign to a constant variable though here's the thing thing it is technically block scoped and then the on each iteration it's a new block but that that's that's pla that's actually the um um that should be the bug does that fix it yeah there we go that fixed it but we're gonna I'm
I'm I bet I the person who wrote this code is probably hating me right now I'm I'm we're going to rewrite this we're gonna we're going to see some other ways to write this I'm going to do my best to try and remove these continue statements like maybe we could do it I guess we we could even make this like a full-on reduce we could turn this this Loop into like a single reduce maybe that turns all the lights on I don't know how's everybody feeling uh and Goram says uh I pronounced your name almost exactly nice nice why are we using an asnc function here um because it doesn't hurt I'm guessing I'm guessing the code that calls this is awaiting it let's see oh no it still works without it that's that's actually interesting that it still works without it because with the async technically it would be returning a promise with results on it yeah uh Mark hugin says you missed the same const I did yeah you saw me read through the code I was like okay we're
starting here and then starting here it's like where else is the const inside that for Loop there yeah that'll do it it does hurt there's no wait yeah uh if the lights rely on one another you could give uh it could give you infinite Rec cursion yeah if there's a circular dependency well I don't think I don't think it would be infinite cuz we do have this outer loop here and we're not really calling any functions here we could run into some like slow code because this is this is this is INS squared um complexity here because we're doing a filter on all the lights and then uh includes well not N squared it's like n time M because includes still has to search the nested array for each light yeah sorry I keep missing your messages on Twitch uh Roga um everyone seems to be over on YouTube um oh yeah Roga found
it yeah okay do I really want to rewrite this and then rogo with the LOL over here on Twitch they're in both spots that's okay you can watch wherever wherever is comfortable um all right if the light is already on continue here's what we here's what I would do at least for right now if the light is not on do this code we're not we're not going to use the continue statement okay broken okay um I guess technically
wait where how I'm I'm I guess I'm missing some control flow here so if the light is on continue means skip the rest of this
dependencies then we're going to check to see if all of its dependencies are on but if they're not then we're not going to turn it
if the current light is not on and wait wait I guess I could do this here if the current light has dependencies and oh no but then we're trying to find the dependencies there
okay we have some suggestions uh Markus saying flip the last if and move the next two lines into the block okay um does code does continue pass outside an if scope it does that that's honestly why I don't like continue because you could be in a deeply nested block inside the for Loop and if there's a continue statement there it basically goes to the top of the for Loop and starts executing from there which which is a little bit confusing um can we find the dependencies earlier then check if the light is on possibly but let's let's see if so if we flip this if can turn on uh turn it
on why is it why is this so confusing also uh what does it mean unexpected return okay does this code this code still works this code still works
um let's do it like this can turn on equals uh true and then we'll reassign it here and then uh if can turn on then we'll turn it on isn't that the same thing yeah I guess the issue I guess it's the same thing except we had to handle the case where a light doesn't have any dependencies that's that's the issue okay now arguably is this code easier to read there's more nesting right it like it we have to go like this but there's less jumping around so I prefer I I think I would prefer it but now that we've got it working we could potentially refactor it a little bit more um
we could also turn this into a map so a lot of times if you see yourself writing a for Loop and then pushing results into some array you typically can turn it into a map but I guess in this case the results array is only going to be the length of lights that are not on it's not the full length so a map wouldn't make sense you would probably turn it into a reduce yeah okay maybe we can break the dependent um break the dependencies immediately prevent the unnecessary Loop for best case yeah that will not trigger if a light doesn't have a dependency yeah that was my issue is um if there's no dependency we still potentially want to turn it on yeah um let's turn this into a reduce because reduce is is uh is fun is fun okay we are going to return lights.
reduce uh this gives you first the accumulator in this case our accumulator is the array that was outside here so our accumulator is results we're going to get then each individual light and the code below it calls it current light so we're going to call it current light here okay and then we can specify an initial value for our reducer so our reducer starts off as an empty array so when you're converting a for Loop to ouru basically take the thing on the outside put the variable name here as the accumulator and then put the assignment here as the initial value then we can take all of the codes that was inside the for Loop and put it inside the reduce um like
this and basically on each iteration of the reduce we always return results so that the next iteration can use it so all the way at the bottom here we return results and basically if if we turned the light on then we would have pushed it into that array um so we can get rid of that we can get rid of this and look at us we've turned it into a reduce that's going to work on the first try yes yes okay um is there anything else we can do here I guess can we potentially like flip these if statements
I guess now we can use returns instead because we're inside of a reduce we're not inside of a for Loop we can do early return which would prevent us from like nested Loops so um if current light is on return the results okay this is the way this is the way now we have one less nested Loop
okay it's still working look at this is going to get nice this is going to get nice and small okay um return early return often says says Ryan uh isn't it better to just use a map well the issue with a map is the map would be the exact same length as the input array of lights but our result array is only the length of the number of lights that we turned on which is why I'm which is why I'm using a um a reduce here so because you can see in this case if if all the lights are on our results array is going to be empty and that's okay um we have a suggestion from noname who says maybe we can combine the dependencies and can turn on in the same Loop
now we could do this um so if we can turn it on then then turn it on also I'm getting rid of the comments basically I'm making this code really hard to read and making it so that only 10x developers can can use it or refactor it uh cannot read properties of undefined reading includes that's okay um we'll do a little go to
this yeah all right now I think it would not have made sense to for for the creators of debug December to write this code this way because uh it would have been a lot harder for people to follow the code um and then we can just do this if this then
that this code is horrible don't ever write code like this I mean you you can't here's the thing the thing the thing about a reduce is if you have anybody on your team that is newer or like can't under or like doesn't know them as well they're going to spend five times trying to understand what this code even does and then the other thing is like we now have um uh we now have uh like these nested nested Loops here that are kind of just like in line uh what you have to think about is uh cognitive load right so when someone comes across this piece of code how long does it take them to understand what this code is actually doing and I would argue that it would take someone it would take the majority of people longer to figure out what this code is doing versus what the code was doing before I decided to refactor it um we can improve it further by making it less readable yeah is uh it's better to use
see um the nice thing about every is uh that actually gives you it will be potentially quicker if one of them is not on so with filter filter is always going to run across every single thing in the array so whatever the length of the array is filter is going to run this function for every single one every is kind of like an early return so if the first one that it comes across returns false it's not even going to check the rest of them so that's a nice little optimization to use every same thing with with Su yeah cool but here's the thing if we would have written the code this way we wouldn't have had the bug of a const I don't have any Con in my code right here all right let's let's move on to the next one day three here we come all right um upon approaching a snowy hill the yeti discovers a playful snowball contest about to begin to light the lantern to start the game sorting a pile
of snowballs by size using some code is necessary however running the code results in the Snowballs get get getting mixed up instead of neatly ordered the Sorting function isn't working as expected and a quick fix is needed to keep the fun going all right so we have a function called sort snowballs it takes in the sizes and then sorts them now I know what the issue is do you know what the issue is I know what the issue is um the sort method if you don't pass in a comparison function it's going to do the default comparison um and I guess I would be curious if we do console logs do we get to see we haven't even done this yet do we get to see what um yeah we we can see what's inside yeah and the issue that we're we're running into here is you can see that it's actually using um string comparison by default or like string local comparison because when you
compare the string one and the string or if you compare the string 1 zero and the string two one zero comes first because it starts with one if if these things were strings but to fix this code we need to sort these as numbers and uh I'll show you a couple of different ways to do it we can do it the verbose way which is um if a is less than b then we return negative 1 because we are saying that uh a should come before B in the sort order if B is greater than a we return or uh if if a is greater than b we return one because we're saying that a comes after B in the sort order and if they're exactly the same we return zero so if I'm sorting them in the right direction which I always get wrong this will work yeah this works uh the other way to do it is is so this sorts from least to greatest but if you want to sort from greatest to
least you would just Flip Flip these two so this is the long way to do it but because we are dealing with numbers both A and B are numbers we can actually just subtract them we can just say uh a minus B because that's either going to return a negative number if B is greater than a zero if a and b are the same or a positive number if a is greater than b um and the comparison function here doesn't care if it's negative -100 or negative 500 if it's just a negative number then it says it'll come before it in the sort order and if it's a positive number it doesn't matter what kind of posit positive number it comes after it in this I keep hitting my microphone sorry on me fix this comes after in the sort order and so this should do it too yeah and so if you've ever come across code like this you're like what is happening that's what's happening negative positive or or zero yeah um yeah I guess I missed some some
chats from earlier oh I didn't see this return on 16 and get rid of the return on 19 maybe that's when I moved the result array up earlier yeah U Ryan says I see there's a DD just debug December URL is there not a URL for the 11 months of the year uh this is a play on Advent calendars so uh Advent of code is a really popular one that does this there's also Advent of typescript there's Advent of rust the idea is in the month of December you solve one challenge every single day which is similar to Advent calendars where you get um a little gift or surprise uh every day this is specifically a Christmas thing if you celebrate Christmas you might have seen these things around there's like physical ones like this where every day throughout the month of December you get a small little gift and then that counts down the days until December that's basically what this this this is this is
a play on that yeah yeah so it's not DG says the default comparisons are lexographic not as numbers so it's comparing them as strings yeah a minus B is just better to me I I like it but I would also argue that if a developer comes across this and doesn't know what this is doing it could be confusing um I will say if you look at the mdn page for a ray sort it shows you the the verbose way and what exactly is happening here um like this so if a is less than b by some ordering Criterion and in in the case of numbers we can just use uh less than return negative 1 if a is greater than b then by some ordering criteria return one otherwise return zero um a pretty common thing you see happening when uh you're sorting arrays of strings is to use the local compare because it automatically returns
um um okay so we want to sort we want to sort this array the thing is by default um that will sort it but if you're dealing with strings that are potentially in different languages you want to use local compare which is built into Strings because the string comparison may not always be the same especially like with accents and stuff like that so uh typically this is what you want to do instead of if you're sorting an array of strings and you have an app that is in multiple languages instead of using the built-in sort you could do a a do local compare B for our purposes we don't have any accents uh this is going to work in exactly the same way uh but if you were dealing with other languages this makes it a whole lot easier to to do that yeah yeah local compare yeah
yeah um what about coordinates sorting X and Y yeah then at that point you're just going to have some some custom logic and I guess it has it depends on your sorting Criterion right if you're sorting them by X's these could actually be objects and then you can compare like a.x minus b.x and so that is going to sort them along the x-axis but you could also sort them along the y- Axis or you could sort them based on a distance function right so let's say you had a place that you were trying to go to and then you had a list of xy's you could calculate the difference calculate the distance between your target for A and B and then return the one has that has the shorter distance yeah there's there's a lot of stuff you can do with that custom compare method for sure all right day four here we come every winter yetis comp compete in the annual Ice Crystal collection challenge racing together crystals in different colors each Crystal must be
carefully placed in its matching spot in the collection box the yeti with the most valid Crystals at the end wins the frosty trophy all right it says count the ice crystals we get all of the user IDs and then for each user ID we are creating a new array with map and then there's a function not available to us that is get ice crystals for user so it's calling this we don't know exactly what this does just yet it gets the ice crystals and so now we're going to have an array potentially of arrays whatever this returns we're going to have an array of that and then for each one of those yeah so it's going to be an array of arrays I'm guessing if they're because they're using the length method here and then for each one of those we're getting the total number of crystals and then returning it but again we have another error here assignment to constant variable uh so
this should be a let and that fixes that error um so the input is 1 2 3 the expected is seven the actual is 23 but here's what I'm I'm going to log crystals by user to see what we're actually getting back from that get ice crystals for user
method n white n n n red n n red blue n n no n n yellow no n blue okay maybe we don't account for the NES maybe we have to filter out the NES um so let's just do that let's get a filter we get each individual value and then only return the value if it's not if it's truthy if it has a value oh that that fixes it I want to see the output though um yeah that fixed it basically uh this function was returning null and we needed to ignore it so in this case we got null white null null null red really the length is actually just two cuz it's white and red and then this one is red blue and yellow and we have to get uh ignore all of the nulles so um this is one way to do it we could add a filter right here and that will only return the values that have a truthy value so it's going
to get rid of all the nulles the other thing is we could filter it right here we could say once we've come across them filter out all the NES and then grab the the length of that this this should work in the same way we're just deciding on a different spot to uh to filter yeah and Mark mentioned they they added their filter on line 16 yeah this is another way to do is you filter it right here whenever you're grabbing that length uh could we use a Boolean here as well yeah in this case this would work for a Boolean uh if you do um like this because when you pass nll into the booing Constructor it's going to return false yeah so that that would work too by putting anything um we're basically returning a truthy or a falsy value and then by passing it into the booing Constructor we're explicitly returning true or false yeah exactly and Mark is mentioning because null is a is a falsy value and Dr Mumba thank you for the
follow over there on on Twitch I think there's a hint about that in the text the most valid crystals I am really bad at reading directions so yeah the yeti with the most valid Crystals at the end wins the frosty trophy that would makes sense so we're getting the crystals but the valid ones are the ones that are not null cool all right let's turn this into a reduce so uh for each user uh we're going to grab this we're going to grab the sum we're going to grab uh each user ID we're going to get those we're going to filter out uh the value we're going to add that onto the sum and our sum starts off as zero uh but we do want to grabb the length like this no okay um crystals for user
it oh okay yeah I I did it R think of verse has it I was so close in instead of a map this needs to be a reduce yeah that should do it sweet all right day five okay so this is the this is the last one for today I I will say that one got harder I would if you didn't log out the crystals you wouldn't have seen the nules um that that's that's a good challenge that's definitely a uh will get people to log the values and try to figure stuff out okay um day five here we come the yeti stumbles upon a frozen pond where a puzzle awaits calculate the sum of the heights of the icicles hanging from the trees to light the lantern the sum of the heights of the icicles the code is used to add them up oh they are using a red so even the codee's getting harder here uh the code is used to add them up but nothing happens the lantern stays dim it seems there's a bug in the code
and without a fix the right total won't be reached to light up the next part of the path um and we're we're seeing right now there's a type error cannot create property total on number
one yeah I see the error and we've talked about this we've talked about this what you need to do inside of of a reduce what are they missing first person in the chat gets a cookie gets a cookie what what's what's missing here an an imaginary cookie I won't really send you a cookie what have we learned so far today class exactly what what Mark is saying yeah and at Dev gots it return we're missing the return right because inside of a reduce the accumulator this this argument right here um it it won't be anything unless the previous invocation actually return something yeah uh two things return and oh so there's there's more than one bug okay so first of all the first bug is always return the accumulator yeah but then yeah you're completely right I missed this too the accumulator is always the first argument and then our value is the second argument that's the fix yeah there we go cool all right
let's go the opposite way let's let's un ruce this let's say we have our um result and that is equal to an object like this and then we want to create a for Loop do we have Snippets in here no that iterates over each one
and we want numbers okay and then the number is numbers at I all right and then we say result.
total add the number onto it and then return the result like that okay see I told you if you wouldn't have used a reduce you probably would have wrote the code right the first time because then then you didn't have to worry about the return statement and you didn't have to worry about getting your arguments mixed up uh now we're 5x programmers yeah we're cutting we're cutting our code in half by writing it the long way cool no this was super fun um so yeah the next challenge unlocks in 4 and 1 half hours and if you haven't done these yet definitely check it out so go to debug december.com uh every day a oh look at this they the it lights the path as we go up every day A new challenge is unlocked and uh on certain days they're doing giveaways so if you look in the about they're doing giveaways on the sixth the 12th the 18th and and the 24th so uh if my understanding is correct they're going to do the giveaway at the end of
tomorrow so you have until the end of tomorrow's challenge uh to complete these challenges that'll in enter you to win the giveaway and the winner will get a code to get anything they want for free in the syntax swag shop yeah so uh Ryan is asking what will I win anything in this in the swag shop the the webm te's got reprinted so um jobs kind no um you can you can just win from the swag shop but the the Web Master te did get reprinted um so that's there we also have the Blazing fast te there's the uh the fingerboards um and if I read this right there is a there's a frequently asked questions here if I read this right I think it's good for anything in the store so if you want to maximize your value you could get a syntax skateboard if if you wanted to
um chat GPT Pro just dropped $200 what who would pay that I wouldn't pay that maybe some people would pay that um there I do believe there's a spot on here that um tells us about it yeah and then here on the stats you can see the number of people that have completed each day so uh only three 300 people have completed uh day five so get on it um yeah so you're going to be provided a unique code and each code is valid for a single item and cannot be reused to claim additional prizes yeah so you can maximize your value and get yourself a $50 uh syntax skateboard this but yours will yours will be brand new it won't have all these scratches on it like
mine the Tumblr Oh yeah we have a um it's a it's actually a can cooler so the the top unscrews and then um keeps your cans cool my wife and I both have one and we just leave it by the couch so anytime we're drinking our sodas by the couch they always stay cool yeah go go for a hat yeah the I have uh this one I will I will say that the hats are big if you haven't seen them in person um you could probably uh tell by looking at the the models um like this model has a slightly smaller head it is a a uh it's kind of big um I think like even this one I could rock that yeah it's still a little big for me I don't wear I don't wear a lot of wear a lot of hats though yeah yeah I think we'll do so how long have we been live yeah we've been live for roughly an hour I think I'll do some typescript challenges now too I think
good I'll probably probably take a quick break and then uh then we'll do some more um did I miss any other chats yeah Advent of TS yeah so we'll do that okay so again debug december.com debug december.com debug december.com go check it out you have until um you have 24 plus 5 hours to to complete some challenges to be inter for the raffle that's going to happen after tomorrow um and then here's all the frequently asked questions if you have more questions about about debug December all right I'm going to take a quick two-minute break and then when I come back we're going to do Advent of typescript so this was created by um trash Dev and similarly it's a challenge every day but these are typescript challenges and I'm also going into these blind so I haven't even used this website
yet um but yeah so you can see each day leading up to Christmas day there's a type Challenge and here we go so yeah here's the description here's the codes and we're going to have to write some typescript to fix it so I'll be back in two minutes we'll do this and uh if you've been sitting down all day just stand up for a second maybe drink some water I'll see you very very soon
all right everyone welcome back now we are going to do Advent of typescript so uh just like debug December and Advent of code these are challenges that are released every day these specifically have to do with typescript so we're going to practice using typescript utilities and working probably on generics I don't know how difficult it's going to be at the beginning I imagine at some point we're going to be working with generics and stuff like that and there are five challenges so far so I'm going to try to do the first five uh I'm going into it blind I have not seen any of these yet so we'll see how good my uh how good my my typescript is what is Vector embedding um it's a way of encoding text so that it creates relationships between the text I think I don't know how accurate I am on that simple explanation but the idea is um if you let's say you have a bunch of
text so take for example over on coding Garden Prav says I'm correct so take for over on coding Garden we're working on a project where I take all the transcripts of every live stream I've ever done and I want to turn it into something that's searchable and then maybe also something where an AI can answer questions in relation to what I talked about on stream but basically what we could do is uh create Vector embeddings for every single one of the transcripts and then whenever someone types a question we can query a database that supports vectors and it can return the the related documents uh really quickly because it will basically encode the question and then also be able to look at the encodings of all of the transcripts and very easily correlate which of those transcripts correspond or are similar to the question that's being asked and then uh those could get those documents could then be embedded in the prompt for like an
AI um yeah yeah exactly Ryan so that's what it is it would be cool if AI could do that in real time while you stream well the the idea with Vector embeddings is you set them up ahead of time and I so yeah I am working I'm building that over on the coding Garden Channel um it's the most recent live streams that I did over there if you click on live yeah so it's this building with Dino V2 these two streams have nothing to do with Vector embeddings these were just me writing the code to talk to the YouTube API get every single uh well video and live stream I've ever done and then get the transcripts for every single one so we've written that code we have all the transcripts the next step is to figure out how to make it searchable and I haven't decided yet but the thing about uh Vector embeddings is there unique to each of the different AI models and you typically have to call an API for it so if we're going to be using open AI they have an API endpoint which will create
embeddings that their models can use they and then for all the other a AI models that they have similar API endpoints and so it kind of feels like vendor lock in like the moment I choose in AI we're creating all of these embeddings and paying to create all of those embeddings so I haven't decided yet the other thing is we probably could do like plain text search or feed it into uh a search engine like Apache Lucine there are a few other search products out there uh but Lucine is one of them this B Lucine allows you to like index documents and then it provides an API to allow you to search across those documents so I might do something like this first so that way I'm not locked into a particular AI I was also looking into using olama because that's like self- hostable or like it can run locally yeah uh right now we're not using any llms at all yeah uh it makes sense if AI was listening
summarizing and answering questions oh like during the live stream that is a whole other thing yeah so if we could have a like a transcript of what's happened in the live stream so far and then someone asked a question in the chat and then the AI answerers because it was listening the whole time yeah that would be pretty cool I mean we could probably create like a product out of that people could have that bot just kind of like sitting in their chat yeah YouTube's done that um I know they've done it for videos I think if you have YouTube premium you can actually there's I I have it on my mobile app uh if you go on a video there's a section where you can actually talk to the chat and I think it uses the transcripts yeah oh Advent of CSS o we should look at that too I have not heard of
that I'm getting sidetracked by the way we need we're going to work on the typescript one look at this um oh you receive an email okay what if I sign up late will you send me an email with all the links oh nice yeah yeah uh Amy from what was the podcast that they do a compressed yeah oh it's after wait not that okay we'll see how hard these typescript challenges are but if I get them if I get through them pretty quickly this would be fun to do as well all right 10% all right Ryan you get you get 10% for having the idea what's up Chad welcome in all right let's do Advent of typescript so Advent of ts.com you get a new typescript challenge every day it was created by trash Dev let's go day one the reindeer have had enough Santa's reindeer Comet and Dasher have a secret meeting with Bernard the head of the elves that's from uh the Santa Claus one
Advent of TypeScript
one of my favorite movies with Tim Allen uh uh there's a an elf named Bernard okay uh comment it's the economy stupid uh Dasher comment I'm read a book or something your productivity is far outpaced North Pole's inflation like two decades ago our wage hasn't changed since 2009 and if you take inflation into account we make half of what we made then we could have the best economy of the last 500 years and it still wouldn't change anything Bernard says boys boys calm down we'll fix this we can fix this comment says oh I'm sorry Mr Bernard the the kesian maybe you can explain why why we're still getting paid in candy canes like it's 1947 apparently we're dealing with reindeer here that don't even know the difference between any and unknown boys boys calm down you're not the only ones with problems I've got 600 elves in the workshop huffing reindeer wax and unionizing over bathroom bricks you just need to come up with a number for our demand what kind of number at this point any number will do but we
need to start somewhere uh why not go all in write down Infinity care and tell him it's non-negotiable what's he going to do hire reindeer scabs you know how hard it is to get stand out of focus these days half the time he's passed out in his Workshop muttering about crypto and Mrs claw's book club drama any number will be fine as a starting point okay um here's the thing about a lot of these Advent challenges there's a lot of story involved and then figuring out what you actually need to do ultimately a lot of times is a bit easier but we'll see this this I think this is going to give us an intro to the whole thing so yeah uh what you just just read above is part of the story for this year's challenge this year's picks up from last year's stories if it interests you you can quickly read all of last year's stories here in these stories we get to know Santa in a much more personal way than you might be used to from picture books and cartoons the story will often give you a lot of context about the changes you need to make to the typescript code in today's example we see that we're supposed to modify demand the typescript type to be
a number uh truth be told though you don't really need to read the stories if you like you can jump right into reading the test to see what the behavior of your type is supposed to do we'll usually start off with something but I'll always be incomplete in some way take a look at the test and make a change uh to make the test pass the hints if you need a hint sometimes you'll see something like this okay so the test code is down below we have expect and equal we have our actual type which is called demand and then we have our expected type which is number so yeah this is a pretty simple one to get us started off and then we're expecting that uh the actual type equals the expected type and so right now it has the type of unknown but if we were to change this to number now that fixes the code so that's literally all it was asking for okay I need to log in really quick and then we can
submit all right we're back submit good job nice nice day one complete that was pretty easy I imagine they're going to get harder yeah um Advent of typescript seems easier than 2023 I went back and tried 2023 and stopped after five challenges I have never completed an advent of code so Advent of code is probably the one that inspire fired a lot of these Advent of code gets really hard and last year I think it would might have been the first or second challenge um it was so hard that uh I posted my solution on YouTube and it was like one of my most viewed videos of the year just because so many people were trying to see um how to solve that that particular problem not and again we're talking about Advent of code he look at this one I had I had
18,000 views on the day one solution um I but yeah I've never completed a full year of Advent of code I think making them easier is good because you get a a wider audience that can actually try these out and it's a hard balance to strike right because you don't want people to get so frustrated that they just quit but you want it to be hard enough to be a challenge it's it's a it's definitely a hard hard balance to strike yeah will will will Ferell turn down 29 million to do an elf sequel um elf is also one of my favorite Christmas mov my my wife and I really like Christmas we watch a lot of Christmas movies elf is at the top of the list the Santa Claus is at the top of the list yeah all right day two here we go I'm gonna I'm going to skip the story I I can I can only read so much uh yesterday Comet and D Dasher just gave number as their demand to Santa That's not nearly specific enough we're going to need something more much more specific look at the test so we uh to
see what we can change to make them all pass um ched said there's a there's a good documentary on the making of Elf I'll have to check it out Santa oh I heard I haven't looked up how to do this but I heard that if you do Advent of code with Dino you get stickers I'm definitely going to do it with Dino okay um the expected type is literally 990,000 is this one just as easy or 9 900,000 yeah this this is a newer feature of JavaScript you may not have seen you can put underscores anywhere in a number and it will just make them it'll just ignore them uh typically it's for making numbers easier to to read but you can also make numbers harder to read um and this this this uh yeah this still works um but yeah that that was add I forget
when that was added was it es 2015 but usually you use it to denote like every three numbers so you can denote like thousands and millions and stuff like that yeah it's syntax syntactic sugar exactly or syntax sugar because we are we are here on syntax yeah uh okay next one day three all right now we got lots of codes the function Bernard created works for numbers but it also accepts other data types like strings booleans arrays and objects that's not quite what we want typescript can help us here all right so survival ratio is supposed to just take in a number this one is also very easy because we can type this to say that this function only accept number accepts numbers like this and then anytime you pass in anything else it will give an
error cool all right let's keep moving day four uh when Santa uses this function sometimes he'll want to pass a number like 2009 and if he wants a specific quarter he'll pass 2009 Q2 our function needs to be able to handle both of these data types not just numbers yeah uh Kim pffu says all of these have been easier than 2023 is day one we'll have to if it I mean I guess I'm already on day three day four we might have to try day one from 2020 23 um yeah okay a syntax Sugar Bar jar okay you're full of good ideas today Ryan I'll relay it to the team um because it's literally syntax sugar like what if we sold like a
ceramic jar with the syntax logo on it yeah can you please over engineer and test type of arguments um I'm not good I'm not as good as at typescript to be able to do things like that the thing is um in this case we just need to use a type Union so this gets a little bit more advanced so you can write a function that takes in one type like this and now it has to be a number but if they pass in a string we're going to get a type error and so what we can say is you can pass in a number or a string and the the pipe Here is known as a type Union so input can be a string or can be a number now this is just uh type annotations to say that we accept these two things and so now the code doesn't break down here the right the the code that we write needs to take that into account and in this case it does you can see that it's checking well if it's a string uh then use it as is but if it's
not a string then um or sorry uh but if it's not a string we want to just assume that it's q1 for whatever year was passed in um so the type Union here allows us to have arguments that are of multiple types
yeah I had to put you at 75 speed when following the hono tutorial like being taught to code by someone after a bottle of vodka I I I've done this before like I've listened to my own videos at like super slow speed um and I it yeah I think you when you do this to most people it sounds like they're drunk when when they're talking really slow yeah drunk CJ teaching you about typescript um okay Ken says they didn't get tougher until day four okay I'll probably I think we're already on day five or uh right yeah we're already on day five so we'll probably try the first few of 2023 as well Santa is hard to please that much is for sure to appease him we want a function that will accept any input and return a value that is has exactly the same type
unknown and it can be anything we could just use the any type right no okay um cuz it because then the return type would be the any type and not the exact type okay we have to use generics okay so here here we go our our one little lesson on generics here so in all of the case in all of the examples here we're passing in a number and we're expecting that the type is going to be um uh a number we're passing in true we're going to expect that the type is a booing we're passing in a an array of numbers we're going to expect that the type is an array of numbers and so what we need to do is declare a type parameter and typically that is with but you'll see it as just the letter T and and typically that refers to just some generic type it could be anything this type can be literally anything anything that's passed into this function um and
then uh so that goes there but the other place that you put it is right here to say that whatever gets passed in here we're going to infer that type and then use it as T and then everywhere else in our function that thing will be to T and so that's that should be the solution here I was say this was a pretty big jump from day four to day five I mean this is probably one of the simplest use cases of generics because it gets a lot more complex because right now we're saying the type can be anything but a lot of times you want to have a generic type but you also want to restrict it in some way so you could say well T extends an object that has a name that is a string so T can be anything as long as it has a name property that is a string or a lot of times you see well T extends object uh and now T can be any sort of object thing but in this case we don't want any sort of restriction on the type we're just saying we're accepting in a type and whatever the type of that thing
that is passed in that is what it will be there we go we did it day five sweet all right I'm going to sign up for Advent of CSS just so I can get the email and then uh we'll we'll try to do some of those and then while I'm waiting on the email we'll do a the first of Advent of typescript
2023 what is the most complicated type you ever encountered that you remember I don't know I've seen some crazy stuff these days nothing really surprises me anymore because I do a lot of source code dive like um even just looking at the types inside of hono are pretty interesting specifically because hono has their typed RPC so if you look at hono um they have the uh hono client so basically you can define a backend API you can Define validators with and with Zod that describe the describe the type of data that your functions are going to return or going to receive um and then you can just grab the type of that and then use it in your hono client so you pass that type in and now this is fully typed it it knows about all of the methods that
you created on your specific app and so I dug into a lot of these types to see how are they doing this like literally just just based on this one type now inside of your editor you get code completion for all of this and like you'll get type errors if you pass in the wrong stuff here and those types actually flow through from right here um it's nothing crazy but it is it is pretty interesting stuff to read um what else have I looked at I feel like I was looking at some some types recently what was I doing I think it was just more hono stuff because like I also did the um the open API hono API and I was looking at how they they did all of types yeah this is the exact part that junk CJ was telling me about um Mark says uh that she also does a companion Advent of JavaScript they're the same just in one you do the CSS she
gives you the JavaScript in the other you get the CSS but you don't write the JavaScript I see yeah we'll just do the CSS one because um we're going to do all our JavaScript writing in the debug December and then I mentioned it earlier but this weekend I'm going to do some live streams over on the coding Garden Channel where I work on uh the first uh six or seven days of Advent of code all right let me go check my email
oh this is a lot more involved than I thought it would be um but we we'll do we'll do at least one of them so we can we can Flex all of our skills today we did JavaScript we did typescript now now we'll do some CSS um yeah when hona first came out you were one of the first people to cover it your videos made me like it I built an API with it for my company and now it's in production yeah that's awesome to hear uh is there a way to export or install the hono client into a front-end app not a mono repo also I find hono client much better than the open API client generators I don't know why uh the open API client generators depending they're all based on your type definitions and your your the schema definition of your of your API and if it's not the best then the generated code isn't going to be the best and they all have their own ways of doing it that you may not be used to but yeah I mean the the easiest way not easiest but one way is to literally just uh build your code and then copy the
type um so you can R run the build command for your backend and make sure that you have uh type declarations turned on and so um as long as you are exporting that type so if we look at um how they tell us to do RPC where you need to export the app type build the code you will have a file called uh API .ts and it's just going to have this type inside of it you could literally copy paste that that file into your your front end app it's that's not the ideal way to do it but that is one way to do it basically build the code grab that typescript file and then just use it in your in your backend code when you're inside of a mono repo um that gets much easier because you can actually just import the type from your backend package um they do have a section here on like compiling the type
yeah compile your client including the server app gives you the best performance put the following code in your project yeah so they they even say this you could actually have this piece of code in your backend code and just call it client. TS do the build that's going to generate the the code with all the embedded types and then you can just import that directly inside of your front end code but that's that's the like the roundabout way to do it how would you build a hono app and then say run view or V on the front end can you do that with a full view router setup for the front end yeah you can um I I've seen some videos on it basically it's just the the old way of doing things where you have a decoupled back end and front end and there's multiple ways to deploy it right you could deploy your front end completely statically and then um your your backend could be deployed as like serverless functions or
something like that uh but your frontend view code can still talk to your your hono back in but you also could combine them and deploy them in the same same code base because if you look at hono they have a uh a file yeah serving static files so technically you could build and bundle your front end serve that from like the root and then the rest of your hono app would like your API endpoints might be accessible from SL API there's a way to do it if you look up tutorials on people that have done it with Express the way to do it with hono is going to be very similar what's up poo good to see you yeah and America 2050 I missed your messages hello hello yeah welcome in Ken yeah we're over here on the syntax Channel today do do you do need to specify the return type as T2 it's inferred yeah uh
we're talking about the Advent of TS right now day five it's a good question uh because we are returning route which is of type T the return type inferred is T technically you could be explicit you could say this function returns type T it's exactly the same but in this case without it we're using inferred types like that yeah yeah
oh nice to hear Ken mentioned they're using um hono inside of nextjs that's a pretty I I hear it's a common pattern um in the most recent episode of syntax Scott and West talked about how you typically don't need to do that because you can just use the built-in way of building apis I will say with nextjs their way of building apis does leave a bit to desired so you can get some nice type completion and type inference if you're using the hono client but if you look in the hono docs check out verel they talk about right here how you can write a hono app and then just put it inside of a nextjs application as like a catchall router and so now you have a hono app living inside of a xjs app it's possible it's definitely possible and thanks for the follow fairy Hunter appreciate you yeah could you do a hono app inside of a next project you could but you probably would want to use Nitro instead so Nitro um is what n is built on top of
and it has its own ways of like building API functions so you would probably want to do this instead of pulling in hono Palo says you did the first five days of debug December uh nice from your phone that's great yeah you don't like Nitro that's why you bring in some that's why you want to bring in something else I I haven't spent a lot of time with Nitro um okay let's look at this Advent of CSS so I signed up and um it looks like we're going to be building this from scratch which seems pretty involved but but let's check it out so uh create a photo gallery card similar to what Facebook uses in their feed the trick here is not just display the grid but make the code smart enough to display the photos correctly regardless of whether there's one photo or 21 there's a figma
file okay so basically we want to write some CSS that when there's a single image it displays it like this otherwise displays the grids and then if there's more than five it it adds the the Plus on there doing this with pure CSS I am I can't immediately think of how I would do this like in my mind I would need JavaScript for this but it's seems like it would be possible with just
CSS yeah all right let's try it let's download the project files oh I think you're right America 2050 we have to you yeah so again I'm not the best at CSS um so you can you can provide hence but what America 2050 just said like in child so we could see like if it has a certain number of children then specify the the grid should be a certain way way
okay and if you're just joining us uh we are doing Advent of CSS I think it's Advent of css.com um where you essentially get a new challenge every day in December for CSS and the what I'm learning now is they don't have a built-in editor in the browser they actually give you files that you need to download that are your assets and then you can just do all of your coding locally so that is uh that's what we'll do let me get set up real quick
Advent of CSS
okay and uh I think I'm just going to use plain old JavaScript HTML and CSS I'm not going to use any framework or anything like that and uh I made a folder called 2024 and then a folder called called 01 and then uh these are the photos that they
provided and we'll use those I'm going to move it into uh a lowercase photos cool and I'll open this up with vs
code okay I need an index.html like so we'll call this AOC CSS number one and then we've got all of these photos so let's do some command line I think if well actually if I do this ls-1 photos good enough so basically I want to add all of these images into my HT and then we'll start to style it um so I'll probably have like a section and then we're going to have each of these and then I'm going to put each of these inside of an image tag and then we'll we'll start working on our CSS so select all of this make sure that the path is right so photo SL that we want an image tag the source is going to be
here like so okay um and then I'll just use a a thing called light server I don't know if I have it yeah so light server it's um um Auto refreshing Dev server but there's no build process basically if it sees changes to any file in this folder it's just going to refresh the browser for us but there we go uh we have a page with some giant images I mean we could we could go ahead and let's go ahead and add some Styles so I'm going to add a link and we'll create our styles .
CSS and for now let's just make all of the images have a width of 100% so we can we can see them on the page um yeah so there they are wonderful now do we get any other hints I think it just says here's a figma file here's the photos make it happen um and Amer 2050 says there's a weird icon in my VSS code why does it have a post CSS icon yeah so that is post CSS I think I have post CSS set as my um my default thing because I use Tail land a lot of times yeah it's post CSS okay I think this is a job for display grid autofill never used it before yeah
yeah so no JavaScript we're going to do this all with CSS how would you implement the plus 21 maybe with the has the has CSS is about to be Turing complete um Jefferson says I have an issue with your hono template inside of nextjs somehow it doesn't find or like the route separation um that includes the open API spec on that route not quite sure what you mean if you open up an issue on the GitHub repo I could try to help you out if you if you provide like a code example um yeah open up an issue here and uh I'll try to help you out there's one that I need to respond to on there right now as well inth child yeah I don't know let's try it out okay so they gave us the base images we have
these figma files um let's just get these displayed in a grid and also with like a little bit of uh border radius because it looks like these images have border radius on them too um I'm G I'm going to go I'm going to work backwards I'm going to go final final form and then we're going to hide stuff depending on how many are there um so final form we're going to have a grid that has two rows two columns on the first row three columns on the second row so um what what did they call this they called it a gallery card so let's do this we're going to have a gallery
card um we're going to set the display to grid the grid template rows is just two rows and grid template columns wait can we
areas maybe we want how's this going to work with grid I don't I actually don't know um CU how would you have two columns in one I guess you could have two separate grids what is g what can we do with grid template
areas yeah this has got to be it right the only issue is then how do I assign each of the images it'll be like the inth child is how I'll assign it and Austin the dev thank you for the follow over there on uh twitch yeah yeah and Ken is mentioning it's not just you can't just drop in the hono stuff in an xgs there's a little bit of stuff you have to set
because then we could have d d and then e and then e like that maybe or can you do you not have to have the same amount can you have a different amount they have to be equal right and egg cetus or celus thank you for the follow on Twitch a a AA bbcc what's up bmind yeah I'm bad at CSS this is going to be the hardest thing we've done all day is uh I need to recreate this I like I could do it with like two a flex box in a flex box or a grid with two columns on top of a grid with three columns um
areas because my thought is if we have six so 1 two 3 four five six six and then we have 1 2 3 4 5 6 like
that right because then you have two and three uh we could have different grid template areas depending on the number of children and that could be like um um a has query but first off let's give this section the class of gallery
100% okay and then we'll say that there are it's not quite what I was thinking because um I guess I don't know how grid template areas works like does this not Define that there are two rows with six
a row is created for every separate string listed okay then why do we only have one oh because there's six elements I see I'm telling it
ah because I'm telling it that there's going to be six images across the top but that is that is not what I want
um okay uh Pao is saying I need to assign a grid area to every image so if we're going to use grid template areas then
or oh okay so this is this is what uh what Pao is saying is there an advent of HTML we could make one so the first child has a the second child has B look at repeat autofill or autoit okay but can someone explain to me in 200 characters or less how do we recreate just just let's not even think about the scenario where we have to change it depending on the number of children how would you create this with CSS grid I guess you could use uh column spans like how would you create this with CSS grid without assigning each element to a specific
cell autofit will create columns automatically grid six columns the first two span three the second row span two each okay so that's kind of what I was trying to do with grid template areas but how do I set up the column spans those are going to have to be on the children aren't they uh yeah good call I do have six do I have six images yes so let's just do it with five you just specify the length of the column all right let's first start with uh
we want uh six columns okay and two rows okay now we need to tell it that the first two children span two columns just make three columns in two rows can something span a column and a half you see my dilemma do you see you can have something span one and a half columns that's what Prav just said okay I'm learning a lot about CSS today column span that's a table cell
okay how do I make something span two columns because I don't even know enough to do that and um I don't know what I don't know what the property is called like in flexbox you would have like Place self or something like
um template cuz it look like that colum span property okay so let's just say the first child
Taco CU see like column span is something completely different grid column grid column start no GD column span that's not a that's not a valid prop how many developers does it take to span an element across two
columns let's see what this stack Overflow has to say yeah this is what we want greed column is what we want gotcha okay so the the first one and the second one are going to span two [Music]
here do I need to say image so the first how does inth child work I also don't know how that works
it doesn't seem like it's getting applied do I have to use um oh that that wouldn't work because we're we're using nested CSS there we go okay
yeah and actually we want these to take up three like that all right we're on our way everyone here we go um I think the other thing we'll do let's just give this Gallery card a fixed height of I don't know 800 pixels um overflow hidden and then um the images um can we set the height of the
all right we're going to lose the aspect ratio for a second yeah this is fine we'll we'll fix the aspect ratio in a second yeah we'll fix the aspect ratio in a second all right um I'm going to give it a gap let's call it Two Rim that's too much one Rim that's better each image is is going to have some border radius 25 pixels uh 15 pixels okay and then now the third fourth and fifth are only going to span two columns like that okay we're closer um oh yeah you're right
Oscar what's up Oscar I see you in the twitch chat who says 15 pixels why don't you just use one Rim which I guess would be 16 pixels yeah okay now how do I make the image well I I think now if I do object fit cover
right contain let's see what contain does no apparently not okay um let's put the little text on the the last one here this plus 21 and then we'll and then we'll work from there then we'll make it work without the plus 21 then we'll make it work with just four images Okay um the fifth child now has the after pseudo selector is going to have content that just says plus
pixels um what's up he says I love this so much I found my people basically it's everybody just yelling at me trying to uh get me to do it right um position absolute on the pseudo selector okay thank you omo and Stella so that'll put it on top of the element one would
hope um do I want after oh then the parent needs to be so position absolute on the image and then position relative on the image okay
oh uh it's I think it's okay to put position relative on all of them do I want to do before or after what's the difference
here Z index says Stella 99 huh um can image tags have content do I need to make this like um yeah images might not have pseudo selectors do I do I need to make this uh what do you call it
block no make the color I'm going to make the color of the text red yes it was Stella's suggestion to change the color so we can see it like where is it even showing up I don't see it anywhere Pao is saying an image is a substituted element so it has no before and after okay let's look up uh CSS image sudo before
um oh and then add style background image yeah this just says there's no way to do it and then the pseudo class needs height and width can it uh um can it be
100% here's the thing if I place a div in the same one what I what I want to avoid is creating a div wrapper for every single image we could do that though right we in our HTML we could put each one of these inside of a div and then it could have content on top of it yeah what's up harsh good to see
containers uh CH Christian is mentioning use an ordered list and then use the counter value of the last child for dynamic data that's clever I was just going to hardcode plus 21 I meant both before and after oh well then I can't have content here how would I how would I put the plus 21 if I have to it has to be on both um put the content
list maybe I should just stick to typescript listen the only way to get better is with practice so here's what we're going to do we're going to put this into a list
well the issue with it being in an ordered list is now it well I guess if it's an ordered list then I could put it I could make that the
look at that um does this get rid of the display grid though oh because I told it the specifically my CSS is looking for uh images but I think I can just remove all of these selectors and just just the inth child
now okay it's a little better um we can see the marker um is getting put in there also the thing is getting chopped off but we now have a marker that we can mess with so I think by default um we will not display the
style none okay uh there's a lot of weird I think yeah there's some default uh let's let's do a basic reset no margin no padding box sizing of Border
oh because the L I think the Allies these are 100% width of their parent but the LI needs to be that as well
algorithms yeah um the ordered list is really messing with my Styles I guess I didn't think about
space cuz now I couldn't even explain to you why these are taller than the others I guess cuz I have height 100%
um okay here's Here's my thought uh list items um have overflow hidden um and then uh position
relative do I need that on there on the list item yeah this did I do it I guess I did it I barely know basically I hid the Overflow from each individual list item and now now it looks fine okay great what we want to do is we want to take the last item so what was mentioned by Christian earlier was we need some kind of counter so use this structure then use the counter value of the last child as the count value for the dynamic data um so I know CSS has counters I've never used them how is this going to work
okay so uh reset the counter on the parent then we we give it a name in this case they're naming it count let's just call this total we're resetting it back to uh zero then we want to increment that count for every list item
it oh we just by default it's going to increment it by one okay and then on the last or on the fifth child we're going to set the content to be counter total color's red um position absolute uh we can use the after well this is where we would say were saying uh display the actual list number but I don't think we need to now that we have the counter um and then we can here do
zero the LI has position relative this has position absolute there it is it's happening everyone okay um we want to center it and I guess we also want maths the counter needs to start uh needs to be reset on the fifth item and then only increment for everyone after that um because in this case um here's the issue though how do if we have more items here like let's say we had a bunch more images inside of Li how do we not display them in the grid I guess we could just give them display
none right because if we do this it's going to add it there I don't I don't want it to be there I want um every other child after this one to be hidden is it 6 Plus that is not valid syntax
6 yeah wow look at us okay so uh basically when you look at the P when you look at the source of the page there are there will there potentially will be a bunch of them and that is what's going to reset the counter so what we can do is specifically on The Fifth Element we reset the counter to zero and then every item after that um increments the count so this will give us an actual count of the the rest the rest of them so should just say
uh one I'm curious though if I add more is that going to increase the count 1 two 3 four five there should be five more no it didn't okay that's not quite right um
I guess counter increment isn't working on this inth child here like this potentially isn't getting run for every other
child H uh did anybody confirm let's see uh Christan saying the count is only final within the last child's Li so that list item should function as the overlay above the fifth I'm resetting on that yeah because I I want it I guess I I just guess it doesn't matter where we reset it because we can reset it at the top but then for everyone after that increase it but that still doesn't do
valid all right uh I'm just going to style it for now then we'll we then we'll do all of the the rest of them for like uh showing less and less images and then we'll worry about getting that count correct um okay how do I get this thing in the center um I guess I could say width 100% height 100% display grid Place content Center first try um we'll set the uh font family to uh sans's serif all right let's make it bold all right let's increase it even
um it looks like it doesn't have a background or anything like that but it does not look good when that is the fifth image um let's swap those two
six it has a background with the opacity lowered um I don't I don't think so in the examples we could we could do that though like we could actually set the uh background here to uh rgba zero 0o uh 0 0.5 like that but this needs to have the same border
radius we could add a nice little uh cursor pointer on there click for more oh overflow clip that's probably better because then we don't have to duplicate the Border
radius no this needs to be overflow hidden uh can you do overflow clip on the on the uh on this one no I'm just going to put the Border radius in both spots okay let's get this number this number working um we would instead like to increment the total for each item after and it doesn't
increment I mean in in the chat let me know if this is right so in plus 6 means this will match the six element and every element after that I don't think it will um there so I have some extras here so I have 1 2 3 4 five and then 6 7 8 9 10 so it should say plus five automatically
above I guess that's I guess that's the other way to do it is keep the counter increment for every item but then reset it when you reach the fifth item that's the way to go huh
none minus5 won't work because we can't do maths as far as I know can I don't think we can do maths on the total value yeah display none is making it not uh oh well the mixed nuts in twitch chat mentioned content paint does that mean it'll draw it but yeah my guess is if we do uh visibility hidden then it's still there oh yeah that's the issue is it it now takes up space but the issue now is uh with display none the counter isn't getting incremented because it's not actually drawing it on the page
okay the thing is I think we're kind of ignoring what chrisan was mentioning to begin with because we're not even using the last list items number um use the counter value of the last last child as the count value for the dynamic data are we doing what you had in mind Christian or am I going down a completely different
path the count is not shared between Lis the value is different with each lii so you need to show the after within the last child so do we just put every last item every item um we put it in uh specifically that that spot how do we place it in the um here how do we place it in a specific uh row and column grid row two is that how that
works R grid row start to grid row end to uh visibility hidden changes the the the layout because it's it's still there um and grid column
or not what's going on here now EV every one of these is in the same spot which is what so they're being rendered they're just being rendered on top of each other I want column end seven yeah and and then uh for the last child is where we'll display the
count because it's going to be the thing that's drawn on top of all the others though we could we could actually just change the
I guess the issue is we would have to display it on the last one is it the possible that we're displaying because we have the total value on the fifth child it's displaying the total as it was when the fifth child was rendered
child anav says yes Chris johani says exactly okay so we're displaying the last child only and everything else is getting rendered on top of it um then we don't need to do the zindex thing yeah okay this is it and now we can reset the counter um on the fifth child that's it that's not it because it's going to increment on itself okay so it's plus five check it out um if we add a bunch more in the HTML so 6 7 8 9 10 now it should be plus 10 yes yes okay so now we take one two three four five now it should just be plus one oh no never mind a lot there's a lot
more here now it's plus one and then it'll be plus zero okay we did it chrisan says great job oh that was tricky uh CSS got hands it's true thank you Christian omo says I can sleep now Prav says me too um be mine says had to run for a bit here's how I would do areas if you were cous curious you can remove the column and row templates if you wanted more int intrinsic let's check it out um yeah this is what I was thinking earlier yeah so this is it you placed each one in a specific yeah that I guess that'd be the way to go that's yes and at this point we're manually placing each of them anyways yeah ELO thank you for hanging out um did I miss anything else all the
CSS stuff okay but now we only only display this if there are if the counter value is um greater than zero so uh what we do is um if the gallery card has in plus six children then we put this on there
did I do that first try okay so now there's only five of them but if I add a six one it should say plus one it doesn't dang it plus two no it doesn't okay it just removed it entirely um how does hasz
work okay this should still be working it's plus two um if Gallery card has as in child okay I there I uh I made a site for this I think it's is it has.
syntax FM yeah so Wes tweeted out a bunch of uh CSS has examples and then they did an episode on it and then I made a mini site um so select an element if all of its children are empty it's not what we want we want select an element with a certain number of quantity queries here we go oh this is what I want um so if it has six in then
select Only The Fifth Element select all but the first five that's so this is what we're doing so far n plus
six H we want to select the fifth no um is it oh I was like is it working all of a sudden no we got rid of this um no I I basically I only want to display this plus two if there are six or six or more children of gallery card
say says I wouldn't Pro I probably wouldn't be able to do this even in JavaScript oh I could do it in JavaScript um but yeah this is tricky um at this point we already shipped well here's the thing we did this one we still have to do all of these but this all has to do this is like quantity queries because we're going to change change the display we're going to change the grid depending on the number of children um but I'm working backwards so I only want to display this if we have six or more children I guess we can ask chat GPT let me log in
okay chat GPT how can I use the CSS has selector to select an element only if it has at least six
children this is what we did inth child 6 checks if the parent element contains at least six children that's I thought we did this
though nested CSS Works differently well I mean we could do it without nested CSS because we could just if we're not using nested CSS then this would just be if a gallery card has six children turn the background red why does it not work with nesting oh do I have to use an Amper
[Laughter] we needed the ERS in that's really weird I thought the has would just apply to the but that makes sense yeah so the emper you need the ersan if you're applying has uh with like modern nested CSS okay that should do it then so now uh if there is just one extra it should say plus one if are no extras it shouldn't display at all great yeah kin says nice we did it this is this is taking us longer than everything that we've done today um but with that has pseudo selector working it's easy it's easy then we can just do uh uh we can apply these
Styles um B based on the number of children so if we have four children we're going to change the grid entirely
this look at this yeah so uh if the gallery card has four children and it does not have five children that means it has exactly for children the so iito is asking what is the Ampersand so we're using CSS with no pre-processor and CSS nesting is a newer thing and they introduced the Amper sand for certain scenarios but basically we're using nesting to say Gallery card has okay so uh if there are only four but not five is the background red yes and if there are five the background is our selector works oh my gosh okay uh yeah okay so now when there are four we have to just change where all where everything goes
um five or six I think so I I think I can do this and this should still work for so we have five six s so it should say plus two it does great um
and I think what I also want is if it has five children I'm going to move all of my column placement into there like all of this gets moved into if we have five children
yeah um but then if we only have four children now all bets are off now we have to place things accordingly um I think we'll also set the rows and columns based on the number of children as well so uh if we has five children that is what our rows and columns look like but if we have four children we're just going to have uh two rows two columns easy simple as and that that matches the spec um Yep this is amazing I'm glad we finally got here it took me a while but now if you have five it puts it there if you have six it'll say plus one if you have seven it'll say plus two um but then if
you move back down to four look at that okay it's super easy after this now we just have another selector to say if there are three but not four
children then we want uh we're going to have to do a similar thing where we have two rows two rows two columns but if you are the third child then you will span two columns um so this is if there are three but not four children then the third child spans two columns easy okay if there are two children but not three that means we just want uh two columns one row so we'll go from three down to two
row oh yeah but then if I comment this one out yeah we did it remember kids CSS is a programming language uh thank you Oscar for the claps who says this is very readable after five months and thank you yeah thanks for that follow uh Riley does rans uh it's I think you can safely remove rows it's not needed only columns may work yeah well thank you for the claps uh ad rman we've done it okay so uh this is huge this is only CSS not a single line of JavaScript basically the number of images inside of the element corresponds to um how many um rows and columns here well I'll show you as I comment out well am I going to do this like
this it's going to be a little scrunched but let me show you so if we have one image it displays it if we have two images uh they display as two columns if we have three images we get two on top one on the bottom if we have four then we get a grid like that if we have five then we get um two on top three on the bottom if we have six it just displays uh plus one and then any more than that it increases the number there wow we we learned so much today actually let's do a recap of what we learned and then I'm going to get out of here uh day two manage memory with CSS yeah we're going to be like allocating memory no unit tests I guess you you could do like snapshot testing uh besides the challenge do you think you should use JavaScript for something like this it depends the main reason to avoid it is
to have less code that you're sending to the um to to your users right because if all we need is CSS um it's potentially less code may or may it may or may not be less code honestly it might actually it it might be less JavaScript if we wrote JavaScript but the other thing is also like Progressive enhancement like maybe they someone has JavaScript disabled now this thing works purely with CSS yeah so learning this would keep you doing it this way or JavaScript the I yeah it depends it depends I think personally it took me an hour and a half to get this working I could do this with JavaScript in 5 minutes so I guess it's also about like well what are you good at I know some people that are really good at CSS and can probably do this in five minutes I couldn't because I have
not worked with CSS grid this much uh we did a whole lot of CSS has we did a whole lot of pseudo selectors and then also the CSS counter thing was super interesting
yeah yeah mixnut says I like the CSS way since it's painting using off threads yeah try and remove Gade template row let's see
yeah it still works because after two things are placed in the columns it's going to wrap to the next row and then we're setting the columns for that
yeah yeah I would say the uh that is something that's a caveat of the way that we implemented this so Oscar is saying um you'll need JavaScript you won't display all the images so this is the issue with our implementation when we have more than five images we're still adding all of those images to the page in order to get the counter to increase but we're just adding them all on top of each other in this very last uh this very last cell so yeah but let's talk through our solution then I'm going to get out of here so uh first of all we're using CSS grid the other thing we're using list items here but now that I think about it because we're using the counter we actually don't need list items these could have just this could have just been divs as well because the the fun part here is CSS counters I I haven't yet found my own use case for CSS counters but we're doing it here so if
you didn't know CSS has the ability to create counters and then increase them as each element gets rendered so in this example they're creating a counter called count it starts off as negative -1 and then for every Li that matches this selector they're increasing that count by two and then you can use that count in your CSS like in the content here so this is how they're able to number this as 1 35 7 9 with purely with CSS and so we're using count to count the number of images so I can tell you how many more images there are than are displayed so if we have uh just five there's no count but if we add a six image now we say there's one more image um you could display none of the images within okay maybe you're going to work on that message we we tried doing display none earlier but the issue was
if it's not rendered on the page then the counter doesn't get increased yeah that's what we tried earlier be mind I mean we could try it again so um right now it says plus two but what what we did is um anything after don't display it and then it doesn't increase so the thing about the counter is that element needs to be rendered on the page otherwise the counter doesn't increase yeah potentially we could do something with visibility but yeah but yeah counters super cool so basically what we did is we reset the counter so uh for every list item on the page we increment this counter called total and by default it increments by one so every single list item it's increasing by one but then we reset the counter on The Fifth Element so we have a selector
here for The Fifth Element and we're resetting it to negative 1 so it'll increase to one on The Fifth Element itself or sorry it'll increase to zero on The Fifth Element itself and then the six it'll be one seventh it'll be two so basically by resetting it here every element afterwards increases the counter and that's how we can get that plus um and the and the way that we also got this working is every element afterwards is all getting rendered into that last cell so they're they're always being placed in that exact same spot on the bottom right here they're basically all just being stacked and so the user can't actually tell that there are a bunch of images right here but that's what we're doing to to increase that number um so yeah that's the bulk of the bulk of the work there and then we're using a pseudo selector on that after element so after the last element uh we can access that counter value and set it as the content so that's how we're actually displaying the text um we used CSS has a bunch so here we're saying if
there is a sixth child meaning there's more than five then we need to display that count and then the other thing is um if there are at least five children then we specifically want this layout with two columns three columns and then the rest of these we just did but basically uh if there are four children but there are not five children that means there there are exactly four children then we change it to be two rows two columns so if we just have four then it just pops into two rows two columns and then we use that same trick for every other one where if it has three then we change the grid and then if it has two we change the grid and then if it has one we change the grid okay so that was day one of Advent of CSS I'm not going to do the others but it was it was a fun practice I think uh we the place where we increased our skills the most is probably CSS counter and then also
messing around with CSS has and also using native uh CSS nesting because this is something I don't do as often and we had a bug where this wasn't working because we were missing that Amper sand yeah yeah uh oh that's a good point Christian display the Lis but don't display the image display none on the image so that way they're all just be scrunched up and there'll be none of them yeah react 19 is
stable would you look at that I'm I'm happy to hear that because nextjs was using a non-stable version wow okay that's that's good to know who needs react we got CSS yeah yeah so Vivian is mentioning do a display non only on image yeah that would have worked too okay I'm going to get out of here thank thanks everyone for hanging out I'll remind you why why we were here to begin with was for a debug December there's actually a ton of these like code challenge websites that you can do um in the month of December so Advent of CSS is the one that we just did which I'll say it was a struggle for me we um I spent probably I mean almost two hours just on that one challenge but that's me I I need to work on my CSS uh we also did Advent of
TS which helps you to learn and practice typescript and in the day5 challenge we got to do generics so you can learn about the basic of basics of generics and then what we worked on at the beginning was debug December so this is put on by Sentry and uh these are just little challenges we have to find the bug in the code and you still have time because they do giveaways on the 6th the 12th the 18th and the 24th uh of December um so you have little over 24 hours to complete uh up to day six and then you'll be entered to to a raffle and uh you'll be able to win uh one item from the syntax shop so you could pick a t-shirt or a fingerboard or a skateboard deck or a hat uh if you complete this and then get entered into the raffle um if you want to see me do Advent of code follow me over on coding Gardens so
youtube.com/ coding Garden this weekend I'm going to go live and work on the first few days of Advent of C because I I have not touched Advent or even looked at Advent of code this year and I'm just going to do them all all in one go uh starting probably Saturday morning so uh follow over on coding Garden if you'd like to watch that um and I I stream here on Twitch as well um in goang no I'm going to do it in Dino because I heard that uh you get stickers if you do Advent of code with Doo um but I stream over here on uh on Twitch on coding Garden as well all right thanks everybody for hanging out do debug December uh have a wonderful rest of your week and weekend and um if if you're going to attend I'll I'll see you over there on coding Garden uh we will do a raid on Twitch so if you're on YouTube and you want to join the raid um head over to here a raid is basically where we take all the viewers of this stream and then send them over to some
other stream so that's what we're going to do stick around for that and uh yeah stay awesome we'll see you soon bye-bye bye-bye
Quote it with credit and a link. Quoting Syntax