Algorithms

Algorithms are all around us. We use them everyday. Some are simple: wake up, brush teeth, shower, while some are complex: drive to work, if this road is blocked, take the other logistical road, while driving don't text, increase decrease and average speed variations, keep eyes on the road, etc....

On this page I will post every algorithm I do from FCC and other sources that comes up. You will see the questions, my thought process of analyzing such, and the finished code logic. I hope you enjoy reading this as much as I enjoyed coding it.

Website: CodePen.

Instructions: My friend Gwendolyn Faraday, a developer that also uses FCC, decided to post a quick algorithm challenge that goes as follows. Bob and Jane are two website owners who are competing to see who gets more traffic per day for n number of days. Bob is represented by bobArray and Jane by janeArray, where each index of the array is the number of unique visitors for that day. Compare each day (index) side by side to determine who won in terms of traffic for the day. If the number in bobArray at index 1 for example is greater than the number of janeArray at index1, then Bob 'wins' and receives a point, and vice-versa. Count up the total number of points for each person and return them in a new array of length === 2, where the 0-index is the number of points Bob has and the 1-index is the number of points for Jane. Then write that total points array to the screen. Here is the codepen: http://codepen.io/gwenf/pen/qNQYPw?editors=0010
If you want to participate, fork this codepen, solve the challenge and post a link in the comments here. Next week on Monday, we can look over the solutions together, try to optimize the code, and give each other suggestions

Thought Process: Make a logical comparison against Bob's and Jane's array data to conclude the victor. The code for the foregoing challenge is below.


Website: FCC.

Instructions: The instructions basically said to reverse this JavaScript string.

Thought Process: Because the instructions are very simple, my thought process was very quick and structured. When dealing with JavaScript, we have to keep in mind that it is a loosely typed language, one of the results of this is that the variable data type, will dynamically change, based on the data that's entered. Keeping that in mind, we now know the reverseString() method first takes in a string, thus we can start by using "string" modification and parsing logic. This first code we need to write, line 4, is to split the string with the split() method. "The split() method splits a String object into an array of strings by separating the string into substrings." The reason I decided to do this is because the "str" variable will now become an array, and that's what we want because it is quite easy to reverse an array in JavaScript because all we need to do is call the reverse() method on the array. But to first do this we had make the "str" variable hold the array data. Now that that is accomplished, we can then use the .reverse()  method on the "str" variable *(line 5)* because it now holds array data. Now that I both split the string data into an array, and then reversed the array, it is now time to join the contents of the array back together which will then form back into a string. We do this by using the .join() method, which joins all elements of an array into a string. Now the only thing left is to return the parsed parameter. 


Website: FCC.

Instructions: Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.

Thought Process: After analyzing the question, one could easily see that it was asking for the largest number in each sub array in the "arr" array. To accomplish this, I decided to use a while loop while utilizing the  built in "Math" object and "apply" method to both search the array's index(sub-arrays), and add the largest number to the "arr" array, thus creating the requested data inside of arr. Once this was accomplished, the next step was to simply return the new data inside of arr using the "return" keyword. 


Website: FCC.

Instructions: Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.
For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".

Thought Process: After analyzing the question, I could see that it is asking to not only capitalize the first character of each sentence, but to also make sure that every other character that's not the first character is lowercase. To accomplish this, I first decided to use the "toLowerCase" method to force every character to be lowercase. Doing this will first rectify any uppercase characters, therefore allowing the next case(uppercase logic) to only focus on the first index of each string when coded. The next step was to use the split("") method to split all the characters inside of the "str" string into arrays based on a space separated (" ") split. This allowed for me to run logic that operates on one area of the string (now array) at a time, which let me apply the "toUpperCase" logic only to the first index of the string. Afterwards, I sliced the remaining characters off of the string because I would have to latter add the string back together with the requested modifications.


Website: FCC.

Instructions: We'll pass you an array of two numbers. Return the sum of those two numbers and all numbers between them. The lowest number will not always come first.

Thought Process: After analyzing the question, I could see that it wanted me to get the sum of the two numbers inside the array while being sure to sum each number in the middle. I could see that I needed to get the max and min value of the array while taking into consideration that the first index will not always be the smallest. To accomplish this, I decided to first find the min and max values of the array by using the "Math" object's sorting logic and saving the data in the logical assigned names. Next I wanted to find the difference in those numbers so that I could use it as an ending point in the future while loop to loop the number of times inside of the difference while calculating each number. Example, the difference of 5 and 9 is 4, so I can now start my loop at 5, run a calculation for each loop and end it when the loop reaches 4. After coding the foregoing logic, the code worked.

Website:  Code Wars.

Instructions: Find the multiplicative persistence.

Thought Process: To find the multiplicative persistence, one has to separate a given number into single digits and multiply those numbers until the one reaches a single digit. I used the logic I learned in the foregoing algorithms to solve this algorithm as a whole. After running the numbers 39, one should get the answer of 4.

No comments:

Post a Comment