LongCut logo

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

By Coding Garden

Summary

## Key takeaways - **Find two lowest positive integers efficiently**: To find the two smallest numbers in an array, iterate through the array once, keeping track of the smallest and second smallest numbers found so far. Initialize these by comparing the first two elements, then iterate from the third element onwards, updating the smallest and second smallest as needed. [05:57] - **Mask sensitive data with '#' character**: To mask all but the last four characters of a string, iterate through the string in reverse. For characters beyond the last four, append a '#' to the result; otherwise, append the original character. Ensure the resulting string is built in the correct order by prepending characters. [26:45] - **Format array of numbers into a phone number string**: To format an array of 10 digits into a phone number string like '(123) 456-7890', use template literals. Embed slices of the array for the area code, prefix, and line number within the desired string format. [48:46] - **Find array index with equal left and right sums**: To find an index where the sum of elements to its left equals the sum of elements to its right, iterate through the array. For each index, calculate the sum of the left subarray and the sum of the right subarray. If they match, return the current index; otherwise, continue. [01:08:51] - **Determine palindrome chain length**: To find the number of steps to reach a palindrome by reversing and adding, first create a helper function to check if a number is a palindrome. Then, in a loop, if the current number is not a palindrome, reverse it, add it to itself, increment a step counter, and repeat until a palindrome is found. [01:26:37] - **Convert integer to Roman numeral**: To convert an integer to its Roman numeral representation, create a lookup table with Roman numeral symbols for key values (e.g., 1, 5, 10, 50, 100, 500, 1000, and their subtractive combinations like 4, 9, 40, 90). Iterate through this lookup in descending order, appending the corresponding symbol and subtracting its value from the input number until the number reaches zero. [01:44:36]

Topics Covered

  • Summing the two lowest positive integers efficiently.
  • Masking sensitive data with a simple string manipulation.
  • Formatting an array of numbers into a US phone number string.
  • Finding an array index with equal sums on both sides.
  • Converting numbers to Roman numerals using a greedy approach.

Full Transcript

[Music]

hello friends welcome to coding guard

with CJ tonight's episode code Coty's

number five well we're gonna break

things down solve these as you solve

these cards you do this welcome to the

stream if you're watching live please

throw a message in the chat this is the

show where I solve code Coty's live in

front of you I have a github repo for

this if you go to github.com slash

coding garden slash code - Coty's all of

the previous episodes are there and I

upload the code that I write at the end

of every episode if you're not familiar

with code Wars it is a website where you

can practice coding and learn how to

solve problems in coding they have

various problems with various skill

levels I've picked a few out tonight on

the github repo there is an issues page

and if you want to suggest a kata for me

to do you can go into that suggestion

issue and just add some links in here

and I'll try them out on the next

episode if you are watching live and you

do have suggestions go ahead and throw

them in the chat and I will give those a

try as well potentially probably I will

yeah I will so suggest them I have a few

picked out but if any are suggested I

would take priority over those and the

first one I'm gonna do is a seven Cayuse

sum of two lowest positive integers then

I will do another seven kayu credit card

masks I've picked out a six Kai you

create phone number and actually I

learned that I'm pronouncing that wrong

it's Q not Caillou Q and then also a six

Q equal sides of an array and then a 5 Q

palindrome chain link

then a 4q Roman numerals encoder so

these last few are can be pretty tricky

but I'm excited I'm prepared to do them

let's start with a fairly simple one the

sum of the two lowest positive integers

so this wants us to create a function

that returns the sum of the two lowest

positive numbers given an array of

minimum for integers no floats or empty

Rays will be passed for example when an

array is passed like this the output

should be 7 and then in so because

that's 5 plus 2 the two smallest numbers

and then in this example the output

would be this large number because it's

10 plus this number so let's try it

okay just get right into it

okay so one thing well really the

problem that we're first solving is to

find the two smallest numbers so we need

to find the two smallest numbers there

are a lot of different ways to do it

there's even a way to do it like built

into JavaScript for the most part you

have to do something a little a little

bit special but I'm gonna do it manually

first and then I'll show how we might do

it using a built in way in JavaScript so

one way we can do this is I'll say look

at the first two numbers in the array

and and really we want to two variables

so a place to store the smallest number

and then a place to store the second

smallest number and so we have those two

variables and we want to look at the

first two numbers in the array and set

the we'll say compare not look at so

compare the first two numbers in the

array and set the smallest number

variable and the second smallest number

variable so basically we start off and

at the beginning of the array we're

gonna take those two numbers and just

assume that they are the two smallest

ones and then we're going to iterate

over the array so we'll iterate over

numbers and we're going to start at the

second index so the third place in the

array because we've already looked at

the first two and now we just want to

compare those two with the rest of the

values in the array so iterate over

numbers starting with the third number

we'll compare with the smallest if if

here we go if the current number is

smaller than the smallest number then we

want to set the smallest number to be

the second smallest annum smallest

number and set the current number to be

the smallest number because basically if

we found out that it's even smaller than

the our first one we know that we can

just get rid of the second one that

we're storing right now so we basically

take this new number put it as the first

lowest one and then move that one up to

be the second lowest one yeah and then

else if the current number is smaller

than the second smallest number then we

will set the second smallest number to

be current number okay and that should

do it because by the end when we reach

the end of the array we've compared

every number with our smallest numbers

and we should have the the two smallest

numbers and the last thing we need to do

is return the sum of the two smallest

numbers okay let's take this logic and

turn it into code so first thing is a

place to store the smallest number so

we'll say let smallest equal and

actually we can I guess we could

initialize these as the first two in the

array

I guess my let's follow my logic that I

said here so we'll have let smallest and

then

have a variable tour the second smallest

we cannot start a variable with a number

so I'll say second smallest and then

compare the first two numbers in the

array set the smallest number variable

so let's say if numbers at 0 is less

than numbers at 1 then we'll set the

smallest equal to the first number in

the array else will set the smallest

equal to the second number in the array

and we can go ahead and set the second

smallest as the other one so numbers at

1 and then this would be second smallest

is going to be numbers at 0 and this

else statement will just work because if

it wasn't less then that means it's

either equal to or greater than and for

the case where they're both equal it

doesn't matter which one is the smallest

or the second smallest cuz they're both

exactly the same ok so we have now

taking care of the first two numbers in

the array we know which of those two are

the smallest now let's iterate over

numbers starting with the third number

so I need a for loop I'll say let I

equal 2 because I want to start at the

third number in the array and I'll say

while I is less than numbers like and

then we'll increment I

okay if the current number is smaller

than the smallest number so let's create

a variable for the current number and

that's just going to be numbers at I and

then they'll say if the current number

is smaller than the smallest number so

if current number is less than the

smallest number then we will set the

smallest number to be the second

smallest number so basically the

smallest number is going to shift up so

second smallest is gonna be the smallest

number and the set the current number to

the smallest number so now smallest

number is the current number you know

when you repeat a word over and over

again it starts to sound weird smallest

that's a weird word okay

so we have now taken care of we just

looked at this number and we set it as

the smallest if it is the smallest else

this number might be smaller than the

second number so let's check that so

else if the current number is smaller

than the second smallest number

and I am using a weird variable there

this should just be smallest so if

current number is less than smallest set

the second smallest to be the smallest

it's like the smallest number to be the

current number if the current number is

less than the second smallest number

then we'll just overwrite the second

smallest so now second smallest will be

the current number and if we do this for

the whole array we should have the two

smallest numbers in the array so we want

to return their sum so return smallest

plus second smallest and let's just log

out our smallest and second smallest

number to see what we get

okay let's so I press command S and it's

safe so that seems to work if I run the

sample test it works and we can see for

all of these sample tests it found the

two smallest numbers and yeah it seems

to work so here's what I want to do I

want to rewrite this using some built-in

methods in JavaScript instead so I'm

going to create this locally so we can

run it a few different ways let's call

this small this what is this called sum

of two lowest positive integers let's

call this sum of two lowest that jas so

we'll keep that function but let's write

it again using built-in method and why

is there a semicolon there shouldn't be

okay so built into JavaScript is this

function on math called math dot min and

this accepts two numbers or X actually

it accepts any number of numbers so one

thing we can do is we can't just pass in

numbers because numbers is an array so

one thing we can do is we can call or

apply this array of numbers as arguments

to this function so if I do math dot min

dot apply apply the first argument is

the this argument we don't need of this

argument and then the second argument is

an array of all the arguments we want to

call a math dot min width so this will

give us the lowest number in the array

so let's say smallest is going to be

math dot min dot apply null and numbers

and let's just log that out

and then let's test it with these we'll

just you have console dot log of those

but we should see the smallest number

logged for each of these arrays let's

see what happens so five is the smallest

number in this array two is the smallest

number in that array and so this is

essentially taking this array of numbers

and calling it with math dot min then we

would want to do that again but we would

want to do it with the array without the

smallest number inside of it so what we

could do is we could say second smallest

could be that same thing but applied

with the array that doesn't have that

value inside of it this is my break

timer let's take a quick stretch

so second-smallest basically we want to

take the number out of that array so we

could really just filter it so we could

say math dot min not apply and we want

to take numbers and filter it for any

number that is not the second smallest

number actually this might not work if

it has the same number twice let's let's

see what happens though so let's do

where num is not equal to smallest and

so this will give us an array without

the smallest number in there and then

apply it to math dot means so let's find

the smallest and the second smallest see

what this does for us so five and eight

two and four three and seven one and

twenty three four and twelve so that

seems to be working and then we just

want to return their sum so if we try

that thirteen wait what is this logging

oh yeah so it's logging what is my some

what's the expected some so that seems

to work let's try this on code words

maybe they pass us in an array that has

two numbers that are the same and it

might not work so it works for the

sample test let's do an attempt yeah and

then it seems to fail let's log those

smallest yeah I expected 24 got 28 my

guess is that we are pulling out the

same thing twice and we only want to

pull it out once so let's see how we

could do that really we could just do a

for loop I'm writing that right now I'm

not thinking of any built-in methods to

pull it out there is like some every one

thing we could do is say grab the index

like the first index so we can say

smallest index

is numbers dot index of numbers so

certain of smallest and that will give

us the first index in the array and if

we pull that index out so if we splice

it out

then we would not get rid of both of

them so this this should work I think it

said in the description though that

we're not supposed to modify the array

hint you not modify the original array

so here's what we can do we can say

numbers dot splice splice we'll create a

copy of the array so copy the array and

then slice out the smallest index and

one of those so this will give us an

array with out that smallest number but

if it was in there twice it'll it'll

still be still be in there so let's see

if this works for this attempt Oh No

let's see what I did

oh you know what I need to I need to

store this in an array because that

doesn't actually return the array so

let's say numbers without smallest is

that and then we'll pass that into

second smallest and if I run this

locally infinity what let's see

I believe splice makes a copy let's

double check this splice removes

elements from an array and if oh I mix

them up it should be a slice to make a

copy and then splice to pull it out okay

that should work there we go

and now if we try this on code Wars see

what it tells us that broke what let's

try it with the sample tests it breaks

there - was I not getting the right

number locally okay smallest is the

minimum of the entire array the smallest

index is going to be the index of that

smallest one and then we're gonna splice

out that smallest index actually that's

why because splice doesn't return an

array so first copy the array then

splice it out because splice modifies it

that that was my issue so you can't

chain those because splice doesn't

return a new array it modifies the

existing already okay there we go

that does it now let's try it on code

Wars sample tests that works attempt

that works cool one last way I'll show

this is instead of doing apply es2015

introduce the spread operator and that

makes it a little bit cleaner so instead

of doing apply we can just say dot dot

dot

numbers and what that does is it spreads

the array as parameters to call the

function so this will work in exactly

the same way but this is just new fancy

syntax that doesn't require as much

we're not we don't have to think about

what is the this value and are we

applying it we're just saying take this

array and call this function with all of

the things in the array as parameters

and so this should work the same way

cool and I think I'll submit this

solution the reason I like this solution

is because it's a single iteration of

the array we start at the beginning and

then we get to the end and we have found

the two smallest numbers this is

multiple iterations so this is one

iteration to find the lowest number and

then this is one iteration to find the

index this is an iteration to copy it

this is one more iteration to find the

next number so this even though this

doesn't have as much logic in it it is a

lot more iteration of the array so I'm

gonna go with this first one that I

wrote and submit it attempt attempt

submit final

I think that worked running running I

think we need a sound for this we did it

cool oh here's some other solutions I

didn't think of so here's what they did

they sorted the array and then returned

the two lowest the two first numbers in

the array after it was sorted that makes

sense

this depending on what sort algorithm

the JavaScript engine use could actually

well like will perform not as well as a

single iteration because sorting at best

is in log in complexity in times log in

whereas this solution is just order in

we iterate once over the array and we

have the the two smallest numbers and

this is like one iteration and some

changed and depending on the sorting

algorithm could be N squared meaning it

has to have like a nested for loop okay

this is a similar solution but instead

of setting it to the two first values in

the array they set it to like the

maximum possible integer in JavaScript

cool

so sort the array grab the first two

values and then reduce that to get it

some these are all clever mine is the

most performant all right let's keep

moving next one is credit card mask Bret

welcome to the stream

all right usually when you buy something

you're asked whether your credit card

number phone number or answer to your

most secret question is still correct

however since someone could look over

your shoulder you don't want that to be

shown on your screen instead we mask it

so your task is to write a function mask

fi which changes all but the last four

characters in into

pound or hash so if we pass in this we

get everything except the last four

numbers and yeah let's do it and they

can pass it in things that aren't even

the aren't even just like a credit card

number it looks like they're passing in

literally anything and you just want to

keep the last four numbers as not being

hidden hey Bob welcome to the chat

welcome to the stream okay let's do this

so here's what I'm thinking we could and

X are we getting in a string or we

getting in an array yeah we are getting

in a string that's fine so here's what I

want to do I want to iterate over the

string in Reverse so start at the end

and go to the beginning so iterate over

the string in Reverse if the current

index and actually we'll need a place to

store the mass number so a place to

store the masked number if the current

index is greater than wait we'll have to

do a little bit of math because we're

going in Reverse but if the current

index is greater than length minus for

app in the number to the mask number so

if we're so iterating in Reverse if

we're at any of those last four numbers

I might have to I'm gonna have to work

on this math right here but if we're at

any of the last four numbers then we

will just up in the numbers themselves

or letters whatever they are in the

string to the mask number otherwise

just a pen a mask and that should do it

we will return the mask number okay

let's give it a try

so say let mask be equal to an empty

string so it starts off as nothing and

then we're gonna iterate over the string

in Reverse so I'm going to say for let I

equal the length of the input minus one

so that will be the very last index of

the input and we'll say while I is

greater than or equal to zero

i minus minus so this is a for loop but

it's going in Reverse okay if the

current index is greater than length

minus four I think this is the right

math because it we're going to length

minus one yeah and we would want to do

minus three maybe needs me at minus five

let's let's do it okay so if I is less

than or sorry is greater than the length

minus four and let's let's think about

this before we just throw numbers into

it

I got a whiteboard let me let me write

this out so this

okay

so let's say we're given the string like

1 2 3 4 5 6 7 8 9 the length of this is

9 so length is 9 and we want to go all

the way to here without setting it as

masked so if length is 9 we want to go

to index like what is my logic length

minus 4 so 9 minus 4 would give us 6 9 8

7 6 5 5 I can do math I think so while

the index is less than 5 so if we go to

0 1 2 3 4 5 that's pointing at 6 and

this is I equals 5 so if we go to here

we actually want to go minus 5 I think

because if the index is less than this

number that's when we want to start

doing masks so this actually needs to be

minus 5 because that will give us index

4

so this is where we want to start the

mask so we say this will be index nine

eight seven six and that's less than

five but then when the index is five

that's not less than five so we do mask

that's what we want mm-hmm okay so minus

five then we're gonna pin that number to

the mass number so we're gonna say

masked plus equal and let's just store

that not the number the letter sorry

because it could be either a string or a

credit card number so let's store this

in a variable this is gonna be a c c @ i

and so then we'll just add the letter

itself two masked otherwise we're just

going to append a pound and then we will

return the mass number okay I think I

got it wrong I think my math might be

off oh no we're appending it in the

wrong direction so instead of appending

it instead of doing a plus equal we'll

have to do letter plus masked

because that will take whatever masters

and put it on the anyway no see so

expected this and stuff got this because

right now we're just appending it at the

beginning yeah we need to be appending

it at the end so this will take whatever

it is and put that value on the end or I

need to flip that I may need yeah I

think I need to flip it I guess I'm

thinking about it differently because

we're going in Reverse whoa whoa yeah so

this needs to be letter plus mask

so put that thing at the beginning of it

and that makes sense because this will

start off as an empty string and then

we're gonna put at the beginning of it

the last character and then then it will

be the last character and we're gonna

put the next character at the beginning

of that so that makes sense there we go

let's try it with more tests wait is

that gonna submit my final final don't

do that no I want to try it again can I

stop it well if if it submits I'm going

to redo it because I want to use some

built-in methods we could probably use a

reduce yeah let's use a reduce train

again okay so it's bring this down

locally credit card masks badge is so

that worked it's a single iteration of

the string let's try it again

but let's like let's let's try to use

some built-in methods because we are we

are doing iteration we're essentially

essentially reducing this input to an

input that has pounds inside of it so I

think we can use a reduce for this

actually you could probably use a map

because we could have this logic inside

the map to say which one to return and

we could do it on something reversed

let's see so let's say we first turn it

into an array so let's say let input

array this is going to be this split on

the empty string it's my break timer

it's like a quick stretch

so split on the empty string and then do

we want to reverse it

no I guess technically we could have

gone in the other direction as well and

just flipped our logic for the F so yeah

let's just do that and then we'll map it

this will give us the letter and a map

also gives you access to the index in

the array so we could have similar logic

so we could say if index is less than

and a map also gives you access to the

array so if index is less than array

length minus 5 that means we're before

the last few characters so we want to

return out otherwise we want to return

the letter and then we yeah so we'll be

iterating over and essentially all the

way until we get to the last four

characters we're returning a pound and

then when we get to the last four

characters we are returning the letter

itself and then we could just join that

back together on the empty string and

actually I'll rewrite this but instead

of going in Reverse we'll go in the

other direction because it should work

with the exact same logic so let's see

if this works

sample dusts off by one cool if we do

the attempt that works too

alright let's let's rewrite this instead

of going in Reverse we'll go in the

other direction so instead of doing a

reverse loop will do zero and then we'll

say while I is less than the length

increment I and if I is less than the

length minus four will add and actually

because we're not going in Reverse

anymore we can just do a plus equal and

then in this case we'll add the letter

itself and then we return masked so this

should work too oh are we not appending

Oh

this shouldn't be masked this should be

pound so we want to append the when

we're before the last four indices we

want to append a pound instead of the

letter there we go

attempt cool and all submit this one and

again I like it because it's a single

iteration this is three iterations we

have to split it turn it into our a

that's one iteration then we map it so

that's another iteration and then we

join it so that's another iteration

before when I submitted it I saw an

example of like regex and that's

potentially multiple iterations of the

string I don't know it's not maybe not

the prettiest code but it would be the

most performant code submit final

working working drumroll

it worked so let's see what this does

so a slice will yeah so this takes out a

[Music]

piece of the array copies a portion of

the array so if you pass in a negative

index it's actually gonna take away from

the back of the index so this is pulling

off the last four and then replacing it

with a pound actually so this is

replacing everything except for the last

word indices with the plough a pound and

then this is just gonna grab the first

four of the array itself

oh-ho interesting so this is a regular

expression that says it's zero more of

anything but these four dots somehow I'm

not the best with that regular

expressions but these last four dots say

somehow save those last four characters

and replace everything else with a pound

there's another regular regular

expression to do that

cool and so I see people doing this a

lot when they're working with strings in

JavaScript but because strings in

JavaScript do support the indexing into

them with brackets you don't need to

turn them into arrays so like in in this

case they're creating an array by to

split it so that they can iterate over

it and then they're replacing the values

inside of that array and then joining it

back together but this could have just

just as well been been done with just

string concatenation rather than turning

it into an array and then turning back

into a string hey Emmy they've been

going decent we were on number two and

we're about to move on to a little bit

more harder so this one was a 7q yeah

that was fun onto the next one

all right this one is create phone

number and it's a 6q write a function

that accepts an array of 10 integers

between 0 and 9 that returns a string of

those numbers in the form of a phone

number so we get an array and we want to

turn that into the format first three

numbers in parentheses space next three

numbers - last four numbers the return

format must be in the correct order to

complete the challenge

don't forget the space after the closing

parenthesis dadums

welcome to the stream awesome

are you the data that I think you are

even if you're not welcome to the stream

okay let's try it so let's copy our

example format

so ultimately we want to end up with

this yeah we could be very manual about

this I think I think I'll do that first

so we'll do like what if you didn't even

know what a for-loop was you could still

solve this problem then we'll maybe do

something with a four loop and then we

can probably do like a reduce or

something like that okay so let's just

say we'll have a place to store the

phone number that is the dad and I

thought it was awesome a place to store

the phone number and then we'll we'll

just index into the array and start

appending it so we will append a per in

and then append the first three numbers

in the array and then append closing

prynne and then append space not it not

app in space and then append the next

three numbers in the array and then

append a - and then append the last

three numbers in the ring

cool and then return the phone number

cool and let's just do this manually

first and then we'll use some built-in

methods to kind of help us out so a

place to store the phone number let's

just say let phone number equal an empty

string and we want to append first

parentheses how about we just start off

the phone number with the parentheses in

front of it and then we're gonna pin the

first three numbers in the array so

let's say phone number plus equal

numbers at zero numbers at one and

numbers at two then we will append a

parenthesis phone number phone number

append a parenthesis and a space we'll

just do that all in one go there and

then append the next three numbers so

this is gonna be three four and five

cool and then append a - so phone number

plus equal a - and then a pin the last

three numbers so this is going to be six

seven one two three four five six seven

oh the last four numbers sorry seven

eight

and number nine cool this is really ugly

we could make this look a lot prettier

but let's just see if it works let's

take a quick break

Kaizen in the chat says I need to do

some Coty's again are you getting rusty

with your programming how the heck did

that happen

let's let's see shouldn't have to wrap

this in parenthesis huh

because for whatever reason this is only

grabbing the first one for this line of

code

oh they're actual numbers so it's doing

math so we need to actually convert

these numbers into strings that's a

tricky thing let's do this yeah so

because these are numbers it's giving us

a single number that's actually them

added up so we could do two string on

each one of these and that way it won't

try to do math on us

actually there's a trick where I could

like make the first thing that it's

appending a string and then the rest of

it would become a string and not do math

let's see if this works and then I'll

try that and then we'll make it even

even cleaner so run the sample tests

there we go that's what we wanted

okay here's here's what I'm thinking

though so instead of doing this two

string thing actually let me save all

this locally so you can see the many

different solutions

so here what does this call create phone

number

so there's that it's like very manual

but if you know anything about coercion

in JavaScript if you have a statement

that starts with a string then

everything else after that becomes a

string I'm pretty sure let's try it but

here's what I'm thinking so instead of

doing this two string thing we could say

empty string plus those next things and

I think JavaScript will course

everything else to be a string and

append without doing the maths let's see

you can test it locally this yeah so

dadums is recommending a template string

definitely so we could use a template

string oh I see what you're saying

though yeah so instead of doing a

pending we could do a template string

that has each of the values inside of it

that won't do math absolutely

let's see if this works and then we'll

do that and then we'll do another thing

instead of having to manually pick out

these numbers we can just like grab a

copy of a portion of the array okay

let's try this that works and the reason

this works is because of coercion in

JavaScript so because the first thing

that JavaScript sees is a string the end

result is going to be a string and so

instead of actually doing math it just

depends these things it courses them to

strings and appends it so that's one way

cool let's do a template string like

data mess is recommending

so instead of doing this weird put a

string at the beginning let's put all of

this in backticks and then we'll bring

in each individual value so oh so I want

numbers at zero

I want numbers at 1 and I want numbers 2

- yeah so this is es2015 template

literal syntax so we're basically

creating a string that has these three

values in it so that won't try to do

addition on them and then we can do the

same thing for all of these we'll just

have to say 3 4 5 6 7 8 and 9

and this should do it too unexpected

token I have a extra parenthesis what is

that in the previous one

oh the previous one I still have that on

there yeah don't want that cool that

works well let's go even further with

this so let's use a template string for

the whole thing so instead of doing

multiple lines of concatenation we can

have one big long template string that

that does the whole the whole thing for

us so let's say we want a template

string that starts with a parenthesis

followed by a space followed by these

things followed by a dash followed by

the last four numbers what just happened

what are you doing BS code yeah I don't

do that oh I copied the copied the the

backticks I don't want the backticks I

just want that cool so it's kind of hard

to read cuz it's all on one line but

let's do this basically we have created

a scene a single template literal that

does what we want so the first three

numbers are wrapped in parentheses

followed by a space next the numbers -

and then the last four numbers so let's

see if this does what we want there we

go

alright the last cleanup of this since

instead of having to pick out these

these three numbers individually let's

use a slice which will create a copy of

just a specific portion of the array and

put that in here so right here we want

to do numbers dot slice and well visual

studio code help me it usually gives me

a description here I'm gonna restart

visual studio code because it has

stopped being helpful go away

yeah so I just switched to Visual Studio

code a code probably like week we can

have a go two weeks ago and this this

seems to be happening to me all the time

all of a sudden my autocomplete will

stop working and hi syntax highlighting

gets really weird and then I have to

restart visual studio code I've never

had to do that with Adam

okay let's try so numbers dot slice well

maybe maybe it won't okay

but the start number and then the in

number is not inclusive I believe so if

we say three that will be in deceide -

yeah Kaizen and the chat says because

codes like no it's okay he knows what

he's doing yeah I mean I could code this

without all the autocomplete but I I

would prefer it and then we want to just

join that on the empty string so that'll

give us the first three numbers and then

we want the next three numbers so we'll

start at into c3 and we want to go to

Enda c7 because it's not inclusive and

then well we'll start at into Cee 7/7

yeah and then I think if you leave off

the last number it just says okay to the

end of the array let's see what this

does for us it works cool yeah I mean

that's a thing cuz it's not a bug it's

an AI feature um yeah so that works oh

one other thing we could do though is we

could we could iterate over the whole

array and say if we're at into c0 append

a parenthesis in front if we're a

tendecy 3 append a closing parenthesis

and a space if we're at into si

six append a - maybe we could iterate

over the whole thing there are a million

and one different ways to do this thing

let's submit

I think I'll submit it's a nice little

template string one we could clean this

up though because like this is like

really long I feel like let's create

like a one variable function that does a

join so let's say kotts J is a function

that takes in an array of numbers and

then just returns them joined on the

empty string so then we can make this

just say J

look at that okay J I should do it it

did it oh wait yeah I did it

cool let's we could make this even

smaller we could

let's take in the the beginning index

the ending index and the right so we'll

take in the array we'll take in the

beginning index the end and the end

index and we'll say in slice on the

beginning to the end and then join it

now I can just get rid of this slice so

J bet J bet J that will it work well

that's weird

oh we also have to pass in the the

indices so J with numbers 0 comma 3 and

then J with numbers 3 comma 7 and then J

with numbers comma 7 and this last one

should work because the third parameter

will be undefined switch to just give us

the end of the array yes

can this be a regular expression

definitely we could say well we're given

an input array is the thing so we could

turn yeah we could turn this into a

string and then use a regular expression

to turn it into a phone number let's do

that how long have I been streaming I

feel like I've spent a very long time on

this one close to an hour

that's okay let's do this with a regular

expression because that would be fun

so here's what we want to do we want to

say let's get rid of this comment let so

see you numbers join on the empty string

so now we have a string and then we want

to do a matcher so we want to match the

first three numbers actually I guess we

could do a replace and then our regular

expression is going to be find three

digits so let's just look up the regex

to do digits find a one digit number and

yeah so this will let us find the first

three digits and then I guess if we wrap

this in parentheses that should give us

a group and then we want to find the

next three and then we want to find

the next four so this is going to match

the first three the next three the next

four I'm not trying to do this with

JavaScript though but basically we want

to say like our resulting string is

gonna have this with like dollar sign

one space dollar sign - so like our

second group - our third group like that

do you know how to do this in JavaScript

Eisen

might have to look it up but because

basically we want to match those three

things and then pull out what was

matched for each and put it into a

string like that let's see if this works

I don't know if that's how you do

matters in regex with JavaScript whoo

then we'll just return this

it worked really that's surprising oh

cool but the way this works is we turn

it into a string we grab out add the

space in the dash and the regex yourself

I don't think so because the the thing

we're regex it over is just a string

that has all of the values in it so

we're not gonna find the parentheses or

the dashes in the string itself so what

I'm saying is find the first three

numbers find the next three numbers and

find the next four numbers and then

dollar sign one gives you access to your

first match dollar sign 2 gives you

access to your next match and then

dollar sign 3 gives you access to your

last match did this really work let me

comment out everything else and then run

my code that worked Wow Wow

I've never used a regex like that in

JavaScript I've done this a lot

just like regex seeing on bash using

said or something like that so that's

pretty cool let's let's do this one

Kaiser says Dane you cool so we'll keep

all of that we'll submit this one

hopefully it works simple tests works

attempt drum roll please

oh I got a submit final the comma thing

blook aizen's mine right now do you mean

the the thing on the right-hand side of

the comma like what what the thing will

become when we do the replace I can't

believe I've never done this in

JavaScript like this this this changes

how I would do a lot of things because

it makes a lot easier to do like find

this inside of the string and create a

new string that is like the result of

finding that thing okay submit final

drumroll please

[Music]

more general

cool let's see how people did it yeah so

instead of using C this is super

interesting like why would you turn this

into a string so that you can use

substring like why wouldn't you just use

slice because that's already built into

an array and then you wouldn't have to

turn it into a string and then oh wow

so we have our format we iterate over

the string and we were in so this this

is not not very performant because a

replace has to search the string to find

it so basically on every iteration a

replace by default will only replace the

first one so they're replacing one X at

a time with the next number cool and in

in this case the they used a regular

expression but since we already know

that everything is a number you can just

use a dot to match a single character

guys in with the champagne emoji in the

chat that's great

and then this is this is like the one

that we did cool all right on two equal

sides of an array I'm gonna take a quick

step back get ready for this one

and do some stretches or a quick I find

that if I just keep coding I'll get

exhausted and less energetic and less

entertaining okay you are going to be

given an array of integers your job is

to take that array and find an index in

where the sum of the integers to the

left of in is equal to the sum of the

integers on the right of in and this is

a 6q if there is no index that would

make this happen

return negative one okay let's say

you're you are given the array that your

function will turn index three because

the third position of the array the sum

of the left side of the index and the

sum of the right side of the index both

equal six see you later dad owns thanks

for tuning in let's look at another one

yes so basically you need to find the

index in the array where to the left of

that everything has those numbers summed

up has the same sum as the things on the

right so in this case index 3 is 0 1 2 3

so right here everything to the left are

the same numbers as on the right so

those have the same sum so your function

returns 3 because index 3 is where that

happens let's look at another one you

are given the array 1 150 negative 50 1

1 and 1 your function will return the

index 1 because the first position of

the array the sum of the left side so

index 1 is here the left-hand side is 1

that's the sum 50-50 one is a negative 1

plus 1 is 0 plus 1 is 1 so the sum of

this equals that ok last one you're

given the array this at index 0 the left

side is nothing and the right side is

that they are both they both are equal

to

zero the right side is that they're both

are equal to zero when added empty raise

or equal to zero and this problem so

wait what yeah the right side is 10

negative ATO negative 80 so the sum of

all of those things is 0 the left hand

side of this is 0 so you get back 0 it's

gonna be a tricky one okay

an input the input is an array of up to

a length of thousand so the output is

the lowest index where the left-hand

side is the same it has the same sum as

the right-hand side okay if you're given

an array with multiple answers return

the lowest correct index of the like the

leftmost index an empty array should be

treated like a 0 in this problem alright

let's give it a try it's gonna be tricky

all right and let's take like an example

already like this one peejoe welcome to

the stream we have been going for an

hour in 6 minutes but I have a few more

that I want to do and they're all not

that easy so so I might be stringing

streaming for a little bit longer but

welcome to the stream we're on a 6 Q and

I just realized I'm basically doing like

interview problems live on the internet

and having fun while I'm doing it

okay so given this array we need to find

the index for everything to the left is

that has the same sum as everything on

the right so one thing well here's what

I think we should do we should iterate

over the array some the left of the

index some the right of the index if

they are equal

return the index otherwise we just keep

going until we get to the end we'll

probably have to have a special case

where what if we get to the end and

nothing happened but what will account

for that so we need to do a sum I'm just

gonna write a simple little helper

function that sums up the array so let's

say Kant's sum is a function that takes

in an array and then reduces it and

we're gonna return some plus them will

that do it start the summit zero let's

just try our little helper function out

real quick sum 1 2 & 3 gives us 6 cool

here's what I want to do though so

instead of just passing in the array

what if we pass in the indices that we

want to sum inside of the array that way

it'll be really easy inside of the

iteration to just say start at index

plus 1 to the end and then start at

index minus 1 to the beginning to index

minus 1 so let's let's create a some

function that will take in the indices

also here I'm gonna do this locally

equal sides of the array

cool so we'll do this whoa not that this

okay so I want to create a some function

this is gonna be some sub array so you

pass in the array you pass it in the

start index and the end index and we'll

only reduce or add if well basically I

know I'm not gonna do a reduce because I

want I want a for loop that only goes

from start to end of the array so we'll

do a for loop well say let index equal

start while index is less than end cool

and we'll do a non-inclusive end and

we'll say sum it starts at 0 and then

we're just gonna add the current so

array at start will array at index add

it to the sum and then we'll return in

that sum peejoe is saying curry the

array slices to the sum method you can

do that in JavaScript I'm not even gonna

attempt that right now I'll get this

going and then we'll see what we can do

but if you're not familiar with currying

it's a functional concept where you take

a function that takes multiple

parameters and create multiple functions

that only take one parameter yeah check

that out let's keep going so this

function should take in an array and

then just give us the sum from a

specific index to another index great so

now let's iterate over the input array

so we'll do a for loop

radar length we're gonna some left of

the index and some right of the index so

let's say left sum is going to be and we

don't need the element and we'll say

some sub-array the left of the index is

going to be nothing so let's start at

negative one and go to well actually

we'll say start at zero plus the current

index because well know start at yeah

just start it start at zero because the

left of the other way is gonna be zero

and then you want to go up to the

current index so that's gonna be the end

and yeah so this should just give us

back zero because if we sum if we start

at zero and our end is zero then this

won't even go into the for loop cool so

the left of the array is up to zero to

the current index and then the right of

the array is going to be from the

current index to array link and now we

do the thing so if they are equal so if

the left sum is equal to the right cell

we got it and we will return the index

otherwise we keep going until we find

something and then let's see what it

says to do if there was no sum

cool

if there is no index that would make

this happen return negative one so if we

get to the end of this for loop and

nothing has happened then we need to

return negative one okay I think this

will work because this is summing the

left side of the array this is summing

the right side of the array at the given

index and if they're equal returns it

cool so let's see if this works and then

see if maybe we can clean it up or curry

it and there's there's chatter of

functional programming in the chat oh

don't do that

it's a cook break

okay let's try with the sample tests Oh

No expected three got zero

oh well it's returning zero every time

oh okay I think I think here's the issue

from okay let's let's figure out its

figure out our issue

we're always returning zero for some

reason so if the index is zero and we do

some sub array of oh this needs to be

index plus one because the right of the

array needs to start to the right of the

index kaizen asked in the chat what are

my thoughts on pure functional

programming and immutable data I think

my opinions have a lot to do with the

fact that the majority of the beginning

of my career was with object-oriented

programming and in object-oriented

programming you mutate state like that's

that's what object-oriented programming

is you create objects they have state

and then their state changes and they

interact with each other so I've always

programmed in that fashion I think

especially like two years ago I didn't I

didn't see as much of a trend this this

past year but like two years ago

everyone in the JavaScript community was

like everything should be immutable and

I think it has its place I I also think

that you have to take a holy dip totally

different mindset when you're

programming in that way like programming

object-oriented and programming of

mutating state is a different way of

thinking and solving the problem than

thinking in pure functions in a mutable

state personally I haven't done a lot of

it so that's why I don't do it that much

but

it seems cool I think one of the things

about functional programming and

immutable data is it is easier to reason

about your code because state isn't

changing you can also think of

everything as immutable immutable and

pure and so because state isn't changing

it's easier to reason about but like I

said I've been programming with mutable

state for almost all of my life and so

that's how I prefer to program simple

tests

oh no expected three instead we're still

returning zero why let's see so

oh we're not passing in the array oh

okay

the first parameter is the array then

the start index and then the end index

let's let's run these locally just to

make sure what did we call this this is

equal sides some assignment to a

constant variable yeah there should be a

lip ok it's working now

I should learn how to call my own

functions

I wasn't passing in the array or the

start of the end sample tests hey

alright let's do an attempt actually

what we'll see if we can clean this up a

bit yeah that worked I am proud of

myself

yeah and petros singing the chat it's

hard to really do anything in the real

world especially in api's with mostly

pure functions and the thing to think

about is like an API request has side

effects like that is not pure so when

you're building web apps you're dealing

with API requests so everything can't be

pure and what he's saying thought

process should be immutable first and

then mutate when needed

definitely okay let's try to clean this

up I don't know how we might can we make

this more functional can we potentially

use a for each instead of a instead of a

for loop it's it's tricky because like

right now that because I'm using this

for loop it will jump out of the for

loop anytime like the moment it finds

that index if I'm if I'm using any other

higher-order function it has to iterate

over the whole array so I like the fact

that this kind of does like a

short-circuit I'm gonna leave it as is

because I have some harder problems

coming up but yeah and an app I want to

build soon is basically where you the

viewer can share your code and like

maybe show different versions of how you

tried to solve this and potentially we

could like use it live so like if I'm

solving it and you want to share your

solution I could go to a page and like

see you coding live so that would be fun

to build drumroll

drumroll

one more why is this taking so

[Music]

let's see how they solve it so find wait

maybe this this maybe this kata got

renamed it I think it did it used to be

called find even index but they iterated

yeah and PG was saying so because when

if you're just joining we were talking

about like functional programming and we

could have used a higher order method

but the fact that I just want to be done

like I don't want to iterate over the

whole array I want to stop here a PJ PJ

was saying that's a good example of when

something doing something non

functionally makes a little bit more

sense in terms of like performance wise

because you don't want to iterate more

than you have to and I think that makes

sense let's see how they do this okay so

they started at index 1 meaning they're

going after the first index to the to

the end minus 1 so they're leaving off

the two last indices if you slice the

array from the beginning up to the

current index and remember slice is not

inclusive so if you put one in there

that's gonna give you a slice of just

the first value in the array then they

summit and if the right-hand side is the

same then they return that cool so

they're doing similar to what I did but

they're pulling out a slice of the array

and then running it through reduce

whereas I just used the original array

and did a sum sum a sum up to two

specific indices instead of pulling out

a copy of the array cool people own

people on code Wars like to use like

very small variable names I've found

it's interesting okay on to the next one

this one actually I think should be

fairly easy though number is a

palindrome if it is equal to the number

with the digits in reversed order yeah I

think I think this one should be pretty

quick and

in Roman numerals encoder will be

interesting it's a fork you okay number

is a palindrome if it is equal to the

number with the digits in reversed order

so for example 544 171 480 for our

palindromes because if you reverse them

it's the same number and 43 190 4 + 4 7

7 3 r-not palindromes write a method

palindrome chain length which takes a

positive number and returns the number

of special steps needed to obtain a

palindrome so the special step is

reverse the digits add it to the

original number if the resulting number

is not a palindrome repeat until you

find a number that is a palindrome and

the number of steps if the input number

is already a palindrome the number of

steps is 0 so you need to return the

number of steps it takes to get to a

palindrome by doing this reverse ad

reverse ad input will always be a

positive integer sort of for example so

87 is not a palindrome so you add it to

itself reversed 165 is not a palindrome

so you add it to itself Reserve's

reversed repeat repeat finally we get

484 which is 13 53 plus that reversed

and then the the return of this function

is going to be 4 because it took us 4

steps to get to that place ok let's do

it mm-hmm

and it's in hits so there's our example

so I think I I need a function that will

tell me if the given number is a

palindrome if it is we're done otherwise

flip it reverse it at it do the thing

and then check if it's a palindrome

again so first I need a place to store

the counts so how many times did we go

through this will have a function to

check if a palindrome and then we'll

have to write out the logic for that too

but then we will have a maybe like a

while loop so while the let's call this

our current num so while the current num

is not a palindrome we will reverse it

and add it so set current num2 the

reversed

[Music]

plus the current dump so that will

reverse it at it we get our current numb

well then check is that a palindrome if

it's not and inside of this while we

will increment the counts okay so

basically where where is the current

number of palindrome no it's not let's

add it to itself flipped increment how

many times we've done this is it a

palindrome no it's not so we'll just do

that over and over again and then lastly

we will return the cap okay so first we

need a place to store the counts so

let's say like let count equal zero and

then we need a current number I guess we

could just use in let's create a

variable for it so let current num equal

in and while the current number is not a

palindrome so I want to say while is

palindrome of the current number not

we will then say current number equals

reverse the current number and add it to

the current number reverse doesn't exist

I'm gonna have to create that and then

increment the count so like count plus

plus okay so first we need an is

palindrome function to check if a number

is a palindrome

and lastly will return counts so we have

a function is palindrome that takes in a

number and we want to make sure that the

number in reverse is the number itself

so millon in one different ways we could

do this we could turn it into a string

reverse it compare it to itself so let's

let's just do that it's easy enough so

return number is equal to number two

string and is there a reverse on a

stringer is that only on an array no so

we'd have to turn it into an array then

reverse it then join it why not

it's getting to the end of the stream

I'm getting tired I'm writing less code

so say two string dot split so turn it

into an array and then reverse it and

then join it back together into a string

that should do it that's a ton of

iteration but that should work there's

probably also some mathematical

operation I could do to check if it's

the same in Reverse that it is going

forward and really we could turn it into

a string and then start checking each of

the characters let's just do this it's

easy enough

so we'll say if the current number is

not a palindrome well then need to

reverse and so

and actually let's let's create a

function called reverse and we can

actually use that here so we'll say

reverse is a function that takes in a

number and does this but we want it back

as a number so we put a plus sign on it

that'll course it to a number so reverse

the the number given so turn it into a

string split it use the built in reverse

method on an array and then join it back

into a string then turn it back into a

number okay so that will turn it into a

number and wait no okay yeah and let's

actually do this too and so we'll be

able to say reverse the number send it

to a string so if the number as a string

reversed as a string is equal then we

know that it's a palindrome okay then

we'll reverse it which turns it into a

number and we'll add it back to itself

let's see what happens I don't like all

of this that's happening that really

will reduce performance but let's see

what we got so far

test passed if we do an attempt I wonder

if it's gonna try with like really long

numbers and then my solution will break

it worked okay here I'm gonna bring it

down locally and see if there's anything

we can do to make it a little bit nicer

so palindrome chain length

okay I wonder if there's a purely

mathematical way to reverse a number and

actually do I need to turn it into a

string when we compare it

no because 484 is equal to 484 so a

palindromic number is a number that's

just equal to itself right of course of

course so I don't even need to turn this

into a string and let's do a quick test

locally

so that's working cool yeah let's just

look is there a way to reverse a number

so math reverse digits and number number

theory how to reverse the digits of a

number is there a mathematical way to

reverse the digits of an integer

mathematically for example okay this is

a pretty complex formula I guess I mean

we could get the length we would then

know that we could do a mod not a mod a

yeah we could divide it by one followed

that by that many zeroes that would give

us that digit we could then add that to

the next thing but multiplied by ten oh

okay it's possible but do I want to

spend the time writing it let's find

another another thing out there

reverse mathematics

no not that how to reverse the number as

an integer and not as a string in

c-sharp okay so this is this is the

formula it seems seems pretty easy we

could convert this to JavaScript so you

have your starting number left is going

to be equal to that number and then

reversed is zero so while left is

greater than the number left mod 10 so

that's gonna grab the mod 10 we'll grab

the first digit let's see let's take a

quick break

oh well hmm as a hackish one-liner yeah

and this is what we're doing right now

like we're turning it into a string and

then into an array because strings in

JavaScript don't have a reverse method

so to string reverse aggragate that's

still turning it into a string so while

the number is greater than zero

you set result equal to zero times 10 so

10 plus the tens place of the number and

then drop the the tens place this will

work let's just do it cuz I don't like

this this is like a lot of unnecessary

things happening when we really just

want to reverse the number okay

Briggs is upping asking what app is that

are you referring to my code editor I am

using vs code Visual Studio code and

we're writing solutions on code Morse

code Wars comm okay but we need to turn

this into JavaScript because we're not

in c-sharp so let's say let result equal

zero and let's just call this num look

at that we just turned c-sharp into

JavaScript and that will return the

rizzo let's see if this still works

do I have an infinite loop I think you

have an infinite loop Oh

Briggs is asking the app that reminds me

to take breaks that is and I'm sorry my

the chat is a little bit delayed so

sometimes I don't know what questions

refer to but I'm on Mac this thing is

called de shell by timeout peejoe do you

want to you want to you're doing a

reverse with just math I'd like to check

it out you can put it on like

code pin and then just send me like the

short code but Dasia dollar is what I

use for breaks I have it set up for a

10-second micro break every 15 minutes

and then a five minute break every sixty

minutes

if you're on Windows there's a app

called I leo does a very similar thing

kind of like a full screen overlay yeah

so this broke

why did it broke

oh wait for his palindrome we just say

is the number equal to the number but we

still need our reverse function for this

thing right here but yeah this this

could okay let's put it back to what the

way I had it silly me

okay so that that could be that but we

don't actually need to reverse it for is

palindrome because a palindromic number

is equal to itself because it's the same

forward and backwards I think yeah I

never mind cool I think I'm gonna move

on how long have I been streaming our in

36 minutes yeah I'm gonna move on

peejoe if you want to send me your

number reverse method I'll give it a try

but I am gonna remove on to the last

challenge of the night Roman numerals so

let's submit the final version of this

real quick drumroll please

we did it let's see what other people

did is palindromic only works for

positive numbers okay well we didn't

take that into account but they didn't

test for it oh this is their function

for to reverse the number so you start

your resulted 0 this is exactly similar

to what I tried to do it's not the exact

same you start your number at zero and

then while in has a value so when

whenever in is zero this will break out

because of like true thief all see in

JavaScript you take the result and

multiply it by ten and then you so

that's going to give us initially zero

and then you add the last digit of the

number and then you multiply it by ten

and yeah we're shifting places so you

multiply it by ten add the last digit

multiply by ten add the last ish it so

it's like shifting the digits to the

front that's cool I like that okay last

one of the night Roman numerals create a

function function taking a positive

integer as its parameter and returning a

string containing the Roman numeral

representation of that integer so modern

Roman numerals are written by expressing

each digit separately starting with the

less leftmost digit and skipping any

digit with a value of 0 in Roman

numerals 1990 is rendered there's a

thousand so you get an M there's nine

hundred left over so you get a cm oh and

you're putting everything at the front

ninety is XC resulting in MC MXC wait

how does this work okay so here's the

symbols that we can use so a thousand is

m9 90 is CM oh this is good

hard because it's 100 of its M minus C

gives us nine hundred and then XC is 100

minus 10 that gives us 90 resulting in

MC MXC okay and then 2008 is written as

2000 is mmm

it's 2000s and then 8 is written as 5 6

7 8 so VIII and then all together that's

mmm VIII

tricky one okay 1666 uses each Roman

numeral in descending order so you get

an mb cuz there's a thousand in it and

then for 666 you're gonna have a d with

a hundred left over so you'll have a C

with 60 left over so you'll have an L

which with ten left over so you'll have

an X with six left over so we have a V

and then with I left over you'll have a

one okay that honestly makes it sounds a

little bit easier because you're

basically just using the biggest

possible number to get to that number

and then moving on to the next one after

you've reached it okay remember that

there can't be more than three identical

symbols in a row oh no it's gonna be

tricky

let's let's do it though okay so this is

the are like lookup table to Train

and also let's throw in some examples to

help us out while we're doing this

so he's okay so

I think the the part that's that's that

is tricky to me is that like 900 is cm

because that's 1,000 minus 2m I mean uh

sorry 1,000 is minus 100 that gives you

900 and then 90 is XC which is 100 minus

10 so how are we going to determine if

we need to do that okay first thing

though I'm going to take this lookup

table and turn it just turn it into an

object because that that will be easy to

access specific things by their

character so I is 1 B is 5 actually

before I do this let's let's write out

some pseudocode to figure out how we're

gonna do this and also let's look at the

examples that they're giving us down

here and I'm going to do this one

locally because it's gonna take some

work

okay those are our tests this is our

code we're gonna write and this is gonna

be a console log okay let's let's handle

the basic case one should return I so

for this case I'm just gonna say if the

number that they're passing in is in an

object where the keys are the numbers

and the values are that just return that

that's it's it's simple enough so let's

create an object

so this is our lookup where the key is

the number so 1 and the value is what is

that Roman numeral so 5 will then be V

and 10 will then be X and L will then be

oh sorry 50 will then be L and 100 will

then BC and 1000 will then be

okay so we got our lookup table and then

let's just check easiest case the number

they passed in is in this lookup table

so we returned that so I'll say if

lookup mm-hmm at that number then just

return look up at that number instead of

returning it let's like build it up

because I think we can start to wrap

this logic as we we figure out what to

do with it each thing so like let's say

Roman starts off as an empty string and

then if when we look that number up we

find it so then we just append the

lookup to the Roman room mm-hmm

as my break time urn I have a suggestion

in the chat I should add 9 40 and 90 etc

to the lookup so I guess that could help

so like if we have honestly so like if I

need like a really big lookup table so

like basically all possible versions of

this so like I I i yeah I like that

because when you're when you're writing

no more Roman numerals I guess when you

get to 4 it's VI yeah I like I like the

way you think

Alex Tube let's let's do this real quick

because this this should work for like

the the basic basic basic case

so when the number that they asked for

is in there

Oh apologies apologies especially if

you're wearing headphones apologies then

just return that so this should work for

our very first case lookup is not a

function no it's not so we need to use

square brackets because it's an object

cool and then like the special cases are

like right I think I think that's that's

where I was getting cut off so like the

special cases are like right before that

number is a compound case so like for is

VI IV IV and nine is IX and if we wanted

to represent eight it would be five plus

III right right okay so we can start to

do some math to figure out what there

are okay I'm getting I'm getting less

less nervous now okay so 490 forty off

topic what kind of mic do I use so I

have a condenser mic I I just got it at

Guitar Center it was a pack of two

condenser mics like one and if I can

show this in the best way so it's a

condenser mic I have a pop filter the

brand is a sterling audio so it's it's

nothing special I think if you were to

buy it alone is probably like 50 bucks

and then I have that plugged into a USB

audio interface with phantom power

Briggs is saying I can add seven eight

twenty and thirty or would that be too

much I

I want to add the special cases where

I'm not just adding up a bunch of eyes

because one is i2 is III three is III

four is the previous value VI and then

and mainly I just want to do it where

those numbers we could we will actually

use so for t be the next one we want

because 20 is xx yeah

but 40 will be Excel I believe he's the

number that we're subtracting from that

yeah and then 90 is going to be 10 minus

100 so it's the next number with what

you're going to be subtracting from it

and then this is the next number with

what you're gonna be subtracting from it

90

yeah because 700 would be actually do we

have one for 500 am I missing 500 I am

why am I missing 500 so we have 90 we

need yeah because if I wanted to

represent 400 that would just be CCC

Kaizen see you later

auditing exams good luck good luck

so yeah 400 would be CCC sorry 400 would

be DC or CD because it's 500 - 100 right

right so what I'm doing is like all

possible cases of the single letters

subtracted from some other value and

then 900 what wouldn't what was 900 cm

because that's 1,000 minus 100

okay are these all possible combinations

and then otherwise you just take the

previous number multiple times so 60

would be L plus 10 so LX right okay and

so basically what I want to do is use

math to grab off the the tens digit so a

thousand is a thousand in my lookup

table then grab that letter if the value

that I want is not in the lookup table

then I need to take its multiple and put

multiple of that value in the result so

cm yeah because then 800 would just be I

don't even know let's see is there an

algorithm for converting a number to

Roman numerals maybe I'll just follow

the algorithm because I am getting tired

I've been streaming for a long time and

I cannot think straight so let's let's

do this let's say decimal number two

Roman numeral algorithm

I'd recommend reviewing the site which

also has a converter if we're looking

for an algorithm see this some product

you were Europe Roman numerals are

numerals are based on the following

symbols cool so yeah I guess it might be

easier if we look at a look like a

larger table too so 1 2 3 special case 5

6 7 8 special case 10 20 30 40 50 60 70

80 90 yeah this this would work though

how big are the numbers that they're

gonna pass in 2007 because here's what

we could do

mm is wait do we have I thought we had a

thousand is M so would have mm because

it wasn't there twice and then then

we're just left over with seven so seven

in our lookup tables VII yeah these are

the numbers that I want in my lookup

table and then I can do basic math to

get the different places to build up my

Roman numeral that is what I will do

let's see if this has an actual

algorithm

cool let's just use this look up I don't

feel like it's cheating because it's

getting late okay so one two three four

and I guess the idea is once I have this

lookup table it's just a matter of math

to figure out what are the different

places in the number that they give me

and then I look them up in in this table

so five is six seven and then eight

viii and then nine is IX and then ten is

X and 11 will be X I wait no actually I

don't need eleven

I need the the ones place the tens place

and the hundreds place so then I need

ten I need twenty

I need 30 40 50 60 70 80 90 100 200

there's once I figure this out there

will be a way to to clean this up to not

have such a big lookup object yes so

Alex Tube is saying I can enumerate the

lookup and then subtracting the largest

possible from the remainder of the

number that's yeah and that's kind of my

plan I'm gonna do like so given 1990 I'm

gonna take a thousand and get it from my

lookup table then I'm gonna take nine

hundred and get it from my lookup table

then I'm gonna take ninety grab it from

my lookup table then I'm gonna grab this

and then that's not there and in this

case I'll take 2000 grab it from my

lookup table what's left over is eight

and I'll grab that from my lookup table

so I'm I'm gonna be moving through the

different places

yeah I guess I guess I guess what Alex G

was saying is for my for my lookup table

I don't need to do potentially the ones

that aren't the special case which is

like the subtraction yeah because it's

50 LX is 50 plus 10 so if I if I go back

to the lookup table that I had which was

not including these

but the the issue that I'm having is

given the current number how what will I

know to look up in the table I'm gonna

solve it this way and then we'll clean

it up though I'm honestly having a hard

time thinking straight tonight so 90s

place hundreds place seven seven

hundreds place eight hundreds place DCC

and then nine hundred cm and a thousand

I see him okay so this is messy we know

I cannot see your solution if you want

to send me you can't post a URL so I

don't see any URL if you get post-it but

if you do want to post like code pin and

then the ID I'll look that up or

whatever you post it on don't put the

URL just say where it is and what is the

idea of the thing and I'll pull that out

for sure okay so this is messy I'm gonna

solve it this way and then we'll make it

better but basically I have the values

for all possible places in a number

right so up to numbers in the thousands

so here's what I do I take my number and

I divide it by a thousand so how many

times does it go into a thousand Oh

actually I

sorry divided yeah and grab the the tens

place so I want to grab 1,000 and then

look it up so so your quick break mm-hmm

so how many thousands do I have in my

number so number divided by a thousand

right grounded or injured memory of is

Wemo in the chat what is the site that

that is on is that code pin

I guess they're giving us a basic

formula here - this might work

oh my I'm not thinking straight tonight

so breaking 1984 into a thousand nine

hundred eighty and four then do each

conversion so one thousand is M 900 is

cm so you take the number after the

larger one we know can I google this is

it a code pin Oh ripple dot it noise

I should just be / gonna copy this mmm

maybe that's not how the URL works

JavaScript

okay Ripple's slash that number okay so

we've got our basic lookup table and

then we are going over the length of the

input and I guess is our input a string

input is actually a number so we'd have

to convert it to a string to be able to

do length but then letter will be Roman

at what is what is the input to this

because if I if I do 1 1 doesn't doesn't

have a length I don't think so are you

passing in a string that we're

converting and yeah so then we're

grabbing the first digit and then we're

looking in the table to see if that

number is in there and then we're

grabbing the next value in the input

value so if we if we call this thing

this is going to be a solution invoked

with like say 1990

we know let me know how you're calling

this function the code kata you're

looking at has a string input this is a

for Caillou the one the way they're

testing it is passing in a number all

I'll throw this in the chat yeah but

when I passed in 1990 it got getting

back nan

oh you're doing the reverse conversion

like IV yeah you're doing the reverse

conversion I moan in the other direction

so I'm going from a number into Roman

numerals not from Roman numerals to a

number

yeah and I'm gonna I'm actually I don't

know if Alex Alex Steve and John and

Rica's are watching but they I think I

figure out what what they're talking

about so you go through the look up and

you find the largest possible number

that the remainder of the number goes

into and you use that then you subtract

it and then you find the largest

possible number that is definitely a

better algorithm yeah and so that way we

can only have the the main main ones we

don't have to put like an entire tyrunt

okay so we don't need all of this we

only need the single letters so like

what are the single possible things that

we can do with Roman numerals and this

is very similar like creating change

using specific coins because you grab

the largest possible one and then you

add the ones of the next yeah so that

should be easy enough do I have all

possible IV x LCD m IV excel CDM cool so

the example that they sent in the chat

let's comment this out so you subtract

the largest possible remainder from the

number so let's say we're given 1990 and

we go and we find the largest possible

number that this is greater than and

then john is saying you need to insert

the specific IV and IX case somewhere

after that yeah so if if it wasn't in

there then we have to find the specific

ones so our I guess our lookup table

should have the double letter

combination yeah I think we should do

one in to letter combinations and then

everything else will use our lookup

table I mean yeah everything else will

will have a certain certain number of

iteration yeah so the the the special

case is so not not just these cases we

need the cases where it's two letters as

well

correct me if I'm wrong and then from

there we can go so so we do need four

which is going to be what is for IV and

then we do need nine which is IX and

then we do need 40 which is Excel and

then we do need I guess that makes sense

because if you have 80 highest possible

one would be L then you need 30 so it'd

be l XX or XXL I don't know it's making

sense so if you have 50 and then we have

90

and then we have 400 and then we have

600 you see and then 900 okay this is

our lookup table all right I'll see you

later boom oh thanks thanks for the good

luck okay now we just need to make

change so we find the largest possible

number a pin that subtract that number

from our existing number do it again

keep on doing it all right I'm ready

I'm got some tea let's do this okay so

we need to enumerate the input so when

you do like the I'm gonna call it

numbers let's say lookups just call it

keys it's going to be object keys with

the lookup

why is this complaining this can be a

Const because we're not reassigning it

lookup is not defined Oh capital u okay

so we're creating an array that has all

the possible numbers in it

now we want to iterate over that array

and find the largest possible number

that our number will fit into so let's

say and actually we want to do it in

reverse

yeah so let's just create a for loop

what I equal keys link - 1 mm while I is

less than or equal to 0 so we're going

in Reverse and then I - - and we'll say

if our number that we have right now is

greater than keys a tie then that means

that we want this character so right now

let's say number is 1990 is 1990 greater

than 1000 yes it is so we want this M so

we need a place to store Roman we'll set

that equal to a string and then we'll

say Roman plus equal lookup

look up at keys a tie so we're gonna

grab that specific character added onto

Roman and I believe are we going in this

direction yeah so I should be able to

just a pen a pen dick because if I put

em and then a pen see em cool

so a pen that cheer Roman make that a

left and then subtract this number the

the current key let's store this in a

variable if number is greater than Roman

letter then we'll do the lookup at the

Roman letter cool and then we need to

say our number subtract the current

value so then that will give us what we

need to do the lookup next cool and we

need to do this over and over again

until a number is zero so we'll do while

number is greater than zero do this okay

I'm feeling okay about this it was a

really rough start getting here but I

think this will work because so let's

let's say the case where we have 1990

and we want to end up with MCC M

okay so right now the number is 1990

we're gonna go in here and we're gonna

happen upon 1,000 and 1990 is greater

than that so we're gonna grab an M so we

put an M on to Roman cool we subtract

that now we have 19 1990 we're gonna

look 1990 is greater than 900 so we have

a cm we're gonna add that on there we

subtract that we get 90 we want to do

less than or equal to because now that

we have yeah now that we have 90 is it

less than or equal to XC yes it is so

then we'll add XC to it okay this is the

winner let's see what we got

no Roman numerals do I have an infinite

loop I think I do

let's see real quick so

we're iterating in Reverse if number is

greater than or equal to this then Roman

will be appending that letter inside of

our lookup and we'll subtract that

number from number greater than or equal

to Z No

take a quick break I might actually run

this code through the through the the

the debugger just to see where my where

my infinite loop is cool well let's try

this with the case of his for is in

there try it with a thousand you should

be it should be just greater than

because when it's zero we want to break

out of this while loop

Oh

we need to brake right here because we

just appended a number we're gonna break

out of the for loop cuz otherwise it

kept it kept appending more and more I

think I think I think oh no let's debug

this thing anybody watching at home if

you see the issue with my code let me

know but I'm gonna run this through the

node debugger so we can just like step

through each line so with your node - -

inspect break yeah so this will

automatically break right when you run

into our code we will see what is going

wrong okay so the debugger is listening

I should be able to find it in chrome

inspect inspect okay so we are here

let's set a breakpoint right here and

we're gonna try this first we'll try it

with 1990 so when it gets here right now

in our scope number is 1 can we change

this change this tonight we're gonna

test this with 1990 make sure that it

works okay so first we create our keys

which are all the possible letters that

we could throw in there then we make

sure that number is greater than 0 cool

so we're gonna iterate over the keys

until we find a number that is that our

current number is greater than so we're

gonna do it in Reverse so we'll start at

weight oh I found what why this needs to

be greater than or equal to zero because

we're not we're not iterating we're

iterating down oh that was our infinite

loop okay

good thing I brought out the debugger

so wall because we're iterating in

Reverse while I is greater than or equal

to zero

decrement I then well then we'll have

our Roman numeral okay nope yeah thanks

thanks I like Steve he found it in there

too

yeah I can't believe I did that always

always always step through your solution

before you assume that it works

especially when you're doing something

like a reverse for loop because we're so

accustomed to like doing a greater than

or equal to our sorry a less than or

equal to but that did it I'm happy so

thanks so much Alex do you for the help

cool I'm glad we figured this out it was

a rough start it was a very rough start

but we made it there

I'm curious could we potentially turn

this into a reduce because we're

reducing I guess not really because we

we have we have side effects like we're

actually overriding the number with a

new value cool but those of you that

have never used the inspector before I'm

gonna keep I'm gonna I'm gonna run

through my solution one more time in the

inspector just you can see how that

thing works so if you have node

installed on your system you can do a -

- inspect break what this says is

inspect the code and then just

immediately stop before running anything

and then you give it a filename and then

that you can you can go into chrome

inspect and it should show up as a

remote target you will have to install

an extension for this but if you look

for like node inspect on the chrome

store you can find it and then if you

click inspect we're actually inside of

our code and we can run like one step at

a time

cool so first we're trying it with the

number one so we're gonna go into our

while loop is one and we can see over

here in our scope table is one greater

than zero yes it is so we go in now

we're going to iterate over all of our

keys now we grab the very last key

because right now I is 13 so we're gonna

go all the way to the end of that lookup

tape

and so our current Roman letter is 1,000

and so this is the other cool thing

about the chrome inspector when you're

using block scope variables like let

it'll actually tell you like this is

block scope and these are the the values

that are inside of it so Roman letter is

now currently 1000 and then we're going

to see is number greater than or equal

to 1000 no it's not so basically this

for loop is just gonna keep going all

the way till it gets to the end of a to

the beginning of the array where 1 is

equal to 1 because right now we're

testing it with 1 now finally Roman

letter is 1 and so our Roman numeral

will append if we look at what Roman is

will append I and then we'll subtract 1

from the existing number that gives us 0

so when we do our comparison we break

out and now we return just the letter I

cool I do want to see this let's let's

let's break whenever we try in 1990

because this is the one that I was

trying here so run all of my code till I

get to this line now run my code till I

get to that line so now we're trying it

with a number 1990 so we'll say keys are

all of the possible values that we want

to look up is 1990 greater than zero yes

it is and so now we're gonna iterate in

Reverse and so our first one is a

thousand is 1990 greater than a thousand

yes it is so we take our Roman which is

currently nothing and append the

character for a thousand so we're gonna

append an M on to it and then we

subtract a thousand from 1990 so right

now it's 1990 after this it will become

990 we bust out of this for loop and

then we check is our number greater than

zero yes it is so we keep doing what

we're doing we grab all of our keys

again

is 990 greater than a thousand

currently our Roman letter is a thousand

it's not so we'll go back again to the

next one now we're at nine hundred is

nine ninety greater than nine hundred

yes it is

so we'll say Roman plus equals the value

for nine hundred cool well subtract nine

hundred which will end up with ninety

blessed out of the for loop is ninety

greater than 100 yes it is and at this

point in time Roman is MCM and now that

we have ninety we already know that it's

in the lookup table so when we iterate

over everything we're gonna reach when

Roman letter is equal to ninety will

append XC subtract 90 from the number

which brings the number down to zero so

when this check happens it's done it

breaks out and we have our Roman numeral

that was fun so Alex Tube is saying that

I can make this solution a bit more

effective because you don't need to

restart searching from the largest

number like pop the last number from the

array and then check and pop that that

makes sense though because if we've

already grabbed the largest possible

number actually well it will that would

that work for 2007 though so that that

works for 1990 because after that

there's no number greater than that but

that wouldn't work for 2007 because I

need to come across him twice so that I

have him that makes sense

Alex tube I think it like that that case

specifically works if the ones digit is

one but if the ones digit is anything

more than that then we actually will

need to loop across the array again

just to see let's let's try this one

more time but with mm so while number is

greater than zero it currently is we're

gonna iterate and so is 2007 greater

than 1000 yes it is

so we append an M and then break out and

so because we broke out we need are keys

to still have a thousand in it because

now we need to look up the number for a

thousand again see so right now Roman is

M and I can look that up

add M go back around in this case it's

not gonna find hey what is oh now the

number is seven oh wow so that was easy

enough it's gonna find a five

so I like she was saying that would work

you remember the largest number and then

you subtract while that number is

greater than it then you pop it from the

array oh this one actually reduced the

number of iteration to because we

wouldn't have to go back around I like

it let's do that and then I'll submit my

solution so right in here I will say if

number is greater than equal to Roman

letter add it and let's just do a while

number is greater than or equal to Roman

letter we'll do this then we'll pop off

the last number because we don't need it

anymore

so we'll say eased up so it'll pop it

off and then we'll do it again I think

that should work let's try it true cool

thanks for the tip Alex you

yeah because basically if if we do it

once and it's still greater than then we

want to use more insane thing with like

if there's multiple I guess would there

be multiple seems I don't know let's try

it on code worse

here we go sample tests oh thank you

Alex tube and also who is the other

person helping me out

John Enriquez you guys Rock attempt

drum roll submit Fido

come roll it's happening more drum roll

it's really slow sweet fantastic this is

probably one of the ones I spent the

most time on on this stream but I

appreciate you sticking by for helping

me out we did a lot on the stream some

pretty hard ones too

I'm gonna push all these up to github

and you can check them out let's look at

what their solutions were so they did a

similar thing but instead of doing like

object keys they did a four in loop so

it would give you the keys and then you

can pull out the value from that make

sense peejoe

thank you we made it through it I feel I

feel I feel like is one of those like

super like nerve-wracking interviews

where you're like you're like you can't

think straight and you just got a I

don't he's got a power through it

eventually like it clicked and I was

like oh oh yeah I was about to write out

like just the largest lookup statement

oh cool they did it with the reduce so

they have their lookup table but they

have this property called threshold and

then so and then they're just they're

just moving like one at a time so while

the number is greater than the threshold

add add the current character to it and

then move on

I like that situation a lot that's

really cool cuz they're they're

accumulating so with the reduce you're

reducing it to a single value so they're

reducing this array of lookups to the

Roman numeral value that's slick cool

similar idea but instead of storing it

in an object they stored it in two

arrays awesome okay okay yeah that's

pretty much it thanks everyone so much

for tuning in live some housekeeping

if you check out my youtube channel

tomorrow morning I'm just gonna be doing

morning tea with CJ that's where I

browse the web talk about the latest

tech in JavaScript news and sometimes if

there's like a new library out or

something like that I'll try to make

something with it but a special show on

Sunday Brooks from Brooks builds is

going to be pairing with me and we're

gonna build a breakout game or a brick

record game with p5.js so be sure to

tune in for that

I've myself have never actually written

anything with p5.js I've watched a lot

of coding train and watched Dance

Schiffman do it and I mean I get I can

watch it and see it happen but I've

never literally done it myself so

that'll be fun and yeah all of these

solutions that I created today I'll push

up to code cutters and you can take a

look at those for my full screen

streaming schedule you can check out

coding garden I am I am gonna do a you

see that I am going to do a morning tea

stream tomorrow but I am going to skip

the afternoon stream tomorrow but I will

replace that with either a Friday or a

Saturday stream so if you look for my

channel and you have notifications

turned on you'll see when the that

scheduled stream pops up Rick says

amazing as always

thanks so much I feel like I really

struggled through that last one I could

not have done it without the chat I

appreciate you guys and peejoe is asking

where do we submit stream ideas thanks

thanks for asking so there's a website

pull coding garden and you'll have to

log in with github

that's just so people don't vote

multiple times but I only get your email

I'm not asking for any other privileges

so once you log in with Google you can

add suggestions or vote on existing

suggestions so there's a few things in

there

I think pretty soon I'll probably do

something with graph QL I haven't done

that on the channel yet and also

something with Redux and react

[Music]

yeah and this would be fun too I

actually haven't worked with noxious Oh

nuts is server-side rendered view j/s

applications which is similar so I think

next which is for react maybe it has a

different name but based in and really

you don't have to do much extra work you

can still build your your view j/s

application but if you write it with

this it creates it as like a hydrated

server-side render app which is pretty

sweet but yeah if you want to submit

ideas you can click suggest a video add

that in there and then you can click the

heart to vote on other suggestions

alright I think that's it thanks

everyone for tuning in and checking it

out is figuring out Dec GL with view to

complex I don't know what Dec GL is but

honestly submit anything like I am

always open to new ideas always open to

like even like learn things on the

string Dec geo I've never heard of it

WebGL powered data visualization oh cool

so like I have heard of 3j s which is

like a library on top of WebGL but this

looks awesome

let's see some examples so it's it's not

not only is it because 3j s is very

somewhat low level so you have like

vertices and shapes and things like that

this looks like it comes built in with

like various visualization things

enhance zoom in very cool 3d indoor scan

that would be another thing I don't have

anything on the list for it but I'd love

to do some some live coding with like

virtual reality something like that

whoa so this is like a was this called

you have a it's not a scatter plot but

basically like they scan this place in

3d with and then grabbed all of these

points in 3d and now they're they're

rendering it wow that's awesome this

this would be fun to explore there there

might be a like a view J s rapper with

it so that'd be cool

peejoe says they use it to show

real-time locations in America with heat

maps it looks awesome that'd be that'd

be great too

all right that's all I have for you

thanks so much everyone for tuning in

thanks for all your help everyone that

tuned in and messaged in the chat

I do this every Wednesday at 8:20 p.m.

mountain time so be sure to tune in next

week and I think that's it have a

wonderful morning afternoon night

wherever you are in the world I'll see

you next time and here is this

[Music]

you

[Music]

you

Loading...

Loading video analysis...