Monday, January 25, 2016

Day 178: FCC, "Spinal Tap Case," "Smallest Common Multiple," "Finders Keepers," "Drop It," and Udemy Courses

Okay, I've been at this for a few hours, so I already solved "Spinal Case," and it was pretty difficult, actually.  Check out my solution:




Next is the "Smallest Common Multiple"  algorithm.  This one is hard.  Here's the question:




Keep in mind we have to find the smallest common multiple of the range of numbers in the brackets, not just the two actual numbers in the brackets.  This one was difficult for me, I had to look at the answer to solve the second (the more difficult part) part of it.  Here's my code, with notes.  What I did is I walked through the loop, to better understand what was going on and why the code returns the smallest common multiple.

function smallestCommons(arr) {
  if (arr[0] > arr[1]) {
    arr.push(arr[0]);
    arr.shift();
  }
  
  var newArr = [];
  for (var i = arr[1]; i >= arr.length - 1; i--) {
    newArr.push(i);
  }
  
// These variables are declared outside the loops.
  var leastCommonMultiple = 0;
  var loop = 1;
  var n;

/* The code will run while n is not the same as the array length.  I'm trying to understand this code.  I did the code above the three variables, but I couldn't figure out the rest of it, so I pulled the rest of the code from the FCC help menu.  I'm going to run through the answer to make sure I understand it, then perhaps I'll try doing it myself from memory to get a better grasp of what is going on in the code below.  I'll use the [5, 4, 3, 2, 1] array to run through this. The leastCommonMultiple, loop, and n variable are all declared above, globally, so that they can be returned outside the function without any issues.*/
  do {
/* Before the first for loop runs, the leastCommonMultiple variable is set to 5 * 1 * 4, which is 20.  That is then run through the for loop, which runs 20 % 3, which does not equal zero (there is a remainder), so the if statement breaks the loop, then loop gets plus 1, becoming 2.*/

/*The leastCommonMultiple is now 5 * 2 * 4, which is 40.  The for loop now runs with n set to 2 again. The if statement evaluate 40 % 3, which which does not equal zero (there is a remainder), so the if statement breaks the loop.  Then loop gets plus 1, becoming 3.*/
    
/* Before the first loop runs, the leastCommonMultiple variable is set to 5 * 3 * 4, which is 60. */
    
/*Now the for loop runs the first time, the if statement then evaluates 60 % 3 (n is 2, so newArr[n] is 3), which is 0, so the if statement does not break the loop this time.  This means the loop++ does not get activated yet, the for loop instead runs again, but now n = 3 (because of n++).  The leastCommonMultiple stays the same as well, because we are still in the for loop.  Now the if statement evaluates 60 % 2 (n is 3, so newArr[3] is 2 because newArr = [5, 4, 3, 2, 1]), which evaluates to 0, so the break is not triggered, and we go again.  Now the if statement evaluates to 60 % 1 (n is 4, so newArr[4] is 1 because newArr = [5, 4, 3, 2, 1]), which again does not trigger a break.  The n variable is now n++, which makes it 5, NOW it can't run again, because the while condition (n !== newArr.length) triggers.  Every item in the array has now failed to break the if statement (the loop ran 3 times on the last go around, which added 3 to n, which made it 5, which ended the while loop), which means the leastCommonMultiple variable is common to all of them.  Since we started from the lowest common multiple possible, by multiplying the two highest values, 5, and 4, and checking every multiple upwards, we know that this is the lowest common multiple.*/
    
    leastCommonMultiple = newArr[0] * loop * newArr[1];
    for (n = 2; n < newArr.length; n++) {
      if (leastCommonMultiple % newArr[n] !== 0) {
        break;
      }
    }
    loop++;
  } 
  
  while (n !== newArr.length);

  return leastCommonMultiple;
}


smallestCommons([1, 5]);

All right, I'm going to try solving that one again later tonight, going over the code again.  O.k., the next challenge is called "Finders Keepers."  Here it is:




Working on it now.  And here's the first input:



Neat.  All right, this is what I came up with for this one, I got some good practice with filter.

function find(arr, func) {
  var nameFunc = func;
  var nums = [];
  var firstNum = [];
  nums = arr.filter(nameFunc);
  if (nums.length > 1) {
    nums = nums[0];
    return nums;
  }
  if (nums.length === 0) {
    return undefined;
  }
}


find([1, 3, 5, 8, 9, 10], function(num) { return num % 2 === 0; });

All right, the next challenge is "Drop it."  I solved it, here's the question:




Here's the parameters and results:



Here's the solution.




Okay, I'm on algorithm 18 of 21 now, so four algorithms left.  It's time for dinner, though, I'm going to head home from the coffee shop and cook up a healthy meal...I might come back if it's not too late when I finish up.

So, after dinner, I started watching a JavaScript video on Youtube, it's called "JavaScript: Understanding the Weird Parts."  The first 3.5 hours of it are free on Youtube, and the rest of it you can buy at Udemy, so I went ahead and did so because I liked the presentation.  While at Udemy, I got one course for free, "JavaScript Essentials," and I bought another two courses, "Learn and Understand NodeJS," and "The Web Developer Bootcamp."  The Web Developer Bootcamp goes over the MEAN stack, and the other courses are self-explanatory.  I thought it would be great to have these courses to go over whenever I have some downtime from FCC, like the 3 or 4 hours before I go to bed when I don't want to be solving algorithms because then my mind is wired and I can't sleep.  I can always watch the videos several times if I need to.

I'm really happy.  Today was great.

SUMMARY OF CODING SKILLS

Total Treehouse Points: 5,503

Treehouse Points by Subject Matter (Miscellaneous not included): 
HTML:                                663 
CSS:                                1,599 
Design:                            1,193 
Development Tools:            747 
JavaScript:                      1,239

Treehouse Ranking (%): "You have more total points than 93% of all students."

Treehouse Courses Completed:
How to Make a Website
HTML
CSS Foundations
CSS Layout Techniques
Aesthetic Foundations
Design Foundations
Adobe Photoshop Foundations
Adobe Illustrator Foundations (66% complete, switched focus from web design to web dev)
Console Foundations
Git Basics
Introduction to Programming
JavaScript Basics

Codecademy (& other) Courses Completed:
HTML and CSS (Codecademy) 

Books Read or in Progress:

Completed: "Head First HTML and CSS," by E. Robson & E. Freeman
Completed: "A Smarter Way to Learn JavaScript," by Mark Myers 
Completed: "HTML and CSS," by Jon Duckett
Completed: "JavaScript and JQuery," by Jon Duckett

My Progress on The Odin Project:
1.  Introduction to Web Development                                             100% Complete
2.  Web Development 101                                                               33% Complete 
Note: I switched to FCC for the great online community and better updates/support.

My Progress on Free Code Camp (FCC): 
1. Get Started with Free Code Camp                                                      Complete
2. HTML5 and CSS                                                                                  Complete
3. Responsive Design with Bootstrap                                                       Complete
4. Gear up for Success                                                                           Complete
5. jQuery                                                                                              Complete
6. Basic JavaScript                                                                                 Complete
7. Object Oriented and Functional Programming                                     Complete
8. Basic Algorithm Scripting                                                                   Complete
9. Basic Front End Development Projects                                                Complete
10. Intermediate Algorithm Scripting                                                   On 18 of 21

11. JSON API's and Ajax
12. 
Intermediate Front End Development Projects
13. Claim Your Front End Development Certificate
14. Upper Intermediate Algorithm Scripting
15. Automated Testing and Debugging
16. Advanced Algorithm Scripting
17. AngularJS
18. Git
19. Node.js and Express.js
20. MongoDB
21. Full Stack JavaScript Projects

22. Claim Your Full Stack Development Certificate

After the 800 hours of FCC work above, there are 800 more hours of non-profit coding projects.


Hours Spent Coding Today: 8
Total Hours Coding: 861

No comments:

Post a Comment