Skip to content

Instantly share code, notes, and snippets.

@KyleWong2510
Last active February 21, 2020 18:15
Show Gist options
  • Save KyleWong2510/0fe93a01ab5196d802a15be0ed29bf65 to your computer and use it in GitHub Desktop.
Save KyleWong2510/0fe93a01ab5196d802a15be0ed29bf65 to your computer and use it in GitHub Desktop.
Reflection on solving a project with a partner

Reflection

The purpose of this exercise was to solve a problem that we had never seen. We stumbled into this like a couple of Stevie Wonders at a flat earth convention: confident in our abilites, but unsure of the things to come. We sadly did not finish. The simple thought of Mr. Wonder's unfortunate hairline could not compare to the sorrow I endured by not completing the problem. The problem we chose was called Lowercase Words(ironic naming, no?). This was our prompt:

Write a function that takes an Array of Strings, but potentially other data types. This function should return an Array of the same length and they should remain in order. Any strings in the returned array should be all lowercase.

Our process involved pseudo-coding and research of Javascript methods that we may need.

First, we wrote down what we needed to do in plain English. Something so clear that even Stevie Himself could see our plan of attack. Not because he's blind, but because he doesn't know how to code.

Next, we made sure to think about the parts of the problem that we knew how to solve. We thought about what tools we needed to iterate through an array and how to push those items into a new array.

We then did some research on the parts that we didn't understand. We needed to look at how to identify datatypes and how to make a string lowercase. We looked at MDN and W3 Schools to find out the answers. These resources were the key to finding our answers, the drums and baseline to the symphony that was our code.

After research, we started writing code. Like Stevie in the studio, we let our fingers do the talking and created a simple, yet beautiful little snippet of code. The first iteration began with an array of uppercase strings and we wrote a function to go through the array and lowercase each item:

var colors = ['Red', 'Green', 'Pink', 'Cornflower Blue'];
function lowerCaseWords(array){
  for (var i = 0; i < array.length; i++){
    if (typeof array[i] === 'string'){
      var lowerCase = array[i].toLowerCase();
    }
  }

This function was only returning the last item in our array. It did return it lowercase but we need all items in the array. We added a console.log within the loop which logged everything in lowercase. Next we had to put those items in an array. Here is our final code:

var colors = [1, 'Red', 'Green', 'Pink', 'Cornflower Blue'];
function lowerCaseWords(array){
  var newArray = [];
  for (var i = 0; i < array.length; i++){
    if (typeof array[i] === 'string'){
      var lowerCase = array[i].toLowerCase();
      newArray.push(array[i]);
    } else {
      newArray.push(array[i]);
    }
    console.log(newArray);
  }
}
lowerCaseWords(colors);

This code logged every new array for every iteration through the loop. However, the items were not lowercase. Given more time, I think we would have completed this challenge and I think we were in a good direction. The next step would involve looking at our code to see why the items were not lowercase and why the arrays were logged multiple timUPDATE!!!

My partner, Rachael, is a badass and figured out what was wrong with our code. We were pushing the original array items, not the variable, into our new array and had the log inside of the for loop. I knew there was something very superstitious about the code and Rachael nailed it. Here is our final code, isn't she lovely?:

var colors = [1, 'Red', 'Green', 'Pink', 'Cornflower Blue'];
function lowerCaseWords(array){
  var newArray = [];
  for (var i = 0; i < array.length; i++){
    if (typeof array[i] === 'string'){
      var lowerCase = array[i].toLowerCase();
      newArray.push(lowerCase);
    } else {
      newArray.push(array[i]);
    }
  }
  console.log(newArray);
}
lowerCaseWords(colors);

And that's it! Signed, sealed, delivered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment