Member-only story
JavaScript Algorithm: Mowing the Lawn
Learn how to write a function to simulate lawn mower cuts and achieve successive grass heights
Introduction:
In this article, we will tackle a JavaScript programming problem called “Mowing the Lawn.” We will walk through the step-by-step process of solving this problem, explaining each step in a simple and understandable way.
By the end of this article, you will have a clear understanding of how to approach and solve similar programming challenges. So, let’s dive in and start mowing that virtual lawn!
Understanding the Problem:
The problem requires us to create a function that takes in an array of grass heights and a variable sequence of lawn mower cuts. The function should output the array of successive grass heights after each cut. If the grass has already been completely mowed, the function should return “Done” for each additional cut.
Approach:
To solve this problem, we will follow these steps:
- Define the function: Start by defining a function called
cuttingGrass
that takes in two parameters:grassHeights
(an array of grass heights) andcuts
(a variable number of lawn mower cuts).