DEV Community

Alex Eickelman
Alex Eickelman

Posted on

Friday Morning Problem Solving With Friends

We started our morning pairing off with a teammate in order to tackle some various exercises. As it was first thing in the morning and I did not have an adequate amount of coffee, we decided to take on the second to the easiest challenge in order to get our brains warmed up to a full day of coding. The challenge was to look through an array mostly comprised of boolean values, with the exception of 2 strings and 1 integer, and return the first string that appears in the array (in this case 'hello'). We were given 45 minutes to complete this task.

We started off by trying to pseudo code, a skill that I am actively trying to work on (as well as actually coding). We looked through the array, found where the strings were located, then began the process of researching how to extract what we wanted. I created a repl, whereas Megan took over looking through various websites to find the correct array prototype we would need.

The first thing we came across was the 'find()' method, which 'returns the value of the first element in the provided array that satisfies the provided testing function.' This was exactly what we were looking for as the string 'hello' was the first string value within the array. Putting that in to a repl looked like this:

function findFirstString(variousThings) {
return variousThings === 'hello';
};

console.log(variousThings.find(findFirstString));

When invoked, we had the desired result the returned value was the string of 'hello'!

This however seemed a little too easy, and as we started messing with the function it turned out to not exactly give us the answer we were looking for. We could set the return value strictly equal to false or an integer and it would still print out whatever our input was. Then we realized we we are at Turing, and there are no simple solutions (even to one of the easier questions).

I knew we would have to run some sort of for loop so that it cycles through the entire array to search for the element that we wanted, but we knew that 'find' was essentially the method we wanted to utilize. Instead of setting the return value strictly equal to the value of whatever we put in, we realized we needed to create some sort of variable so that it only accepted strings and nothing else.

Since the array contained different types of elements (strings, booleans, integers), our good friend typeof came to the rescue in order to find only strings. The combination of the find method and typeof operator was exactly what we needed in order to produce 'hello' in the return statement (in this case, the correct way). The following was the outcome after some trial and error:

function getFirstString(variousThings) {
var found = variousThings.find(function(i) {
if(typeof(i) === 'string') {
return i;
}

});
return found;
}

High fives were shared, and only a few tears were shed during this time (a daily process here at Turing).

Top comments (0)