AdjventJS 2023: Day 3 Challenge
Fixing the modified mischievous elf steps!

I am a Software Developer who loves coding, cats and sharing content!
Search for a command to run...
Fixing the modified mischievous elf steps!

I am a Software Developer who loves coding, cats and sharing content!
No comments yet. Be the first to comment.
AdventJS is a serie of coding challenges that are released every day since december 1st to december 25th (Christmas). The idea es solving each of one to practice and improve our coding skills.
Decoding messages in parentheses!
A React Native alternative

Palindrome Swap Challenge: Finding the indexes!

Alternating Christmas Lights!

Drawing Christmas Trees!

Organizing Gifts for Christmas Eve!

The solution to Challenge #3 of AdventJS 2023
The solution to the previous challenge
The solution to the next challenge
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.
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
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.
Input:
Original (original): The original string, from which we must start to find the differences. These are the original steps.
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:
''Considerations
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 ''.
/**
* 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 '';
}
Solution by Achalogy:
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:
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!