AdjventJS 2023: Day 3 Challenge
Tags: javascript, node, adventofcode, challenges
Published: 2023-12-04 16:56:50

---
title: AdjventJS 2023: Day 3 Challenge
published: true
date: 2023-12-03 06:00:00 UTC
tags: javascript, node, adventofcode, challenges
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s9m40ed4yffiy01tmbnh.png"
canonical_url: https://alexvalle.dev/adjventjs-2023-day-3-challenge
---

The solution to [Challenge #3](https://adventjs.dev/challenges/2023/3) of [AdventJS](https://adventjs.dev) 2023

The solution to the [previous challenge](https://dev.to/fenriuz/adjventjs-2023-day-2-challenge-955)

The solution to the [next challenge](https://alexvalle.dev/adjventjs-2023-day-4-challenge)

## **Challenge Description**

In Santa's workshop, **a mischievous elf** has been playing around with the gift production line, adding or removing an unplanned step.

You have the original sequence of _original_ manufacturing steps and the modified _modified_ sequence that may include an extra step or be missing a step.

Your task is to **write a function that identifies and returns the first extra step that was added or removed in the manufacturing chain**. If there is no difference between the sequences, return an empty string.

```javascript
const original = 'abcd'
const modified = 'abcde'
findNaughtyStep(original, modified) // 'e'

const original = 'stepfor'
const modified = 'stepor'
findNaughtyStep(original, modified) // 'f'

const original = 'abcde'
const modified = 'abcde'
findNaughtyStep(original, modified) // ''

```

Please, keep in mind:

- There will always be one different step or none.

- The modification can occur anywhere in the string.

- The _original_ steps could be empty

## **Analysis**

The goal is to write a function that can identify the first difference between two strings, either by an extra char in the string or a missing char.

> 💡**Char:** In programming, the **char** data type is for characters. It refers to a symbol (letters, numbers, punctuation) like a, +, 6, !, S. In JavaScript, it is still a string, as there is no data type for char, but when we talk about it we refer to a single character. - [MDN Glossary](https://developer.mozilla.org/docs/Glossary/Character)

**Input:**

1. Original (`original`): The original string, from which we must start to find the differences. These are the original steps.

2. Modified (`modified`): The modified string to which we must find the first extra char or the first missing char. These are the steps modified by the mischievous elf.

**Output:**

- A string showing the extra or missing char, if not found returns an empty string `''`

**Considerations**

- The modified sequence must not only contain the chars from the original but also have them in the same order.

## **Solution**

You can use a loop to compare char by char in the strings and, upon finding the difference, evaluate whether it is an extra element or a missing element and return it. If the conditions are not met, we return an empty string `''`.

### **Code**

```javascript
/**
 * Find the first different char between two strings.
 *
 * @param {string} original - The original string.
 * @param {string} modified - The modified string.
 * @return {string} The first difference between the original and modified string.
 */
function findNaughtyStep(original, modified) {
  // We start by declaring the index we will be iterating
  let i = 0;

  // While i is less than the sizes of both strings
  // we go through each one to compare them
  while (i < original.length || i < modified.length) {
    // If the char from the original string is different from the char in the modified string,
    // If they are the same we continue iterating and adding 1 to the index

    if (original[i] !== modified[i]) {
      // If the original string has more characters than the modified string,
      // return the char from the original string.
      // otherwise, return the char from the modified string
      return original.length > modified.length ? original[i] : modified[i];
    }

    i += 1;
  }

  return '';
}

```

### **Community Solutions**

Solution by [Achalogy](https://github.com/Achalogy/advent-js-2023/blob/main/retos/reto-3/main.ts):

```javascript
findNaughtyStep(original, modified) {

  const [lessWords, mostWords] =
    [original, modified].sort((a, b) => a.length - b.length)

  return [...mostWords].find((x, i) => lessWords[i] != x) ?? "";
}

```

Solution by [iswilljr](https://github.com/iswilljr/adventjs-challenges/blob/master/2023/challenge-03/challenge-03.ts):

```javascript
findNaughtyStep(original: string, modified: string) {
  const originalLen = original.length
  const modifiedLen = modified.length

  const sequences = {
    [originalLen]: [original, modified],
    [modifiedLen]: [modified, original],
  }

  const [steps, reference] = sequences[Math.max(originalLen, modifiedLen)]

  return [...steps].find((step, index) => step !== reference[index]) ?? ''
}

```

And that was the challenge for December 3rd and its solutions. Do you have another alternative solution? Leave it in the comments!