Code Katas #26 - Solving Code Wars Katas and Talking Through My Process

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello friends welcome to coding garden with CJ welcome to code Coty's episode 26 it's gonna be a fun time let's say hello to all the people in the chat hello data welcome to the stream hello Danielle hello rabbit works JavaScript hello the coding cat hello data who asks thoughts on BTC mooning I think you mean Bitcoin mining maybe I'll tell you my thoughts on that hello I'm a tau welcome to a stream hello Tyco it's been a good long while since I've caught a live stream um the last time I streamed was I guess it was probably a week ago well over a week ago but before that I hadn't streamed for like a month yeah I'm back I'm here hello ran welcome to the stream hello mahira hello back hello Joey welcome to the stream hello Nessie hello Jonas welcome welcome so if you are new to the show all of the code that I have written in past episodes can be found on github github calm slash coding garden slash code - cut as you can see every episode you can see links to the episodes links to the Coty's links to my solutions etc and all of the Coty's that I solve tonight will be pushed up to this repo as well I have not selected any Coty's so those of you that are watching live if you have suggestions for Akata that you would like me to solve feel free to throw them in the chat otherwise I'm just gonna maybe find some maybe we'll take a poll if I find a couple that I like and I'll decide which one to do but yeah here we are hello b-max welcome the stream hello Alka welcome welcome oh that's a good call rabbit work says will you be in the CI server soon I think you mean like the coding garden community app yeah I've I've been busy and neglecting that but yes this week I will have time to get back in do code reviews and talk with people and stuff like that okay um I I see that's what he that's what data said earlier thoughts about BTC mooning meaning that Bitcoin has gone gone up to the moon oh yeah Becca's asking can you solve the one I upload it to your github as a problem is that possibly the issue here yeah here is back yeah I can I haven't looked at this one in depth yet but it is a 5q so when I get to the five Q's I'll probably do this one yeah so definitely thanks for suggesting that cool okay I usually like to start on the easier ones let's pick out a 7q to solve right now I have it filtered by my languages 7q let's maybe choose a topic like hmmm logic no and everyone that has a check by it I have solved before this will be interesting is this a triangle Oh apparently I've attempted it I don't know breaking chocolate problem your task is to split the chocolate bar of given dimensions in by M into excuse me small squares each square is a size one by one and unbreakable implement a function that will return a minimum number of breaks needed whoa okay that I mean this is it's supposed to be easy probably if I thought about how to solve it it might be easy but that seems tricky for example if you're given a chocolate bar of size two by one you can split it into single squares and just one break but for size three you must do two breaks is it one less than the number of squares don't know that's an interesting one maybe I'll do that one let's find another one triangular treasure triangular numbers are so called because of the equilateral triangular shape that they occupy when laid out as dots for example first second so this has one dot this says three dots this has six dots okay this was created by Julian Nichols its Julie and watches the stream sometimes that's pretty cool maybe I'll do this one yeah that's him cool I'd have to look at this first I can't figure out what it wants me to do but it looks potentially doable let's see don't give me five in this kata you get the start number in the end number of a region and should return the count of all numbers except numbers with a 5 in it the start and the end number are both inclusive okay one through nine overweight you should return the count of all numbers except numbers with a five-minute okay so one through nine the count of all numbers except for anything of the five is eight because there is no five four two seventeen doesn't have a five or a 15 so there's 12 numbers okay that's doable let's pick like one more and I think we'll do a poll so maybe choose a different topic let's go puzzles you summing and numbers digits I feel like I've done one like this before write a function name some digits which takes a number as an input and returns the sum of the absolute value of each of the numbers decimal digits for example I haven't solved this one before but I definitely solved one like it on the stream before two fighters one winner create a function that returns the name of the winner in a fight between two fighters each fighter takes turns attacking the other and whoever kills the other first is victorious death is defined as having health less than or equal to zero each fighter will be a fighter object or instance see the fighter class below in your chosen language this sounds fun I kind of want to do this with both health and damage per attack will be integers larger than zero you can mutate the fighter objects for example you have a fighter that takes the name health and damage per attack yeah so it's like you have to create the object for it that's pretty cool okay um I'm gonna catch up on the chat a Matthau is linking to a 5q I might take a look at this one when we get down to 5q but I'll I'll keep it open over there Oh Joey's saying it's a literal candy crush problem chocolate crush people are saying they like the chocolates good England okay I want to do a quick poll though there are at least 30 people watching maybe five people will do the poll what can I use for a poll let me figure this out that's me hello I think this slide Oh is still slide you slide oh is still active yeah you yes you okay we're gonna do a poll gather a strawpoll Oh it's not it this one and it hasn't did pop up on the string there's a super chat from rabbit works thanks so much $2 super shot from from rabbit works who says CJ I must ask what are your thoughts on view 3.0 maybe I should do a whole episode my thoughts on 33.0 but that's why isn't this copying copy copy paced koffie yes yes it's technical difficulties too please give me one moment slide okay thoughts on view 3.0 I think it's really cool that they're rewriting it in typescript that's gonna make it much easier for people to contribute and it I say that because with with typescript and having a typed codebase new developers that have the right development tools like vs code can navigate the codebase a lot easier and will have a lot more suggestions in their editor if they're trying to contribute to it so I like the idea that they're rewriting an entire script so that they can get a lot of more a lot more contributors and a cleaner codebase so that's cool I like the fact that they have tons of RFC's open that pretty much anyone can comment on those of you might have seen the recent I don't know what to call it but a lot of people got really really salty or like really concerned about one of the RFC's and said some pretty mean things and just got very serious about it and that's not cool but the result of all of it is that the RFC has been updated to say that there actually won't be any breaking changes it was it was an RFC that talked about how the view how you could write view components in a more functional style without having like a data property or having methods or computed and some people started commenting saying that they hated it it's gonna break the syntax view is turning into angular 2 all this drama but ultimately there's it's not gonna be a breaking change it's gonna be an option and so the thing I like about view 3 is that it's all up for discussion like if you don't like something that's gonna change you can talk about it and you can give a reason for it not to change what else yeah I don't know I mean I think in general like view like your it's still gonna be one of the easiest front-end frameworks to learn and pick up and build apps with so I I'll still be writing view even when it goes to a 3.0 I don't think it's gonna be the same thing that like like what happened when angular 1 decided to go - well not cited they went to angular 2 but it was a total rewrite now it's like angular versus angularjs I don't know but that it was root only couldn't put completely different things view 3.0 is not gonna be that way so I'm all for it has anyone tried typing in this URL yet what do i do what do I do okay holes here we go I'm a little scatterbrained today um I'm almost there aren't I this is it this is the winner okay stick this took a stick way too long to get there's the pole I'm about to create a pole but we can vote on which kata I should do okay here we go here we go great a pole moldable choice which 7q kata should I do option number one is the breaking chocolate problem option number two is the triangular treasure created by Julianne Nichols a coating garden a spectator and community member what else don't give me five what else last one two fighters one winner and I will say I am partial to this one this one sounds like it's gonna be fun I like object-oriented things you cannot select multiple options go I think I have to start it go vote now thanks Kai Greenshaw for putting the link I was having a really hard time getting that link in there yeah you're right right person not enough year but I had very little caffeine today yeah rabbit works is saying about view 3.0 I think I'll enjoy it after getting used to it the thing is as far as I know the the core way of building VJ s apps is still gonna work but you can still write apps the same way you've been writing apps it's really just the internals are changing yeah so it should I mean from my understanding it should be the same old same old view okay we have 71 percent of the votes for the two fighters one winner and 21 percent for the breaking chocolate problem this is the one that I was favoring I'm gonna do it I'm gonna do it that's the one I'm gonna do thank you all for voting who ever voted uh stop the voting now okay all right okay here we go first thing I'm gonna pull this down locally it is using vert node version 8 I wonder if I can use a class instead of a constructor function we'll see all right new file what do we call it this is what we call it you it's our our tests okay here we go so ultimately we have to implement a couple of things so we we have to implement this declare winner function which takes in two fighters and whoever attacks first and then based on their initial stats will return the name of the winner here oh the the return should be the name of the winner this is the name of the of the first attacker so first we have to implement either a class or a constructor function for the fighters and then we have to fight them so let's let's read this again so each fighter will be a knob each fighter will be a fighter object or instance so we can see here when they create a new fighter they're passing in three properties the name the second parameter is health and this is damage per attack it's a pretty simple fighting concept you have the first fighter and then you just attack back and forth and the damage gets decreased so probably the first thing we need to do is create a class fighter so that way we can create instance of a fighter I'm going to use the class syntax and maybe I'll write it as a constructor function so we can compare afterwards but anytime in your code when you see the new keyword they are creating a new instance of some class so we need this fighter class and you'll notice my my editor stopped complaining so the moment I created the fighter class they're just like oh I know what that thing is and then this class needs a constructor and the constructor takes in three things it takes in their name their health and damage per attack so we can see that those three things will correspond to these three things and then the instance itself needs to know what these properties are so we're gonna put it on the instance and you do that with the fist keyword so say this dot name equals name fist health equals health this dot damage per attack equals damage per attack ultimately what that does is creates an object that has these properties named health and damage per attack so for instance if I were to just log fighter one here we're going to see an object that has three properties named health and damage per attack and it has those three properties because we put them on the instance using the this keyword so let's start at Raqqa and we can see there it is it logged the fighter and it's an object that has three properties name is Lu health is ten damage per attack is two and just to show what's happening here like if I comment that out this object is not going to have a health property so by using the this keyword we're putting a property on to the instance of that fighter cool so we have two fighters and then we we need to fight them so yeah create a function that returns the name of the winner and a fight between two fighters each fighter takes turn attacking the other and whoever kills the other first is victorious cool so what we first need to do is determine which of these two fighters is the first attacker so the third parameter that's being passed in here is the name of the first attacker so let's let's say let's call this first attacker name and we need a variable that's going to be like who is the current attacker and in this case they're going to be the first detector initially we don't know who they are we basically have to compare the name of fighter one with this and if it matches then they're the first of the first fighter so we could say if fighter one dot name is equal to first attacker name excuse me then current attacker is going to be fighter one else it's going to be the other one cool so based on the name they gave us we determine who the current attacker is now we just need to go back and forth so we need to attack one against the other and decrease their health so let's do this so we probably need some sort of like while loop so we could probably say like while fighter one dot health is greater than zero or wait so we basically want to keep repeating the the attack sequence until one of them is dead so while fighter one health does is we'll say greater than zero and fighter to health is greater than zero so the moment one of them one of their health goes to zero we know that the fight is over okay now we need to attack them against each other so we we want to do this in more like an object-oriented fashion so I think the fighter should have an attack function that takes in damage and then decreases its health by that damage so here I would say this dot health minus equals damage so when you attack a fighter its health is going to go down and so I guess we need probably two variables so we have current attacker and then like current defender so the person receiving receiving the attack that way we can keep track of both and we'll like swap them so we'll say current defender is equal to in this case fight or two but in this case they are fighter one so we have these two variables current attacker and current defender so the first thing we're gonna do in like while we're simulating this fight is we're gonna say current defender dot attack with current attackers damage per attack right so we are passing in the damage per attack to the attack function for the current defender reducing their health and then they'll swap places and do it again so we'll say current defender equals current attacker and then we need a temp variable to say current attacker equals yeah it's the old swapping problem so set temp equal to current defender and then set current defender equal to the current attacker and then current attacker becomes the current defender so we're swapping their places it's gonna go back around and the other one's gonna attack the other one okay I realize I just coded this this whole thing with no pseudo code but I'm gonna take a break look at the chat like I said I'm I'm really I'm really off tonight okay okay yeah hello Alan welcome to the stream coding cat is asking what is caca yeah and Sean answered so it's a plug-in that actually runs the code here so anytime I have a console log it'll actually show the output of the of the code I'm so off tonight okay so you log the thing and we see what it actually is it's actually running the code in real time and so we can see at the end of this so let's log the current attacker and current defender before so before the while loop there just as they started so Lou has ten health and two damage per attack and Harry has five health and four damage per attack and then they go back and forth and after it also the moment one of their health is below zero it comes out of the while loop and then we have Harry who's now at negative one and Lou who still has to health so Lou is actually the winner here and basically the last thing we have to do is just see which of the fighters has some remaining health but one thing we could do is we could add a property to the the class something like this is alive and that's going to be if health is greater than zero so that I could just default this to true but that way if you pass in like a negative health they're like instantly dead so we have is alive here and then anytime you attack we can update that variable and say if this dot health is less than or equal to zero this is alive equals false just like that and actually I guess instead of setting a property this could actually also just be a function so instead of having a property here you actually have a function that just returns health is greater than zero so let's do that let's say is alive and this just returns this dot health greater than zero because if it's greater than zero is alive we'll return true cool so at this point when the loop is all done and finished we need to determine which one of the two is alive so we'll say if fighter one dot is alive return fighter one name else return fighter 2 dot name simple as that it's thinking of something else we could do I don't know maybe not but if we look at this currently we got the right result there you get the right result there we got the right result there and there and there okay so our code is working now does anyone have suggestions for how I can clean this thing up maybe how can I make it more object-oriented and then I'll also show how you would do this using a constructor function and the prototype rather than a class but this is like the basic code I think this could maybe be a little bit better let's see yeah I like so our jingle angel is saying wouldn't it make more sense for the method to be called take damage yeah because it's not the one that's doing the attacking we could actually I like how about this so what if attack takes in the fighter that they're attacking and then we say fighter dot take damage with Vista damage per attack I like this because it hides a lot of the implementation details whereas right here we have to like reach in and grab that property so we'll have both an attack function and a take damage function so take damage we'll take in the damage and then just reduce the health so this dot health - equals the damage cool but now that we've done that we can say current attacker dot attack the current defender and that makes more sense so the attacker is attacking the defender but then when we get into this attack function it's it itself is calling the take damage function of the the other fighter the one that we're attacking and reducing it by its own damage per attack kind of like that and that should all still work yeah it all still works cool good suggestion oh good Cohen is mentioning a very cool way to swap variables yeah with es2015 you can use I think it is like dich its destructuring syntax but it basically allows you to to swap so I can say a comma B equals B comma a like that but these things don't exist so if I said basically I'm gonna swap the current attacker and the current defender and I just swapped them on this side like that nice I don't I don't know if this I think this just is just a play on destructuring syntax but it does allow you to kind of like swap in place without a temp variable someone someone correct me if I'm wrong but I do believe this isn't like a special swapping syntax this is actually array destructuring used for swapping Oh yeah an archangel is commenting on how we kind of made these two methods here so they say as a game programmer myself who has to work purely in the style isolating instances and having them be responsible for themselves makes sense to me I don't know if this the there's a programming principle called the open-closed principle but earlier whenever I was like reaching in to grab the damage per attack and passing it to the function that's that's that's like leaky implementation details but by making it a lot simpler here if I ever need to change it I change it in one place and I don't have to change it in all the places that I've called the function so I think it makes sense to do it this way cool I think I can I can clean this up slightly like choosing who the current attacker is if we default the current attacker to fighter one and fighter two then we really only need one if statement so we'll stay here if fighter ones name does not equal the first attacker name then we got it wrong and we're going to swap them otherwise we leave them as default that that kind of cleans up the code a little bit cool and I guess the other thing is instead of having this while loop technically we could would still be a while loop but we could return inside of the while loop so the moment one of their one of the fighters dies we just immediately return the other fighters name so we could do something like if fighter one is alive is not alive returned fighter two name same thing if fighter 2 is not alive then we return fighter one name and that will like immediately exit the function so we can just have it as simple as that let's see who any other thoughts suggestions on this while the auxin is saying how does the scope of all that work I think are we are we talking about the the destructuring here i think the thing is like typically when you see a ray destructuring you see like a let right there and if you put a let here then that is creating new variables but if you use existing variables and the values are going to be placed into those variables and the way a rady structuring works is you have the array on the right hand side and it has many values in it and then on the left hand side whatever is here matches the 0th index in the array and that value gets put into this variable and whatever is here matches the first index in the array and that value in the array gets put into this value so on the left hand side this is not an array this is a way of pooling values out of the array the right hand side is literally just an array with two values inside of it you yeah Wally oxen has a good point why not just do current defender that is alive that that's a great point because if you just the defender is the one that just got attacked so they're the only one we need to check to see if they're alive like this and then we only have to do one check I love it because we can say so we know that that current attacker is still alive because it's the one doing the attack if it was dead it wouldn't have made it back around and current defender is the one that just took some damage so it's really the only one that we need to check to see if it's still alive because it just took damage so if current defender is not alive then the attacker is the winner great great suggestions there's a debate on hello Topher welcome to the stream I'm doing code Coty's this was a very simple simulation of two fighters going back and forth it's a code it's a code Oh while the auxin is saying I was meaning more of the scope of the classes you're passing in the men but it's like global scope so the class itself is technically I mean if this were running in the browser this would be the global scope because it this class is available anywhere when we're doing the tests here we're actually creating instances of that class so like it would be like storing these in variables and so the scope of this instance is literally only exists inside of this function because when we invoke the function we create the instance then inside of the function it becomes fighter one but then at the end of the function invocation so when this function is done executing the fighter one variable isn't needed anymore so it's completely out of scope and then like on line 37 this this fighter is no longer in scope it's it's it's gone because it was only used right during the invocation another way you could write this let's say like Lu 1 equals that and Harry 1 equals this ultimately this function invocation does the same thing but because I stored them in variables before I called the function now those two fighters are actually still in scope action after the function invocation I think I think that's what you're asking but because we don't really need them multiple times we can kind of just do that all right one thing I'll show well actually let me just let me throw this in a code cut the code of Code Wars and see if it actually works sample tests Oh No you am I not allowed to overwrite so maybe I did too much work I think this is the only um okay I did too much work I wasn't supposed to create this class but if I write it the function way so here I can do this so I showed how to write it as a class but if I write it as a constructor function it should overwrite the one that they wrote and then I can add all of my functions on to it like this so I can say like this dot attack equals this function and this dot take damage equals this function and this dot is alive equals this function so what did I do wrong oh this wants to me colons this is one way of writing it in a second I'll show how to do at the prototype way if this even works the error I was getting right now let me run the sample test again is identifier fighter has already been declared the issue is the the class keyword I believe behaves very similarly similarly to like let in Const in that you can't overwrite something that already has this name and because they already have a function called fighter it won't let me declare the cat the class like that but if I override it I should be able to let's see yeah so that still works and the main issue there is I can't read declare fighter because in the scope of this kata a fighter already exists okay this is one way to write it but the other way uses the prototype and that is actually closer to what's happening here so if I want to add a function to the fighter I think it's a fighter dot prototype dot and then the name of the function that I want to add I can do fighter dot prototype dot take damage equals some function and then fighter dot prototype dot is alive equals that's engine ultimately the code will behave in a very similar way but there are different implications here by putting the function on the prototype every single instance shares the here's the function and I just realized something I can't actually use this here because I have this arrow function syntax so if I'm doing it the prototype way I actually do need to make these full full-on functions because they need to be able to be bound to the instance and JavaScript automatically does that for us like so so arrow functions cannot be bound regular functions can and so inside of here when I say this dot damage per attack it knows our JavaScript basically sets the visse of this function to be whatever instance we're calling it on but then all instances share the same function hope that makes sense but let's try it with this way it should still work sample deaths fool storks all right it's okay well yeah I don't know maybe I did get enough sleep last night but I'm feeling weird okay let's submit this and then move on because I've spent way too long on it attempt there you go nice oh look at all those tests you I'll submit let's see how other people did it yeah and so we went a much more verbose way of coding it but I think a lot like what's what's name yeah Archangel is mentioning like when you're like building a game or something like that if you were to code it just this way without having methods on the on the classes you would have a lot more code and a lot more places to update code if things change yeah so very similar logic we've just made it more object-oriented it took more of like a functional approach using objects very interesting this person added and is dead they also added an attack function that takes in the other and decreases their health we took it one step further and instead of just decreasing their health we excuse me we called dick take damage of the other fighter yeah that's interesting nobody else like added more functions to the to the class okay moving along let's find ourselves a 6q are these filtered right now I guess they are you yeah let's look for data types that are 6q Coty's i have not trained on is a number Prime find the unique number let's look at it there is an array with some numbers all numbers are equal except for one try to find it very interesting sort the odd if you came across this one last time you have an array of numbers your task is to sort ascending odd numbers but even numbers must be on their places yeah so only the odd numbers get sorted everything else stays in its original place seems kind of tricky look at this one complete the solution so that it splits the string into pairs of two characters if the string contains an odd number of characters than it should replace the missing second character of the final pair with an underscore that seems interesting as well right number in expanded form this one seems fun too you'll be given a number and you need to return it as a string in expanded form for example if you're given the number 12 you need to return a string that has ten plus two if you're given 42 you should return a string that has 40 plus two if you're given this you should return a string that has 70,000 plus 300 plus four very interesting anything else about this give me a diamond that's too long I don't want to read it all kamo case method right a simple dot camelcase method all words must have their first letter capitalised without spaces so if you're given this that's not camelcase that's a Pascal case isn't it I think Pascal case is where the first letter is capitalized camelcase the first letter is lowercase I don't know pick like one more simple encryption for building the encrypted string take every second character from the string then the other characters that are not every second character and can cat them as a new string do this n times this is a test becomes this time so take every second character from the string so tea yes I I don't know however but every second character so I think they mean like one two one two one two one two take the second one put them at the end weird and you do this in time so this was done once but this is done twice and you create an encryption function and a decryption function so an encryption function turns it into this and then you have to know the number of times to be able to decrypt it very interesting okay let's vote on which 6q i should do holdable choice alright first choice find the unique number second choice sort the odd third choice split strings fourth choice write the number in expanded form you and last one alternating split all right and I will reshare that link for anybody that lost it so we are currently voting on which 6q cata should I do you you you event didn't end it there we go okay here it comes again please vote now you and hello we all love Pok well on how you doing good morning from India that's great Sean says we should do a unique number which one was that first one find the unique number yep that one's interesting and hello ever Fabio from Bolivia that's great welcome 40% of the votes for find a unique number you you hello Eric from Mexico Steve that's great you okay the numbers where the numbers are moving I think I'm gonna stop it there the majority of people have voted for finding the unique number let's do it doo-doo-doo-doo that one okay I'm going to bring it down locally new file we have a function that takes in an array and let's start up Clark okay so right now our function returns undefined so we need to actually do the magic but we're given an array that only has two different numbers in it but one of them occurs multiple times we need to return the unique one so in this array zero is in there twice one is only in there one so we returned one in this array one is in there several times but two is only in there once we return two and this one three is in there several times but ten is only in there wants to return it in so we're finding the unique one in the array so how are we going to do this I think what I'm going to do is keep track of the count of every number that we've come across and the moment we come across something that has more than one instance of itself we know that the other number is the unique one so we need like a place to store the counts of numbers I'm thinking like an object or something like that then we need to iterate over the array and then for every element in the array we increment the count of the current number in the counts object so this is this is going to be the counts object every number that we come across we're going to increment its count and then we check if basically if there are too numbers in the array I'm sorry in the in the council object excuse me return the one with only one count yeah I think it's it and so by what I mean for a place to store the counsel of the numbers is like for this first instance we would create an object that has like the key of 0 and a value of 2 because there's two of them and then a key of one and a value of one because there's only only one of them and then like the case of this one we would have a key of one and a value of one two three four five and then a key of two and a value of one so we're using this to keep track of how many there are and the moment there's two in here we pick the one that only has one we know that we found the unique number that's how I'm gonna go about this yeah I could there are definitely other ways to go about this I think some people are mentioning kind of like if the previous one was different from the current one and we've found it we would also need to make sure that you're not just at the second one in the array like if the index is greater than 3 and the previous one is the same as the current one there's not the same than we found it I'm gonna go about it this way maybe we'll solve it that way okay so a place to store the counts of numbers I'm just gonna say counts gonna be an object then we need to iterate over the array for now I'm just going to do a basic for loop and we're iterating over this array and we'll create a variable called number that's going to be that given element in the array so what we want to do is increment the count of the current number in the counts object so I can say counts at number is equal to itself or set it equal to zero so basically the first time I come across a number I'm going to put it in here with the value zero so what this would do is if counts that number doesn't exist so right now counts as an empty object when I come across this zero it's gonna say oh this doesn't exist so we're gonna set it to zero so we're going to say counts at zero has the value zero and then right after that I'm going to increment it so I'm gonna I'll then say counts at number plus plus and that will then change it to a one so if it's not in there at it that's what this line does and then the next line just increments it this line also says if it's already in there then just keep it the way it is which is what we want okay so we'll keep this stays as an empty object now if there are two numbers in the counts object return the one with only one count so at this point if we've seen two different numbers and want and one of them has a count of one and the other one has a count greater than one we know that we found the unique number so the way I might do this is I can say if counts we can do object dot keys and if it's length is equal to two and that means at this point we have found two different unique numbers and it's really the first the first occurrence of a unique number so the moment we come across the second number we're then gonna drop into this if statement and in this case and like in this array when the moment we get to the two we're gonna drop into this if statement and now we need to check and see what they are so we can say an extra list let's store this in a variable it's stored in a variable we call it numbers it's call it entries you and then here we let's let's pull those those entries out do it do I want to do this yeah I probably I have like a and B equals the entries and I could say if entries at know if counts at a is equal to one and counts of B is greater than 1 then we need to return counts we need to return hey cuz that's that's the unique number and we need to return it as a string for now we'll leave it as is and then else if counts of B is equal to 1 and counts of B of a is greater than 1 then return B and basically what this does is it makes sure that one of them has only been seen once and the other one has been seen more than once and the reason that matters is like in the case where both unique numbers are at the beginning of the array once we get here we still don't know what the unique number is because we've only seen two things so we need to go one more and at this point one has accounts of 1 and 0 would have accounts of 2 and so we know that we have found the unique number so that does it there's definitely other ways to do this let's see what the chat has to say yeah creating a set we didn't solve this because we need we need to find the number that is unique a set would give us back a set with both numbers we wouldn't know which one only had one occurrence at least I don't think so maybe you're thinking of another way to use a set yeah and uh Hatem is saying just iterate when array and I plus one is different from a rate I then return a rate I that's one way to solve it but you do have to be careful for the very first two values in the array because if I get here or no if I sorry if I get here and if I say array at I plus 1 is not equal then I returned array at I I would actually get it wrong because I need to look at at least three elements so it could potentially work in this case like I would look is this one the same it is move along is this one the same it is move along this one's different so now I know array and I plus two is the unique one that would be one way to solve it we just need some tricky logic when the numbers are at the beginning of the array but actually I do want to solve that because I think we can handle it with like some initial conditions hello Cure mosque from Russia that's great it's very early in the morning there Seana saying compare the original or array with the unique finder and return the different one you okay so we have some user-submitted solutions let's check them out so Shawn is using sets here let's see what this does okay we're finding the difference in set a and set B difference here is with set a what do we call this with though are we calling this with compare the original array with unique finder we only get one array we get one array that has there's just some numbers in it so we're only given an array that has two different numbers in it but one of them is only only occurs once so what would be the two values that I pass in here let me know and then Hatem has a solution let's take a look oh there we go okay so we have an array and we say if array at I plus 1 does not equal array at I returned array at I plus 1 it works for these test cases it's what if I did this if 10 is the first occurrence here this function actually returns 3 as being the non unique one so we need a little bit of extra logic to make sure that in the case where the two possible values are at the beginning of the array we don't just return I plus 1 after probably the third value in the array we can then do this for the rest of the array like if we can be sure that the if - values in the array are the same this function will work and like similarly here like if I did 1 0-0 it returns the wrong unique number but this this I get where you're going with this but there's just some extra cases we need we need to take into account okay Ryan is saying split the array I mean the array is already split I don't know what you mean about by that Oh like in half so the sets become two different halves I see I see I see so one half of the array is a set the other half of the array is a set we now whichever set one one one of those two sets is going to have two entries in it the other ones only gonna have one and whichever entry is not in the other one is the unique value I get it now that makes sense you Tyko saying does the cotton not concern you with more than two number values as far as I know that is it so your array is only gonna have two numbers in it but only one of them occurs once and ha to him no worries so I think we can we can definitely expand on your solution well like I said we just need to account for that very tricky case where it's it's the first value if it's not and it works like if we do that or if like 10 was the last value in here or even here still works so we're gonna we're gonna we're gonna work on that but we can make it work for sure Stephen is saying you could sort the array and then check each element in the array with the next one until you find the one that doesn't match actually I thought I think I maybe not that but if you sort the array then the unique number is either going to be at the beginning or the end right yes I think me the first one or the last one so yeah we thought of like several different ways to solve this I'm gonna get this I'm gonna try to get to make this work with sets because a lot of people are suggesting it I still I still don't think I know what Shawn is asking so Shawn is saying set a is the original one and set B is the unique with what do you mean the original one like yes let me let me show you what I what I what I and actually let me go back to these original tests okay if I were to create a new set with this array I only get back 0 & 1 so what do you mean so I would still have to find the unique value I I still I still don't get it you okay so in your solution what we're saying here is this is like an array and the second one is a set you Shawn I need you I need you to write how do I invoke find yeah how do I invoke your difference function she showed me an exact example of what I would pass into your difference function because I still don't get it I want to I want to get with that I don't I don't get it like I said I'm uh I'm pretty slow today okay yeah so I mean I what I like what I kind of just showed is when you pass an array and really no matter the length into a set it plucks out the unique values but we need to know which one only occurs once and if you do that it does tell you that 10 and 3 are the only values in here but it doesn't tell us which one only occurred once ok I'm gonna I'm gonna try to solve it using the way that Tim suggested but we can add in that extra logic so what this is doing is it's just comparing the current value with the next value in the array and if it's not the same we found our unique value but that breaks if our unique value is the first value in the array so really we just need to add an extra check up here so we can say if array at zero does not equal array at one then we basically just compared the first two we know they're not the same so we know that one of those has to be the unique one and really we just need to look at the third value in the array so we can say if array at 0 is equal to array at the third value in the array then our unique value is array at 1 else our unique value is array at 0 because 0 wouldn't did not equal the third one and that fixes it so we just handle the special case where our unique value is one of the first two so I believe that's what I think we're technically works so the special cases if the unique value is the first one in the array whoa I think that yeah I think that does it yeah I think you yeah people are still suggesting it I really I really don't what's happening okay one suggestion that was made though was what if we were to sort the array one thing we can do is if we sort the array then we instantly know that the unique value is either at the beginning or the end so let's let's do this we pretty much can reuse this this logic here but what we'll do is we'll say array dot sort does that work by default or do I need to pass in the comparison function okay so first we compare the first two values in the array if they're not the same then our our unique value is the first one in the in the array else we have to look at the end of the array now that we've sorted it so well yeah if it's if it's not the first value in the array then it's gonna be the last one there we go so we sort it from I think this is doing least to greatest and after it's been sorted if the first two values in the array are not the same we know that the first one or Dewey the first one is the unique one because it's from least to greatest and then similarly if the first two are equal then we know the last value in the array is the unique one because it's been sorted from least to greatest I think that makes sense we can we can try it on cohorts and see if it works for other cases as well yeah that still works we try it like in a different order like that that still works okay so that is also an interesting solution you you hmm you yeah so someone just shared a stack overflow the one thing I don't like about it is it's it's a nested loop hiding in plain sight because there's and let me just pull it up so this is a stack overflow question that's saying get all the unique values in a JavaScript array removing duplicates where ours is actually a lot simpler than that because there's only one value in there that is is unique but if we look at their solution they're using filter and this will take in the current value the current index and the array itself so if we say array dot index of value is equal to that to that index that means it's the only one in the array the issue with this though is index of has to search the entire array to find that index and this is getting called inside of a filter which is essentially like a for loop so you have a loop inside of a loop and so for really big arrays this can actually take quite a long time from or as we're if we look at these solutions this solution stops going through the array the moment it finds the unique one and that's because of this early return inside of the for loop here so like in the worst case we have to look at the first sorry in the in the in the best case we look at the first three values in the array and we're done and there's no like nested looping there this one is is very similar it's just a bit more logic but there's no nested looping so if it's at the beginning of the array we immediately found it otherwise we iterate until we find it so it's not doing any extra iteration this one has some nested looping hidden in plain sight so the sort function typically depending on the implementation ask to compare one value to another so this might actually do some nested looping so even though this solution looks very simple under the hood this might actually take a lot longer because it has to sort it first whereas with these two solutions it's just iterating through the array and then stopping the moment it finds the the unique one in kill him liam is saying could you try the test case like that that is not what this problem is solving so they're asking for the test case with this this problem does does not solve out this this problem is specifically for arrays that only have two two different numbers inside of them not in this case there's three different numbers we would have to write a totally different like if there were if we're if we're truly trying to find the unique one and there's more than one my first solution would actually I'd have to modify the entries length here but that one can easily be modified to pick out that the four is the one that only only occurs once but the other solutions were not meant to solve something like that cool all right I'm gonna move right along apologies to Shawn I still don't know what you're asking me to do I just don't see how like what so I think what you had before was like set a and set B maybe if I do with you just pass in the array twice about what we're doing here empty set about this one pass in the array twice yeah I don't know I don't know what you're asking me to do okay let's move right along I do need to submit a solution let's let's submit this one I like this one we do some basic checking otherwise we start iterating the array we don't have any we're not like sorting it or anything we're just iterating and then returning the moment we find one that is not unique go that worked we'll do an attempt naive final submission you it's going yep this person did the sorting method they're looking at the first three values in the array if those first three values are all the same then we know that any other value in the array is the unique one and that's what this is doing here this is doing that index of thing but this is actually two loops inside of a loop it's not a triple nested loop but it's still a nested loop which I don't like hmm cool I do need to copy the URL put in the readme there is our 6q all right for the 5q there have been there were two suggestions the one from beck was compared number you so we need to compare two numbers that are not integers so if a is less than B return the string less if a is equal to B return the string equal and if a is greater than B return the string greater but the numbers a and B you're given as strings just a second someone's knocking on my door you I'm back maybe I was just hearing something I don't know there was no one at my door okay cool hello Amy welcome in a stream um okay the interesting thing about this problem is that a and B are not numbers they are strings they can be positive or negative they can have leading zeros in the characteristic part and a trailing zero in the mantissa part I think those are actually math terms for the front and the back of a number yeah okay but let's try this yeah I think we'll do this one because it was suggested by Beck blue suggested this one max sub-array some so as someone in the chat suggests on this one earlier I felt like I've done subarrays some problem before maybe it wasn't called the same thing okay but let's try to do this one create new file I don't really know how I'm gonna do this I mean I think is that noise I do have a camera at my front door I'm looking at it now there's no one there I hear a noise though there's someone in my house but I'm live on the Internet and 36 people are watching someone will call the cops right okay now they're like the initial solution here which is to be to parse the thing into a number and then do the comparison but where that gets tricky is if there's very very large numbers like this because then JavaScript actually cannot represent those very large numbers so you wouldn't be able to compare them as numbers so that's that's where it's going to get tricky like check it if I say well like if we have this string startup Kaka and I say let's log that but if you put a plus sign in front of a string that essentially coerces it into a number so if I would a plus sign here look what we get we get scientific notation so if we're trying to compare the scientific notation like yeah let's let's just do this like for these really big numbers we're gonna log a we're gonna log B so for these two numbers in scientific notation it's just one e with thirty three zeroes however this one ends with a one and with scientific notation you lose what's the word specificity precision he lose precision so this would actually just say that they're equal when in reality they're not so this is the tricky part this is the tricky part of this problem the fact that you can't just turn that string into a number and do the comparison you must do something else which we will need to figure out amy is asking is really somebody in your house I don't know if they are they should come hang out in the basement and be on live stream I don't think they are I really don't know what that noise was them okay so this this is is is the dilemma what like wait let's let's do the naive solution first and then try to think about how how we can make it not that like how we can handle very very large numbers okay so let's say num a is going to be a turned into a number and num B is going to be be turned into a number and you can say if num a is less than B return less if num a is equal to num be returned equal and if num a is greater than num be returned greater there we go so it works wonderfully for very small numbers but the moment we try to do bigger numbers it's not going to work anymore like these yeah it technically it seems to work with the one that has leading zeros um but in both of these examples it breaks because of the large numbers so how are we gonna solve this which is telling me dude someone's knocking is it you twitch are you at my door really don't think there's anybody here you doo-doo-doo-doo it could be fireworks it's almost the fourth of July here and there's a lot of fireworks going off so that could have been it something okay one thing we can do though so I'm trying to figure out how do we do this comparison with very very large numbers we could compare each individual digit so if we know that these are both the same length then we just compare each digit and a moment one of the digits is longer than the other we found it and if you're comparing two numbers and they have the exact same length or they have different lengths the longer length is the bigger number because it has more digits I think that's fairly decent logic at least for that but then things get even trickier because we need to account for negative numbers decimals yeah you let's think on it catching up on the chat Tim is saying removed the zeros first compare lengths if different return much greater if saying compare each number and return one greater from left right that would probably do it but the tricky thing is when there are decimal places because these two technically have the same length but one's a decimal and ones negative so we need some custom logic there for these that would be easy enough because we just removed the leading zeros and we can figure out that that is larger um I guess if if a number does have decimals we can maybe store it as two different things so we have the whole number and then we have the decimal on the right yeah so we need we need a lot of custom logic here but let's let's do it okay so we we can't just turn them into numbers like this like this is the we'll call this the naive solution we can't just do this we must do better okay if the length of a and B are different and we need to do this specific to a because okay so we'll say if the link of a is longer than the length of B return greater if the length of a is the same as B iterate and compare each digit and that will determine if it's less equal or greater if the length of a is shorter than the length of B return less simple enough but now we need pretty much all of that extra logic to like remove leading zeros to handle decimals I guess I think we'll handle that next let's just get this working for the simpler cases and then we can try to handle decimals and negative numbers and stuff like that okay so we're just going to handle the simplest cases right now get that working and then we can modify to work for decimals negative numbers all that all that good stuff okay so say if a dot length is greater than B dot length we're done return grater if a dot length is equal to B dot length we're gonna need to be to do some comparison of each number we'll get to that and then another easy case here is if a dot length is less than B dot length we return yes okay so in the simplest case this returns the correct thing for for less that's great but now we definitely need to add the logic for if they're the same length we basically need to iterate and then the moment we find a number that isn't the same we've returned greater than less than or equal to okay iterate and compare each digit so we know the links are the same so we just need to start iterating create a four loop well I is less than a dot length we can say numb is going to be um a at I so we'll say digit a is a at I and digit B is be a tie and we compare them so we'll say if digit a is greater than digit B we return greater and like in this case we can see that it is returning greater the but that's because it compared the first two values a is greater so it returns greater awesome else if digit a is less than visit B we're gonna return less great but now we need the case where it's equal and ultimately if we get to the end of comparing each individual value and they were neither less than or greater greater than then they were all equal so if we make it outside of this for loop we can just return equal and that works cool now for these really really long numbers it should work except we we need to remove the trailing the the beginning zeros and the number so like this one should work and it does because this one is one less than the other and we're comparing each individual value so that's great and then this one works as well but this one won't work because we're comparing the length and we need to remove those beginning zeros for it to be accurate um you yeah so before we even do the comparison we need to remove those beginning zeros so I guess we could create an array and then remove the beginning zeros until we get to the till the first digit so like if we do a dot split a equals a dot split B equals B dot split function will still work we're basically just turning that string into an array but now that it's an array we can remove some values from it actually we don't we don't have to turn it into an array here's what we do say while a at 0 is equal to a 0 say a equals a dot slice one so that should that should remove the first the first letter in the array and then it's just going to keep going and then we can do the same thing with B I believe slice works on strings okay so if it removed all the leading zeros and actually let's just do this let's just log a right here we can see it is removing the leading zeros that's a good that's a good sign and now that we have removed the leading zeros from this one we get the right answer so that's awesome come so slice is actually a built-in function on strings that can get out a specific section of the string it takes a start number and an in number if you leave off the end number it'll just give you the rest of the string so by saying slice one we're saying give me the string at the first index to the end so it gets rid of the 0th index so these are essentially removing all of the the leading zeros so that's great okay so we've handled the really big numbers this should work this special case here because we remove that leading zero and then we compare each digit this is where we're going to run into problems so now what do we do about decimals and negative numbers I don't know yet this one should work it does because we removed the leading zeros everything works just fine this one will not work because we are not accounting for the decimal that one won't work all of these all of these have decimals so let's let's handle decimals first okay so think we'll we'll remove all of the leading zeros that's great but now we'll see if if there is a period anywhere then that means there's a decimal place and I actually want to store the decimal in a separate variable so let's do this let's say but a decimal equals nothing and B decimal equals nothing men we can say let a decimal index will be a dot index of a period so find the index where there is a period and then we're basically going to create two strings out of it everything before the period will get stored in a and everything after the period is going to get stored in a decimal so you can say if a decimal index is greater than or equal to zero so is a not equal to negative one so index of will return negative one if it doesn't find a period so if we find a decimal we need to split this number up so we need to say a is going to be a dot slice from zero all the way up to the decimal index and then a decimal is going to be a dot slice from the decimal index plus one all the way to the end so here if we log a and log a decimal um let's see what we get so like in this case we should get a is a thousand a decimal is 0 0 so a is a thousand that's good and then a decimal is nothing so this didn't quite work what if I do I think technically that should work Isak coming up back is nothing you you see this oh I need to I need to grab the oh no I'm not yeah cuz I'm not okay I need to get the decimal first because otherwise I'm modifying a like that okay cool so grab the decimal first and then remove the decimal from a so it's still stored in the original value but now we split it up so we have our a value here and then we have our decimal value and pretty much if both have a decimal we can just recursively call this function to compare the two decimals so like if if the number is completely equal except for the decimal then we compare the decimals otherwise we don't even have to compare the decimals because we compare the number before that okay so that's great so that works we just need to do the same thing for B okay and if we log B and log B decimal that seems to work so now we are separating the number from its decimal and now really we just need to compare it so like in the case of this one has a decimal one doesn't let's let's let's handle this case right now it's returning equal but we're still not comparing the decimals what is the same Claire did value but never right that's fine okay so basically now that we're pulling off the decimal we need to handle it in all three cases so well not really no we only need to handle the decimal in the case where the length of the two numbers is exactly the same because if we get to this point right here where the entire number is the same now we need to compare the decimal place so here we'll say if a decimal for B decimal so if either of the two have a decimal then we need to compare it otherwise we're going to just return equal so we should return compared with compare the two decimals a decimal and B decimal and it should work exactly the same way because it's going to remove the leading zeros and just compare the two numbers and we should get the same result so yeah let's see and now it's working so this technically and we can we can see it right before he gets called let's log the a decimal and will log the B decimal so in in the case where we're calling it with 1,000.00 and just 1000 the a decimal is 0 0 and the B decimal is just nothing and then when we go in to compare those it's gonna call this function again first it removes all the zeros from a so now it just becomes the empty string and then because they have the same length and they're both empty we just return equal so that's great but if we see for example like this case where we're comparing 0.09 and point oh eight it actually works but we can see that when we get here let's comment out the other one so the value 123 is exactly the same then we need to compare the decimals so we just compare 0.9 or just 9 and 8 it goes through all the same logic it removes the leading zeros and then that ultimately is what we use for the comparison so that's working beautifully I think the only thing we need to handle now is negative numbers so what do we do if there's a negative number I'll figure that out I'm gonna go to the heat to the chat peejoe hello P Joe it's been a long time says you could check what the number conversion turns each number into if its scientific notation use the link solution else do the greater list actually I kind of like that yeah so like if the number is way too big then we need to do this comparison otherwise we can just do this yeah I don't know the the thing that is though if you're comparing like a smaller number with a very large number you have to use this method anyways or don't you know K yeah because then you're then you're technically using the length property yeah hih tim has a good suggestion we can just replace all instances of zero at the beginning of the string with nothing let's see hijos saying if number is greater than max safe integer or number is less than min safe integer used a yeah we could probably do that hello tiger welcome to this dream huh tim has a suggestion for splitting it on a period and then using key structuring we could we could technically do that right here and we don't have to do the the slice thing let's see what we would get here so like I would say a-and a decimal equals a dot split yeah we do get undefined a lot um but technically we could we could default it to the empty string like that yeah this is a way easier way of writing it we do the same thing for B decimal yeah and so and we need to store that beep like that the reason I'm not putting a letter a Const in front of this destructuring is I actually want to reassign a here to be a itself efforts been split and if I put a Const there then it would try to read it clear the variable a and that's why I have to declare the variable before I do the D structuring so that should still technically work looks like it does all our decimals that's cool I like that peejoe says is this one of those where you submit 50 lines that someone has a once online solution I don't think I don't think so like well well Arthur and other than are they're newer number primitives built into JavaScript that can handle really really large numbers because if there are then probably just use that and that's like a four liner I don't know let's handle negative numbers now um I think we could do it so I wonder is the negative number ever embedded after the leading zeros it's not it's it's always at the front so we can do a simple check right here we could say if a at zero is equal to the negative sign and B at 0 is not equal to the negative sign and we need to return automatically less because the AE is negative and the B is not negative so we know that a is automatically less easy enough then if B is negative but a is not so the reverse of this but we reverse it and we return greater than easy enough so let's see like this one the right hand side is negative we instantly know that it's greater or this one the right hand side is negative we instantly know that the left hand side is greater or this one the left hand side that is that all we need that can't be all we need and then this one the right hand side is that can't be that can't be all we need so this seems to work because it's comparing when there there are different signs but what if we are comparing these two yeah so if we're comparing two numbers of the same length but they're both negative then we need to flip our answer so so here we need to say else if B well a at zero wait yeah so here's what I'm thinking if they both have a negative sign we're gonna remove that negative sign because afterwards we removed like the leading zeros and we're gonna have a variable that says like flip result because if they're both negative then when you compare the two numbers the larger one is actually less because it's more negative so that's that's what I'm gonna do so if they are both negative say flip result equals true and we're gonna default this to false and then we need to remove the negative sign from both a and B and then we'll go we'll go about our day we'll do the exact same comparison but at the end instead of returning less you return greater and instead of returning greater we return less okay and this is going to remove the negative sign from them but now so if flip results been less else greater you I think this makes sense I think it's the easiest way to handle it because then we can just do our same old comparison but we know that it's a negative comparison so we actually flip the result right like that and it seems to work so the the one thing that I was concerned if whoa I'm still broken you wait wait wait yeah digit a okay oh it comes down to the decimal I need to pass in flip result to the comparison here because it when I'm comparing the decimal place it lost track that it was you here we go and we default this to false but now when we recursively call it if it was flipped we passed in true and it will stay true and now that works this is a whole lot of code this is too much code but um I think I think we did we kind of broke it down we did it like one step at a time and it seems like we can handle most cases like what about that what about a negative point twelve yeah I think we've done it I think we've done it oh and earlier I mentioned that there might be like a native really large number in JavaScript but P Joe is saying big it might is still in like the spec yeah so if we compare these now without their minus signs this one's greater and this one's less but if they have yeah this one is greater than that one but if they both have a negative sign then they are both then the one on the right is less yeah what about two negative numbers like two negative whole numbers we could try that here like if we compare negative 1,000 one and negative 1,000 negative 1,000 one is less than negative a thousand because it's it's more more in the red anything else okay I'm gonna I'm gonna try to submit this on code Wars and see what happens it seems like we've handled most of the edge cases you Oh if there are leading zeros in the decimal I think that'll mess us up I don't know sample tests it passes let's just let's just do an attempt maybe maybe they didn't oh no oh no um so - 2 out of 130 failed let's log what the numbers are you you ah so comparing 2.40 and 2.4 messes up because then we compare 40 and 4 so probably what I need to do is when we go we get to the decimal comparison right here is we need to remove the trailing zeros before we do the comparison and then in this case you expected equal instead got greater these two are not equal not even close what that's that's just wrong this number is greater than that number right am I missing something but I do need to handle the decimal case so let's just say I kind of need to do the reverse you need to do these two but in Reverse so we'll say while a at a dot like minus 1 is a zero and we're going to remove the trailing zero oops okay we haven't broken anything but this is the this is what I want to check um so this should actually come out as equal but it's not because it's when it gets to the decimal it's comparing 40 and 4 and the code I just wrote is not working oh I need to use a decimal in B decimal not a you and now they're equal cool I don't think this would always work but it doesn't seem like they're passing that into test here what's failed wait did I not paste it in yeah there we go tipped okay one failed and I'm pretty sure that the tests are broken because I'm gonna I'm gonna log the failing test here and you will see that one is greater than the other where's the failure waitwhat waitwhat expected greater instead got less oh is that this one this one yeah and so this this is I think this is what I was worried about so we're comparing 81 and 701 when really we should be comparing 810 and 701 because it's the decimal place and yeah so in this case do I want to remove the trailing zeros in this case I want to make them the same length so I'm gonna Pat it with zeros if they're not the same length you yeah yeah this is a whole lot of code there's too much code there's definitely a better way to do this I just want to submit it so I can see the solutions and see the better way to do it what okay this code right here is removing the trailing zeros but before we compare the two decimals we want them to be of the same length but then that gets tricky if the decimal starts with a zero how can we do this you I think I think I pad it with zeros but yes I technically yeah Pat it with zeroes because if I end up trying to compare point zero zero zero zero zero zero zero zero one with point one if I pad point one with zeroes point one is larger than point zero zero zero zero zero one well right now I'm removing the leading zeros so to set them equal to okay I need to pad one of the numbers so that they are the same length yes okay if a decimal length is less than B decimal got length my ID dip at it so while a decimal length I don't even to do it need to do an if I'll just do a while is less than B decimal lake a decimal plus equals zero and then same thing with B decimal so we're gonna Pat it with zeros so that way when we compare it the decimal values remain not the same but are what they should be nice it worked so we pad this with a zero and technically we're now comparing 810 with 701 and this actually is greater cool but in the other scenario that I was talking about where we have like point zero zero zero zero zero one and point one this is less because this gets padded with zeros to be up to that that length are we removing the no we're removing trailing zeros first and then making them the same leak okay I think I think this is the winner this this is for all the marbles here we go go we did it okay let's see how other people did it are there solutions are there solutions as verbose as mine this isn't as verbose but it's really hard to tell what this is even doing okay I see so that they were doing what I think P Jo suggested earlier so if the length of the number is more than ten digits then you got it you got to do the manual stuff otherwise you can just compare it like normal so turn them into arrays check the length or get the longest length and then we're going to iterate the longest length you weird so this okay get this gets the length of the longest one this isn't handling negative numbers or decimals you this is my solution you yeah I wonder what my solution showed up there weird everyone's code is really hard to read like I don't know if this is true or not but I at least try to write code that if I start reading it from top to bottom I have a general idea of what's going on whereas something like this I really have to parse each line so we are replacing all the zeros and then this is a mess is this doing yeah this is doing something with the decimal place IRA I can't I don't know that whatever it would take me a little bit longer to figure out what that regular expression is doing let's look at this one okay so yeah they did the same thing we did where you compare if you see a negative sign you instantly know which is which oh this is clever instead of instead of having that variable that we have which is flip result they literally just put be inside of a and a inside of B so that you don't have to flip the result that's pretty cool hey they did the very similar thing so they have the the decimal variable here just from splitting and they are using D structuring so that's cool this is creating leading zero is it leading zeros yeah this is putting leading zeros on both a AC and bc to make them the same length putting trailing zeros on the decimals to make them the same length and then they do the comparison WowWee okay that's cool um this is a rough this is probably the roughest episode of code Coty's that has happened yet I've just been totally out of it we did it though it's a whole lot of code but we did it yeah yeah PJ was saying people are more into being clever in these so they look he says stupid I mean I think they just look like hard to follow I don't know cool I'm gonna go now um I am so out of it this was fun though we solved some Coty's we wrote a lot of code was it the best code maybe not was it code yes it was this one was fun this one was fun we did some object-oriented programming cool all right I'm done now I am gonna stream again probably like two more times this week I need to I need to figure out my schedule but streams incoming I'm definitely gonna do an episode on deploying with now version 2 I'll probably do that maybe Friday and this weekend I have a special surprise not a special stream that is leading into a stream that I'm gonna do next week we'll talk more about that on either Saturday or Sunday watch out look at the channel to see when the upcoming streams are I will try to have more sleep before the next episode so I'm less spicy but look at my channel you'll see any upcoming live streams as I schedule them yeah thanks everyone for tuning in last comment from PG PG oh those nested turn Aries are gross to me yeah I I'm guilty of writing code with nested turn Aries and ignoring my linter which tells me not to nest turn Aries only because I don't want to write it out and I'm the only one that's gonna look at the code but I know exactly what you mean cool um yeah that's it I'll push up all this code to github thanks to BEC for making the suggestion on fine unique that was super fun and we'll see you soon so uh wherever you are in the world have a wonderful morning afternoon evening or night until next time here's this you you
Info
Channel: Coding Garden
Views: 2,110
Rating: 5 out of 5
Keywords: html, code wars, backend, frontend, learning, learn node.js, learn to code, quokka, coding, node.js, live streaming, lesson, frameworks, vscode, full stack web development, debug, how to code, learn web development, katas, educational, code, programming, mechanical keyboard, learn javascript, debugging, live coding, beginner, full stack javascript, javascript, wars, css, vs code, web development, full stack
Id: JPXei00o3Ig
Channel Id: undefined
Length: 147min 50sec (8870 seconds)
Published: Wed Jun 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.