---
title: 'JavaScript Picks a T-Shirt Winner w/ Sound'
source: 'https://youtube.com/watch?v=_dvQpqoJkc4'
video_id: '_dvQpqoJkc4'
date: 2026-06-28
duration_sec: 646
---

# JavaScript Picks a T-Shirt Winner w/ Sound

> Source: [JavaScript Picks a T-Shirt Winner w/ Sound](https://youtube.com/watch?v=_dvQpqoJkc4)

## Summary

In this live stream, the host celebrates reaching 50,000 subscribers and demonstrates a JavaScript script that uses the YouTube API to randomly select a comment winner for a custom t-shirt giveaway. He explains the code, picks two winners, and discusses future content ideas like Google Maps and Google Photos APIs.

### Key Points

- **Subscriber Milestone** [0:32] — The host thanks viewers for reaching 50,000 subscribers and expresses gratitude for the support.
- **Giveaway Setup** [1:07] — He plans to pull comments from the previous video using the YouTube API to select a winner for a one-of-a-kind t-shirt.
- **Unique Shirt Design** [1:45] — The shirt is a one-time design printed only once, making the winner the only person in the world with that shirt.
- **Code Walkthrough** [2:02] — He shows a refined script using the browser for better UI, with an async recursive function that fetches comments in batches of 20 and picks a random unique comment.
- **No Authentication Needed** [3:58] — The YouTube API can be used without authentication; only a Google Cloud project is required.
- **Suspense and Uniqueness** [5:46] — A 5-second timeout adds suspense, and Lodash ensures all comments are unique, giving each commenter equal chance.
- **First Winner** [7:31] — The first winner is 'garfi Anto' (index 179 out of 147 comments).
- **Second Winner** [8:23] — A second winner, Arturo Blanco, is picked, making it a two-of-a-kind shirt.
- **Future API Plans** [9:55] — The host mentions interest in the Google Photos API, which recently exited Developer Preview, for potential business opportunities.

### Conclusion

The livestream successfully combined a subscriber milestone celebration with a live coding demo of a JavaScript script that uses the YouTube API to randomly select giveaway winners, while also teasing future content involving Google Maps and Google Photos APIs.

## Transcript

hey what's up everybody can you hear me
okay
hey what's up everybody can you hear me
okay if so let me know in the comments
so yeah there's that a few second delay
on my end so yeah everything's good let
me know and then we'll get going
cool so yeah first of all we're about to
hit 50,000 subs so I just wanted to say
thank you to everybody for that it's
never really expected to get to that
point so yeah I just really appreciate
all the support and everything there and
then yeah I wanted to just kind of
experiment with these live streams I
think in the future we could do like
some ask me anything kind of videos and
stuff like that cool are you guys
getting an echo or do I sound okay cool
um alright so I think the first thing
I'll do is just will switch over to the
code what I'm going to do is pull all of
the comments from yesterday's video
using that YouTube API so it kind of
keeps with that same theme of using
Google API is to get something done so
if you haven't already commented on that
on yesterday's video you've got about
maybe two or three minutes to do that
and then we'll pull all the comments in
real time
so I'm gonna go ahead and switch over to
a screen share and then we'll just blow
through the code in a couple minutes and
then we'll pick the winner oops alright
and so this is the actual shirt that is
going to be going out it's a one of a
kind like for these giveaways I just
create a one-time design and it only
gets printed once so if you win you'll
be the only person in the world that has
to share it and then I'm gonna switch
over to vs code here and we'll see last
time last time we use node to pick the
winner but I did a little more refining
to that script and we'll actually use
the browser this time to provide some
better UI elements and basically just
what we have here is some simple HTML
and I have a few empty elements and I'm
gonna trigger this pick winner method
and it's going to fire off this pick
winner script that I'll show you here in
a second
and basically the script is an async
it's an async recursive function so what
it's going to do is pick comments from
the YouTube API and batches of 20 and
then it will put all of those into an
array and then we'll do some random
stuff with JavaScript to actually pick a
random index from that array and make
sure that only a unique comments can be
picked so we'll set this up as an async
function and then we'll make this
recursive by passing in a page token
argument that starts out blank and then
it will end this function once the page
token ends that means we've reached the
end of like YouTube's comment pagination
so to make a request to the API we're
just using vanilla JavaScript and will
preside up some URL search brands and
that's going to take a key the video
idea what this is the YouTube video ID
from yesterday so you still have time to
comment C from there we'll go ahead and
request the snippet and then pass in the
page token which again we use for
pagination to know how to get like the
next 20 comments in a batch so let's see
here the actual YouTube API this is the
URL for that and then we'll format the
actual URL that we'll be making this
request to and just a side note you
don't need any authentication for this
API you can just use it as an
unauthenticated user all you need is
that google gcpd project and so we use
the browser or fetch API and that's
going to be a promise or wait that
that's the actual response from YouTube
and we can one tip here is when you have
a JSON response you can actually D
structure it using this syntax here and
just a cleaner way than setting up
multiple variables for the different
things that come back on the response
from there we'll go ahead and map the
items to the there's a bunch of data
that comes back in the response so
basically I'm doing here is just mapping
it down to the top-level comment and the
snippet on that comment which has the
user name and user profile and stuff
like that and we'll take this array and
for each batch we'll go ahead and
compose a new array so this wouldn't be
it in a rainy it gets bigger and bigger
and batches of 20 and this is what makes
the function recursive so if we have a
next page token then we're just going to
call this function that we're define in
some notes we're inside the size of
pacing function if we have a page token
then we're going to keep calling it
until we run out until we get to the end
of the pagination from YouTube so that
takes care of the function and then
we'll just define our array outside of
the scope of this function and mutate it
and then our event handler does its pick
pointer and so what this is going to do
is just grab a couple of elements from
the DOM and we'll update some
information in the DOM and what I want
to do is add a whole bunch of suspense
to this function so I have just wrapped
it in a timeout for 5 seconds so we'll
have to wait at least 5 seconds for the
swag gods to respond with their decision
and I'm kind of cheating here I'm using
a low dash to make sure that all the
comments are unique so that means if you
if you commented on the video multiple
times you'll have the same chance of
winning as someone who just commented
once and it's easier to do this with
lodash because it's quite a bit more
work in vanilla j/s when you have it
like deeply nested object property like
this and then to actually get a random
value we'll take the floor of a random
value and then we'll multiply that by
the length of unique values in the array
which last time I checked was about 110
so that's your odds will be about 110
Plus whoever commented in the last 10
that's that we'll take that winner it'll
be the index in our array and it'll just
update a bunch of stuff in the DOM and
that's it so if anybody has any any
issues of that logic let me know now in
the comments but otherwise I'm about
ready to click the button and we'll
figure out who wins so we can actually
this is just plain HTML and JavaScript
so we can actually just grab the path to
this to this file and we'll just open up
a new browser tab here and hopefully
everything works I kind of just threw
this together and I think I think it
should be good but hopefully we don't
run into any issues all right so here we
go three two one and the winner is garfi
Anto it should I he I'm not gonna say
that right but congratulations you won
that was index 179 out of 147 so yeah
quite a few more people commented in the
last few minutes and cool yeah thanks
for the nice video and you requested the
Google Maps API and that's definitely
something I want to do more of I know a
lot of people want to see a Google Maps
extension to the ionic course so I might
might add that to the ionic course here
in the near future um let's see but
that's one winner let's we might as well
go ahead and pick one more while we're
at it I think I said it was a
one-of-a-kind shirt but I guess it's
gonna be a two of a kind shirt now so
we'll wait one more decision and see
what happens and Arturo Blanco you're
the second winner pool so dashboard for
analytics with plotly and yan I did a
plotly video quite a while back but
that's definitely something that could
be revisited
so cool congratulations to the winners
if you're on our slack channel the best
place to give me your address and shirt
size would be on the channel but you can
also send me a direct message through
YouTube as well so yeah that's pretty
much all I have for the that random
winner is I think there's definitely
something I want to do more of because
there's plenty of opportunities to do
this and it's easy enough to do you know
on a weekly basis I think and yeah if
you guys have any questions in the
comments I can maybe try to answer some
of those now but I think that's pretty
pretty much all I had for this
livestream so I'll give that a few
minutes and yeah definitely want to do
some more stuff with the YouTube or with
with Google API is a big one that just
came out of Developer Preview recently
was the Google Photos API so that's got
gives you access to like at Google's
users users albums and photos so there's
a lot of I think business opportunity
there if you want to do you know some
kind of photo management app or even
like a physical printing app for photos
[Music]
cool and I'm just kind of looking
through the comments here
well again yeah thank you everybody I'm
gonna go ahead and wrap the stream up
there we can chat more on slack and yeah
again if you have any questions just let
me know all right thanks everybody see
you later
