#100DaysOfCode Day 16: Find the Strongest Beer

Shawn McMahon
2 min readJul 28, 2021

--

Day 16: More Practice

Day 16! Today I spent some time practicing my iterator methods. What better way of doing that than revisiting some exercises with JSFun! JSFun is the practice repo we received in mod2 to practice our Javascript Fundamentals. (Side note — I have had this repo for over 15 weeks and I just now noticed JSFun is short for Javascript fundamentals. LOL! The things you realize when you actually have to write out your thought process. Or maybe I’m just not the sharpest crayon in the crayon box. Or maybe both?) Either way, let’s get into it.

Code snippet of the solution

The quest

Find the beer with the highest ABV given array of breweries. These breweries have a property of beers which is an array of all of their beers they serve. My mission is to find a way to go through each beer from each brewery and find the strongest beer.

Code snippet of the data set

The solution

For this problem, I reached for the reduce / forEach combo. This combo of iterator methods is a super powerful way to check for properties that are nested within an array of objects. We can use reduce to access each brewery object. From there, we can use forEach to look at every single beer within every single brewery. Since we only need to find the highest beer and not return a new array of beers, forEach is a good choice since it doesn’t return anything. We will look at each beer’s abv and if it’s higher than the current highestAbv, that beer’s abv becomes the new highest abv. We also assign that entire beer object to the accumulator of Reduce so we can return the entire beer object once the strongest beer is found. Return the accumulator and we have solved the problem!

Bam! Coding magic!

Code snippet of the solution

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Shawn McMahon
Shawn McMahon

Written by Shawn McMahon

Software Engineer, Snowboarder, Music Enthusiast

No responses yet

Write a response