Breaking Records
Tags: javascript, beginners, programming, tutorial
Published: 2024-06-13 14:09:26

*Prepare your favorite cup of coffee*, because we are about to enter the fantastic world of **Breaking records**.

## The problem

![Breaking records problem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yr5jmdfclqd6lumw9y5e.png)

![Breaking records problem](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tksv52srfvttnxngsqrb.png)

## The solution

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/om8n9h0t7zntq8gn85gj.jpeg)

To start the solution, let's start by defining the `breakingRecords` function that will receive a `scores` parameter, which is an array of scores:

```js
function breakingRecords(scores) {}
```

To save the best score, we will define the variable `best`, starting at 0, so it will consider the highest score so far:

```js
var best = 0;
```

We will also define a variable to compute the worst score so far, which will be `worst`:

```js
var worst = 0;
```

Next we will define the variable `currentBest` which will consider the first score in the `scores` array as the best score so far:

```js
var currentBest = scores[0];
```

The variable `currentWorst` defines the current score as the worst score to date:

```js
var currentWorst = scores[0];
```

Now let's go through the `scores` array, starting from the second element (`i = 1`), because the first element was already considered previously in the `currentBest` and `currentWorst` variables:

```js
for (var i = 1; i < scores.length; i++) {}
```

Inside the loop we will perform validation, comparing the current value in the `scores` array with the `currentBest` variable:

```js
if (scores[i] > currentBest) {}
```

If the condition is met, we will update the value of `currentBest` and increment the `best` variable to indicate that there has been a positive record break:

```js
currentBest = scores[i];
best++;
```

If the previous condition is not met, we will compare the current value in the `scores` array with the `currentWorst` variable:

```js
else if (scores[i] < currentWorst) {}
```

If the condition is met, we will update the value of `currentWorst` and increment the `worst` variable to indicate that there has been a negative record break:

```js
currentWorst = scores[i];
worst++;
```

Finally, we will return an array containing the number of positive record breaks (`best`) and the number of negative record breaks (`worst`):

```js
return [best, worst];
```

## Final resolution

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2w8gxysypzvrd3z7gh56.jpeg)

After following the step by step we have our final resolution:

```js
function breakingRecords(scores) {
  var best = 0;
  var worst = 0;
  var currentBest = scores[0];
  var currentWorst = scores[0];

  for (var i = 1; i < scores.length; i++) {
      if (scores[i] > currentBest) {
          currentBest = scores[i];
          best++;
      } else if (scores[i] < currentWorst) {
          currentWorst = scores[i];
          worst++;
      }
  }

  return [best, worst];
}
```

*Share the code, spread knowledge and build the future!* 😉

> Images generated by **DALL·E 3**