Coding/Blog challenge 5

Coding/Blog challenge 5

After a couple of days of minimal coding, I am ready to try out another challenge. This challenge was on CodeWars. I am going to start including more links to JavaScript documentation when referring to the methods that I choose to use. That way if you aren't familiar with the method or its syntax you can easily check it out.

The Puzzle

JsProblem.png

Today's challenge differs from some of the last ones, for sure, but also has many solutions. I've thought of a couple different ways to attack it & will share the approach that I ended up taking. Spoiler Alert In hindsight, I would do this differently if I were to refactor.

Beam me up, Scotty

My first instinct with this one was automatically to create an alphabet array. This way I have something to compare with the argument array. So, we can define our function & then declare the alphabet

function findMissingLetter(array) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";

return;
}

Initially, I forgot to even check for case. Let's not forget it again. To check for the case, I decided to set up a conditional statement to check if the argument array at index zero was equal to itself toLowerCase method. If it's not equal, we can use deduction to assume that it's an uppercase letter. If it's uppercase then we can turn the alphabet toUpperCase Then, I split the alphabet into an array. Let's set that up now.

function findMissingLetter(array) {
    //make alphabet
    let alphabet = "abcdefghijklmnopqrstuvwxyz";
    //is this uppercase?
    if (array[0] != array[0].toLowerCase()) {
        //if argument is capitalized, turn alphabet to uppercase
        alphabet = alphabet.toUpperCase().split("");
    } else {
        alphabet = alphabet.split('');
    }
return;
}

Next, I wanted to create a comparative array to populate. For this, I wanted to know the alphabets index of the first letter of the argument array. I also wanted to know the ending letters index on the alphabet.

To find the index I used the findIndex method.

function findMissingLetter(array) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";

    if (array[0] != array[0].toLowerCase()) {
        alphabet = alphabet.toUpperCase().split("");
    } else {
        alphabet = alphabet.split('');
    }
    //arguments first letter starts here on alphabet
    let begIndex = alphabet.findIndex((e) => e == array[0]);
    //last letter index 
    let endIndex = begIndex + array.length;
    //comparative array
    let comparedArr = [];

    return;
}

Then I wanted to fill my comparative array. I used a while loop & accumulator pattern to push the correct letters into the comparative array.

function findMissingLetter(array) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";

    if (array[0] != array[0].toLowerCase()) {
        alphabet = alphabet.toUpperCase().split("");
    } else {
        alphabet = alphabet.split('');
    }

    let begIndex = alphabet.findIndex((e) => e == array[0]);
    let endIndex = begIndex + array.length;
    let comparedArr = [];

    //push correct letters into comparative arr
    while (begIndex <= endIndex) {
        comparedArr.push(alphabet[begIndex]);
        begIndex++;
    }

}

Finally, we'd need to compare the argument's array to our comparative array, element by element. If "this letter is not the same as that letter" then we output the comparative arrays value at current index.

function findMissingLetter(array) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";

    if (array[0] != array[0].toLowerCase()) {
        alphabet = alphabet.toUpperCase().split("");
    } else {
        alphabet = alphabet.split('');
    }

    let begIndex = alphabet.findIndex((e) => e == array[0]);
    let endIndex = begIndex + array.length;
    let comparedArr = [];

    while (begIndex <= endIndex) {
        comparedArr.push(alphabet[begIndex]);
        begIndex++;
    }

    for (let i = 0; i < array.length; i++){
        if (array[i] != comparedArr[i]) {
            return comparedArr[i];
        }
    }
}

And this was my solution. After writing this solution, I knew that this solution was a bit too verbose & could be optimized. I didn't need to push letters into a comparative array. I could instead compare to the alphabet string by using the indexOf method to find my starting point.

Refactor

function findMissingLetter(array) {
    let alphabet = "abcdefghijklmnopqrstuvwxyz";

    if (array[0] != array[0].toLowerCase()) {
        alphabet = alphabet.toUpperCase();
    }

    let letterIndex = alphabet.indexOf(array[0])

    for (let i = 0; i < array.length; i++) {
        if (array[i] != alphabet[letterIndex]) {
            return alphabet[letterIndex];
        }
        letterIndex++;
    }
}

And I lived happily ever after, the end.