Saturday, July 18, 2015

Day 118: Chapter 36 of the JavaScript Book

I finished Chapter 35 Functions in the last session, but I started off this session with a quick review of that chapter.  Next I went on to Chapter 36 Functions Passing Them Data.  I finished that chapter and went on to Chapter 37 Functions Passing Data Back From Them.  The difference between an argument and a parameter was a bit confusing, but it seems the argument is the data that is placed inside the parameter.

I coded up this function, with a return keyword at the end (and called it with merch(50) in the example below):

function merch(merchTot) {
  var orderTot;
  if (merchTot >= 100) {
    orderTot = merchTot;
  }
  else if (merchTot < 50.01) {
    orderTot = merchTot + 5;
  }
  else {
    orderTot = merchTot + 5 + (.03 * (merchTot - 50)); 
  }
  return orderTot;
}

merch(50);


I like functions.  Note that a function may only return a single value to the code that calls it.  Also, a function may be used within a function call:

var orderTot = merchTot + calcTax(merchTot);

And also can be called within another function:

function calcTot(price) {
  return price + calcShip(price);
}

Neat.  Next I moved on to Chapter 38 Local VS. Global Variables.  A variable has local scope when declared within a function, that is, when you use var to declare it within the function.  Important to note is that if you get sloppy and use a variable within the body of a function without explicitly declaring it, it will be global.  This is not good practice because it will cause confusion.  Also, variables that are named as parameters are implicitly local variables.

Let's say you declare the same variable inside and outside a function.  Then in that case, that variable will have different values within and without the function.  This is not good practice, and in this case, within the function, the global variable is said to be in the shadow of the local variable.

Although a function can use global variables, this is not good practice.  It is always best to pass values explicitly to functions through arguments (passed through parameters).  Also, you can change the value of a global variable within a function, and this will happen whether you use return or not, but it's better practice to use a local variable within the function, then pass that value back explicitly through a return statement.

The next chapter is Chapter 39 Switch Statements How to Start Them, but I wanted to give Euler Problem 3 a shot again now that I've brushed up on functions. So I worked on that for an hour or so...still working on it.

I learned that x /= y means x = x/y.  Here's a bunch of operators on the left and the translation on the right:

x++ x=x+1
x-- x=x-1
x+=y x=x+y
x-=y x=x-y
x*=y x=x*y
x/=y x=x/y

Neat.
 
SUMMARY OF CODING SKILLS

Total Treehouse Points: 5,385

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

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

Treehouse Badge(s) Earned Today:



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)
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
In Progress: "Eloquent JavaScript," by Marijn Haverbeke (On pg 27)
In Progress: "Head First Javascript," by Eric Freeman and Elisabeth Robson (On pg 56)
In Progress: "A Smarter Way to Learn Javascript," by Mark Myers (on pg 89)

My Progress on The Odin Project:
1.  Introduction to Web Development             100% Complete
2.  Web Development 101                                29% Complete
3.  Ruby Programming                                       0% Complete
4.  Ruby on Rails                                               0% Complete
5.  HTML5 and CSS3                                           0% Complete
6.  Javascript and JQuery                                  0% Complete
7.  Getting Hired as a Web Developer                 0% Complete

Hours Spent Coding Today:4
Total Hours Coding: 564

No comments:

Post a Comment