Having trouble falling asleep? It might be time to start counting some sheep. But, instead of counting with numbers, we're going to count the number of times the boolean of true exists in an array. That's right nerds, we're putting a spin on this classic method to help with insomnia. After this, you'll be put to sleep in no time!

So, what's a **boolean** you ask? A boolean is a data type that has one of two possible values, either true or false.

What's an **array**? Well my friends, that's a data structure that consists of a collection of elements, that is identified by it's index value.

So, now imagine we have an array that we're assigning to a variable (called sheep1). Inside this array, we have an array of true and false elements. We're going to have 17 true values and 7 false values. Like this:

```
var sheep1 = [
true, true, true, false,
true, true, true, true,
true, false, true, false,
true, false, false, true,
true, true, true, true,
false, false, true, true
];
```

## Let's get started

Our challenge now is to write a function that is going to return a **Number** that represents the number of times that true was present in the array. So, when we invoke the function, we expect to return 17, which is the number of times that true is present in the array.

There's multiple ways to solve this fun problem, but before we get ahead of ourselves, it would be a good idea to talk through how'd we solve this in plain English, instead of getting caught up in all this computer speak. This is something we like to call, **pseudo coding**.

To start, we'd like to look at the sheep1 array and find all of the instances of the boolean true. Maybe we create a bucket that we can put these true values into each time we see the word true. Then, at the end we can look at our bucket and count how many true values we have. Okay, now we're ready to start coding!

Follow along with this link to my repl.it (https://repl.it/@michellekaplan7/counting-sheep)

#### Let's start by explaining the simplest way to solve this problem:

- Let's first created a function called, countSheep
- Next, we'll declare a variable called count that is assigned the value of 0 to start
- Then, we'll iterate over the length of the array (this indicates the use of a 'for' loop)
- Each time we iterate over the array, if the current index of the array equals true, increase the value of our counter by 1
- Finally, return the value of our count variable, which will be the amount of times the boolean of true was in our array.

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

#### That's too easy you say!? How about another solution!

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

In this solution, instead of creating a count variable, we are now declaring a variable called trueCounter assigned to an empty array. This way, each time we iterate over the array, we can use an array prototype (called push()), and "push" that instance of the boolean of true into this new array. Once we've gone through our 'for' loop, we will return the length of this trueCounter array! Pretty cool huh?

#### Still not tough enough for ya? Check this out!

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

With this method of solving the problem, we are using the array prototype of slice(). Effectively, we believed that we could iterate over the array and if the index of the array is false, splice that index of the array off. Then, we could return the length of the array. But, WARNING! This solution doesn't work!

#### Are you still awake?

Comment below and let's figure out why this last solution doesn't work!

## Discussion (15)

What you list as the "simplest way to solve this problem" is not

actuallythe simplest solution. There is already a built-in JavaScript function that achieves this result:If you want to technically meet the full requirement (which is to provide your own function that does this), then you just need to wrap the built-in JS solution in a function like:

You may use Boolean too

Wow, that's great Adam! I'm only 4 weeks into my studies, so I definitely have a lot more to learn. Thanks for posting this solution. I'll be using this!

I normally wouldn't make this kinda pedantic observation, but since you say you're "only 4 weeks into studies"...

Get rid of the

`var`

. You don't want to use that in "modern" JavaScript design. You should always, always,alwaysuse`let`

or`const`

.It may

feelright now like that's a semantic, rather useless, observation. But it's not just based on codingdogma. JavaScript has some particular, umm...headachesregarding`this`

and the hoisting of`var`

-declared variables intoglobalscope. These headaches are solved by religiously sticking with`let`

or`constant`

.The full description of why

`var`

is "problematic" is far longer than I care to type in this reply. But as you get further down your studies, it's a good thing to research.Also, FWIW, the

`var`

keyword tends to cause headaches inotherlanguages as well (e.g., C#).Instead of counting sheep, how about we

reducethem?What's going on here? Where did the sum come from?? Adding the a boolean to a sum??? Are you

nuts?!Ok let's go step by step.

reducerfunction for each of the items in the array and returns a single value.accumulator, in our case it's the`sum`

. The value that you return in the reducer function becomes the accumulator for the next iteration.`true`

.`true`

.otherwords, if the value of our current iteration is`true`

, we want to increment the`sum`

by 1.`sum = sum + (true ? 1 : 0)`

.`sum + boolean`

???`true`

will become`1`

and`false`

becomes`0`

. So`Number(true) === 1`

.`+`

, JavaScript will automatically convert values to numbers for you!Now, you may have noticed that we never declare

`sum`

anywhere. We never tell the reduce function to start counting at 0. Next to the reducer function,`Array.reduce()`

can take a second parameter, which is the initial value for the accumulator. If we do not pass an initial value, it will just take the first value of our array.There you have it!

We can just add the boolean to our sum, and it will add 1 if it is

`true`

or 0 for`false`

, exactly what we want.That's so cool Edwin! Reduce sounds like a very interesting prototype that I'm going to look into more!

Nice One!

You can also do this with a one-liner es6 arrow function using a JavaScript array filter method.

You can read more about the filter method here.

Here I have tried a different approach to this problem.

Taking sheep1 array as reference.

So many different solutions, I love it!

Fun article!

The

`array.splice([i], 1);`

line should be`array.splice(i, 1)`

. Is that what you were looking for?not recommended though.

I dunno why, but based on the title of the post I was expecting a deep neural network which would segment sheeps in a picture and then count them XD

You can embed REPL IT like this:

That's a great tip, thank you! I will use that next time!

Checkout:

var sheep1 = [

true, true, true, false,

true, true, true, true,

true, false, true, false,

true, false, false, true,

true, true, true, true,

false, false, true, true

];

function countSheep(sheep1) {

count = 0;

for (let value of sheep1)

if (value)

count++;

return count;

}

console.log(countSheep(sheep1));