Member-only story

JavaScript Algorithm: Mowing the Lawn

Learn how to write a function to simulate lawn mower cuts and achieve successive grass heights

Max N
3 min readJun 18, 2023
Photo by Kouji Tsuru on Unsplash

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:

  1. Define the function: Start by defining a function called cuttingGrass that takes in two parameters: grassHeights (an array of grass heights) and cuts (a variable number of lawn mower cuts).

--

--

Max N
Max N

Written by Max N

A writer that writes about JavaScript and Python to beginners. If you find my articles helpful, feel free to follow.

No responses yet