Skip to content

Instantly share code, notes, and snippets.

@collinkallery
Last active February 21, 2020 18:00
Show Gist options
  • Save collinkallery/c19c1a01d6ec2875ed517656d16e97ee to your computer and use it in GitHub Desktop.
Save collinkallery/c19c1a01d6ec2875ed517656d16e97ee to your computer and use it in GitHub Desktop.

Counting Sheep Blogpost

The Challenge:

In Week 4 of Module 1 at Turing, our instructor gave us a list of code problems, told us to choose one, and had us spend 45 minutes solving it with a partner. If we solved it right off the bat with time left, we had to think of another way to solve the problem. Our instructor put an emphasis on pseudocoding and talking out loud while we were solving the problem before jumping into writing code. Here's the problem we chose:

Write a function that takes an array of Booleans. This function should return a Number that represents the number of times that true was present in the array.

We were given the following example:

  var sheep = [
  true, true, true, false,
  true, true, true, true,
  true, false, true, false,
  true, false, false, true,
  true, true, true, true,
  false, false, true, true
];
countSheep(sheep);
// => 17

Our Approach:

The first thing we did was take a few moment to digest the problem independently. After that, we took some time to talk in plain English about what we thought the problem was asking.

Basically, this problem is asking us to look over every element in the array, and any element was true, to count it and then give us the total at the end.

After deciding what the problem was asking us in plain English, we could then start using some technical terms. We decided that because we needed to look at each element in the array, we needed to use a for loop to loop over the array. After that, we needed to pseudocode a little bit:

As we loop through the array, we want to count the number of trues in the array. Each time we loop over a true, we want to add it to the counter, and then return that count.

My partner voiced that we should create a variable called "counter" and set it to zero initially. After the for loop begins, we should increase the counter every time the current index of the array is true. Here is the function:

function countSheep(array) {
  var count = 0;
  for (var i = 0; i < array.length; i++) {
    if (array[i] === true) {
      count += 1;
      }
    }
    return count;
  }

That immediately worked, and returned the correct number of trues in the array. We then tried to take on a different approach that I came up with. As we loop through the array, every time the current index is true, push it into the "trueCounter" array. When the loop is finished, return the length of the trueCounter array. Here is that function:

function countSheep(array) {
  var trueCounter = [];
  for (var i = 0; i < array.length; i++) {
    if (array[i] === true) {
      trueCounter.push(array[i]);
    }
  } return trueCounter.length;
}

This also immediately worked, and returned the correct number of trues in the array. We tried to then create a few different edge cases to test the functions. We tried passing in just falses, strings of true, random integers, and every time it worked. After that, I thought of a third way we could write a function: as we loop through the array, if the current index in the array is false, then splice it out of the array. After two times of perfectly written functions, we were expecting this one to work. First of all, here is the function:

function countSheep(array) {
  for (var i = 0; i < array.length; i++) {
    if (array[i] === false) {
      array.splice([i], 1);
    }
  } 
  return array.length;
}

Something very weird happened when we called this function however. In our array, we knew there were exactly 17 instances of true, and 7 of false. When we called the function with the splice prototype, it returned an array length of 19. We thought this was strange, so we logged out the array into the console and noticed that it was splicing our most of the instances of false, but it was leaving two. After looking a bit further, we realized that it was leaving a false in the array when two falses appeared one after the other in the array. It was splicing one of them, but not the other. We were working in a repl (which we know sometimes can be a bit weird and tempermental) so we moved the array and the function into the console and tried it there to make sure it wasn't just a bug in repl. After running in the console, we still ran into the same issue. At this point, we aren't sure why splice is having issues with a repeated value in an array, but it is definitely something worth investigating more!

Update: Solution Found!!!

Splice deletes the current index immediately, so when it runs into a false (let's say, index 15), it DELETES index 15, then the loop moves onto index 16. BUT the following false is then moved into index spot 15, so it is skipped over. A way to deal with this would be to iterate through the array backwards so that it is not modifying what is in each index.

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