Wednesday, January 6, 2016

Day 169: FCC, Completing the New JavaScript Sections

Okay, I wanted to get straight to the Pomodoro Clock today, but since the FCC curriculum was updated a couple of weeks ago, I may as well complete the additional sections that were inserted into sections I had already completed.

The additions are very simple, so I don't think it will take me more than a few hours, if that.

Well, now that I'm actually doing it, it looks like they added more like 5 hours of basic material to one section, as well as one more algorithm scripting problem.  Between those two things, that may be enough to occupy my productive focus today.

I've done a bunch of problems so far today, but I've still got about 16 more to go on the Basic JavaScript section.  

We've got dark skies and a cold rain pouring down on Austin today, but that's all right, it makes me appreciate the sunny days the rest of the year.


I made a switch statement.  I've encountered them before, but they don't come up too often. 

function myTest(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
  case 1:
    answer = "alpha";
    break;
  case 2:
    answer = "beta";
    break;
  case 3:
    answer = "gamma";
    break;
  case 4:
    answer = "delta";
    break;
  }
  // Only change code above this line  
  return answer;  
}

// Change this value to test

myTest(1);

Here's another switch statement:

function myTest(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
  case "a":
    answer = "apple";
    break;
  case "b":
    answer = "bird";
    break;
  case "c":
    answer = "cat";
    break;
  default:
    answer = "stuff";    
  }
  
  
  // Only change code above this line  
  return answer;  
}

// Change this value to test

myTest(1);

This one's different, it gives switch statements for various results at the same time, instead of one at a time:

function myTest(val) {
  var answer = "";
  // Only change code below this line
  switch(val) {
  case 1:
  case 2:
  case 3:
    answer = "Low";
    break;
  case 4:
  case 5:
  case 6:
    answer = "Mid";
    break;
  case 7:
  case 8:
  case 9:
    answer = "High";
    break;
}
  
  // Only change code above this line  
  return answer;  
}

// Change this value to test

myTest(1);

This is neat, so this function:

function isLess(a, b) {
  if(a < b) {
    return true;
  } else {
    return false;
  }
}


isLess(10, 15);

Is the same as this function:

function isLess(a, b) {
    return a < b;
}


isLess(10, 15);

As in, it returns a true or false.  That's useful to know.  This next problem was really fun to work on, here's the actual problem:




And here's my solution!

var count = 0;

function cc(card) {
  // Only change code below this line
  if (card === 10 || card === 'J' || card === 'Q' || card === 'K' || card === 'A') {
    count -= 1;
  }
  else if (card === 7 || card === 8 || card === 9) {
    count = count + 0;
  }
  else if (card === 2 || card === 3 || card === 4 || card === 5 || card === 6) {
    count += 1;
  }
  var holdOrBet;
  if (count > 0) {
    holdOrBet = " Bet";
  }
  else {
    holdOrBet = " Hold";
  }
  return count + holdOrBet;
  // Only change code above this line
}

// Add/remove calls to test your function.
// Note: Only the last will display

cc(2); cc(3); cc(7); cc('K'); cc('A');

Neat, huh?  I'm wondering if it will work the same if I shorten the OR sections...I tried it, and no, it won't so when I do the && or ||, make sure to explicitly state each comparison, the shortcut won't work, at least on this example and on another I experimented with earlier.

I learned that if a property of an object we're trying to access has a space in its name, we have to use bracket notation.  This one was neat to work on also:


// Setup
function phoneticLookup(val) {
  var result = "";
  // Only change code below this line
  var lookup = {
    alpha:"Adams",
    bravo:"Boston",
    charlie:"Chicago",
    delta:"Denver",
    echo:"Easy",
    foxtrot:"Frank"
  };
  result = lookup[val];
  // Only change code above this line
  return result;
}

// Change this value to test

phoneticLookup("");

It filters and returns the property for the key.  Here's how to access an object with several levels:

// Setup
var myStorage = {
  "car": {
    "inside": {
      "glove box": "maps",
      "passenger seat": "crumbs"
     },
    "outside": {
      "trunk": "jack"
    }
  }
};

// Only change code below this line


var gloveBoxContents = myStorage.car.inside['glove box']; // Change this line

Note the bracket notation for the last property, and the quotes.  Also note that if we had placed the property in a variable and then accessed it via brackets, we wouldn't have used the quotes.  This accesses the "pine" value:

// Setup
var myPlants = [
  { 
    type: "flowers",
    list: [
      "rose",
      "tulip",
      "dandelion"
    ]
  },
  {
    type: "trees",
    list: [
      "fir",
      "pine",
      "birch"
    ]
  }  
];

// Only change code below this line


var secondTree = myPlants[1].list[1]; // Change this line

Check out this one!!!  I solved it quickly:




And here's my solution:

function multiplyAll(arr) {
  var product = 1;
  // Only change code below this line
  for (var i=0; i < arr.length; i++) {
    for (var j=0; j < arr[i].length; j++) {
      product = product * (arr[i][j]);
    }
  }
  // Only change code above this line
  return product;
}

// Modify values below to test your code

multiplyAll([[1,2],[3,4],[5,6,7]]);

O.k., so now, it's 1:34 a.m., and I'm planning to go to bed at 2 a.m., BUT I have only one problem left, out of 100 total in the JavaScript Basics section...so I really want to finish it, but it seems to be the hardest problem so far of all of them.  I'm going to give it a shot, I've got to go at it in a very methodical way to knock it out and go to bed on time...

With this solution, I added an array element to an existing array (1245):

// Setup
var collection = {
    2548: {
      album: "Slippery When Wet",
      artist: "Bon Jovi",
      tracks: [ 
        "Let It Rock", 
        "You Give Love a Bad Name" 
      ]
    },
    2468: {
      album: "1999",
      artist: "Prince",
      tracks: [ 
        "1999", 
        "Little Red Corvette" 
      ]
    },
    1245: {
      artist: "Robert Palmer",
      tracks: []
    },
    5439: {
      album: "ABBA Gold"
    }
};
// Keep a copy of the collection for tests
var collectionCopy = JSON.parse(JSON.stringify(collection));

// Only change code below this line
function update(id, prop, value) {
  if (prop === "tracks" && value !== "") {
    collection[id][prop].push(value);
    console.log(collection[prop]);
    return collection;
  }
  if (value !== "") {
  collection[id][prop] = value;
  }
  if (value === "") {
    delete collection[id][prop];
  }
  return collection;
}

// Alter values below to test your code

update(1245, "tracks", "Addicted to Love");

The code adds "Addicted to Love" to the tracks array for 1245, but it adds that string as an item in the array, it doesn't replace the array in tracks with a string, it inserts "Addicted to Love" into the array in tracks. 

That was the longest problem of the day, it took me one hour and 45 minutes to solve, and that hour and forty five minutes flew by in no time...I really enjoy when I get so into programming that I lose track of time.

Today was great, I'm much more comfortable with objects now.  It's 3:41 a.m., though, I stayed up a bit late coding...I'll leave the blog post at 11:30 p.m. yesterday anyways, so I don't have two posts on the same day.

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                                                 On 4 of 5
10. Intermediate Algorithm Scripting                 On 4 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: 9
Total Hours Coding: 793

No comments:

Post a Comment