Friday, January 22, 2016

Day 175: FCC, Intermediate Algorithm Scripting, "Where Art Thou" and "Search and Replace"

Today I'm working on the 4th problem (but this may change, FCC adds problems to the curriculum every so often, so the number order could change, if they insert new material prior to older material) in the Intermediate Algorithm Scripting Section.  There are 21 problems in the Intermediate JavaScript Algorithms section at this point, and I've completed 5 of them, leaving me 16 more to go.  Once I finish these problems, there's a short JSON and AJAX section (approximately 2 hours), and then 6 Intermediate Front End Development Projects.  At that point, I will have earned my Front End Web Dev Certificate, but can keep on going with the curriculum.  My plan is to earn the certificate, then make copies of my codepen projects on GitHub, update my portfolio site, and keep upgrading my front end skills as I work on securing a front end dev position.  This could change, depending on how long it takes me to complete the front end section.  I may just continue with FCC until I complete the entire curriculum.  We'll see, but the plan at the moment is to complete the front end section then do the work I need to do to be prepared for a front end dev interview and position.

So, back to today.  

The name of the problem is "Where Art Thou," but this doesn't really tell you anything about the problem, so I'll give a little more details.  In this problem, I have to go filter through an object and return certain items, if they are found.  Here's a screenshot:




And here's the sample inputs and the code to start off with:




This code allows me to return all the "first" values in collection:

function where(collection, source) {
  var arr = [];
    for (var j = 0; j < collection.length; j++){
  arr.push(collection[j].first);
}
  return arr;
}


where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

The output would be:




Then, if we switch it up and use .last as the key to search for, like so:

function where(collection, source) {
  var arr = [];
    for (var j = 0; j < collection.length; j++){
  arr.push(collection[j].last);
}
  return arr;
}



where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Then is that case, the output would be:



That's neat.

So, what I've learned from doing these algorithms is that the first thing I need to do is to actually understand the problem.  So let's do that, let's clarify what exactly we're being asked to do. 

Okay, I understand the problem now.  Every key and value pair that is present in the second parameter (an object) has to be present in an object in the first parameter (an array of objects) in order for us to return the object in the first parameter.  If there are extra key and value pairs present in the object in the array that is a match, those should be returned as well.

I'm going over this code:


for (var key in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
        var val = obj[key];
        // use val
    }
}

And this code:


for(var key in objects) {
    var value = objects[key];
}

They both deal with getting the values of an object.  Then this:

function where(collection, source) {
  var arr = [];
  for (keys in collection) {
    arr.push(Object.keys(collection[keys]));
  }
  return arr;

}

Returns all the key and value pairs in the collection array:



I'm learning, I'm still pretty new at working with objects, I've done a lot of challenges with arrays, but objects are still new to me.  Hmmmm, I've got this so far...

function where(collection, source) {
  var arrS = [];
  var arrC = [];
  for(var key in source) {
    arrS.push(key + ": " + source[key]);    
  }
  for (var i = 0; i < collection.length; i++) {
    for(var key2 in collection[i]) {
      arrC.push(key2 + ": " + collection[i][key2]);    
    }
  }
  return arrC;  
}


where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { first: "Tybalt", last: "Capulet" });

The Output is:




What it does is it puts both arrays into another array...which I can then access to compare?  But they're strings now, not objects...

Yeah, that won't do me any good.  But hey, I'm getting a lot of practice with manipulating objects.  

Okay, I've reset the code, starting from scratch again.

Oh no!  I realized I've been making a big mistake!  The input for source should actually just be: 

{ last: "Capulet" }

I had added a first name to that input a while back to test something, and then I forgot to remove it, and this has been affecting everything.  Well, at least now that I reset it, I can begin correctly again.

Okay, I'm going to save some code, I put source inside an array, and that allows me to manipulate it a little better:

function where(collection, source) {
  var sourceArray = [];
  sourceArray[0] = source;
  var array2 = [];
  var array3 = [];
  var collectionArray = collection;
  // What's in a name?
  for (var keys in collection) {
    array2.push(collection[keys]);    
  }
  //return array2;
  for (var keys2 in sourceArray) {
    array3.push(sourceArray[keys2]);    
  }
  return array3;
}


where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

But I'm still figuring things out...well, I managed to get it to solve the problem for the first scenario:

function where(collection, source) {
  var sourceArray = [];
  sourceArray[0] = source;
  var arrayCol = [];
  var arraySour = [];
  var collectionArray = collection;
  // What's in a name?
  for (var keys in collection) {
    arrayCol.push(collection[keys]);    
  }
  //return array2;
  for (var keys2 in sourceArray) {
    arraySour.push(sourceArray[keys2]);    
  }
  //return arraySour;
  for (var i = 0; i < arrayCol.length; i++) {
    if( arrayCol[i].hasOwnProperty('last') && arrayCol[i].last === arraySour[0].last) {
      var jack = [];
      jack.push(arrayCol[i]);
      return jack;
    }
  }
}


where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Now I need to make it so that a variable holds the "last" value and changes as the inputs change.  This problem's a challenge.

I got this one to work also:

function where(collection, source) {
  var sourceArray = [];
  sourceArray[0] = source;
  var arrayCol = [];
  var arraySour = [];
  var collectionArray = collection;
  // What's in a name?
  for (var keys in collection) {
    arrayCol.push(collection[keys]);    
  }
  //return array2;
  for (var keys2 in sourceArray) {
    arraySour.push(sourceArray[keys2]);    
  }
  //return arraySour;
  for (var i = 0; i < arrayCol.length; i++) {
    if( arrayCol[i].hasOwnProperty('a') && arrayCol[i].a === arraySour[0].a) {
      var jack = [];
      jack.push(arrayCol[i]);
    }
  }
  return jack;
}


where([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "c": 2 });

The output is:




I may be on the right track...

Hmmm...now I got the 2nd one working...




This is neat...this code works for the first and the second parameters, as long as I change 'last' to 'a', which is the key used in the first and second parameter, respectively.  So I need to grab the name of the key from source, then place that in a variable, then use that variable in the formula.  The reason the other two don't work (I think) is because they have a second key and value pair in source, and I haven't configured my code to run more than one time, as far as source is concerned.  I'll work on that problem after the variable issue is solved, working on that now.

Okay, I've got two of the scenarios to work, here's a pic:




And I'm saving the code:

function where(collection, source) {
  var sourceArray = [];
  sourceArray[0] = source;
  var arrayCol = [];
  var arraySour = [];
  var collectionArray = collection;
  var skn = Object.keys(source);
  // What's in a name?
  for (var keys in collection) {
    arrayCol.push(collection[keys]);    
  }
  //return array2;
  for (var keys2 in sourceArray) {
    arraySour.push(sourceArray[keys2]);    
  }
  //return arraySour;
  var jack = [];
  for (var i = 0; i < arrayCol.length; i++) {
    if( arrayCol[i].hasOwnProperty(skn) && arrayCol[i][skn] === arraySour[0][skn]) {
      jack.push(arrayCol[i]);
    } 
  }
  return jack;
}


where([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });

Now I need to get the other two to work.  The other two will work if I get the formula to cycle again with extra keys...that's what I need to work on next.  

I solved it!  Holy Toledo!!!




Neat!  Here's the code, so I can go over it anytime:

function where(collection, source) {
  var sourceArray = [];
  sourceArray[0] = source;
  var arrayCol = [];
  var arraySour = [];
  var collectionArray = collection;
  var skn = Object.keys(source);
  for (var keys in collection) {
    arrayCol.push(collection[keys]);    
  }
  for (var keys2 in sourceArray) {
    arraySour.push(sourceArray[keys2]);    
  }
  var jack = [];
  for (var i = 0; i < arrayCol.length; i++) {
    if( arrayCol[i].hasOwnProperty(skn) && arrayCol[i][skn] === arraySour[0][skn]) {
      jack.push(arrayCol[i]);
    } 
  }
  if (skn.length > 0) {
    for (var j = 0; j < arrayCol.length; j++) {
      if( arrayCol[j].hasOwnProperty(skn[1]) && arrayCol[j][skn[1]] === arraySour[0][skn[1]]) {
        jack.push(arrayCol[j]);
      } 
    }
  }
  return jack;
}


where([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "b": 2 });

This was the input on that one:




I'm going to take an hour or so to go over everything I just did, I really want to get the concepts here.  Nonetheless, 15 more Intermediate JavaScript Algorithms to go!  

I'll note that my solution looks nothing at all like the other solutions on this problem, they use .filter(), .forEach(), .reduce(), and .every().  I need to learn how to use those methods.

Well, I put in an extra hour and solved the next one already!

Here's the problem:




And here's a pic with my solution:




That one was much, much, much easier.  That's my first draft, I'll see if I can clean it up a little, I just solved it a few seconds ago.  Woohoo!

14 more Intermediate JavaScript Algorithms left!

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 6 of 21 (#13 and #14 also done)

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: 10
Total Hours Coding: 845

No comments:

Post a Comment