Friday, February 17, 2017

JavaScript Object Oriented and Functional Programming



Declare JavaScript Objects as Variables:

//The constructor function is called Car.
var Car = function() {
  this.wheels = 4;
  this.engines = 1;
  this.seats = 5;
};

//In order to use the constructor function, you will be required to create an instance.  In this case, our instance is myCar.  The keyword "new" is required to when calling a constructor.
var myCar = new Car();

//Now, once the myCar  instance is created, it can used like any other object and have its properties accessed and modified.  For example:
myCar.nickname = "twin";

//After running the code above, you will have the following properties in myCar instance.
{"wheels":4, "engines":1, "seats":5, "nickname":"twin"}


Make Object Properties Private

//Here the private property is gear.  The value is initially set to 4 once the object has been instantiated.
//The constructor function is called Bike.
var Bike = function() {
  var gear = 4;
  
  this.setGear = function(changeGear){
    gear = changeGear;
  };
  this.getGear = function(){
    return gear;
  };
  
  return gear;

};

var myBike = new Bike();


Iterate over Arrays with map (Reference: Mozilla Developer Network)
// Predefined Array declared with values
var oldArray = [1,2,3,4,5];
var newArray = oldArray;

//Using the map method.  It will iterate through every element of the array just like a for loop would.
var newArray = oldArray.map(function(val){
  return val + 3;
});


Condense arrays with reduce

//To use reduce you pass in a callback whose arguments are an accumulator (in this case, previousVal) and the current value (currentVal).  The accumulator is like a total that reduce keeps track of after each operation. The current value is just the next element in the array you're iterating through.

//The array method reduce is used to iterate through an array and condense it into one value.var array = [4,5,6,7,8];var singleVal = 0;  singleVal = array;

//This array filter function will add all values in the array.singleVal = array.reduce(function(previousVal,currentVal){  return previousVal + currentVal;},0);


Filter Arrays with filter

//The filter method is used to iterate through an array and filter our elements where a given condition is not true.  The filter method is used to iterate through an array and filter out elements where a given condition is not true. filter is passed a callback function which takes the current value (we've called that val) as an argument.  Any array element for which the callback returns true will be kept and elements that return false will be filtered out.
var oldArray = [1,2,3,4,5,6,7,8,9,10];
var newArray = oldArray;

//This array filter function will only select values below 6.
newArray = oldArray.filter(function(val){
  return val < 6;
});

Sort Arrays with sort

var array = [1, 12, 21, 2];
//This function will sort the array from largest to smallestarray.sort(function(a,b){  return b - a;});

Reverse Arrays with reverse

var array = [1,2,3,4,5,6,7];
var newArray = [];newArray = array;
newArray = array.reverse();

Concatenate Arrays with concat
//concat 
can be used to merge the contents of two arrays into one.
var oldArray = [1,2,3];var newArray = [];var concatMe = [4,5,6];
newArray = oldArray.concat(concatMe);

Tuesday, January 24, 2017

Python - Using Custom Nested Exception Handling with Super


'''
Description:
The program runs and raises an exception in the grail function.
There are two classes in this program that will be used via the super() function.
Once the exception is raised, the class AlreadyGotOneToo is called, thus calling the AlreadyGotOne class.
Here the data 200 and "message" parameters are passed down to AlreadyGotOne.
So looking upward, the Exception parameter for the AlreadyGotOne is the AlreadyGotOne parameter that is a parameter
for AlreadyGotOneToo.  And the 200 and message parameters are for the AlreadyGotOneToo class.
This example describes a couple important areas in python.  They are nesting exceptions and using super()
'''

# User-defined exception
class AlreadyGotOne(Exception):
  '''Base application exception'''
  def __init__(self, statusCode, message=None):
    if (message is None):
      message = "An unspecified application error occured."
    super(Exception, self).__init__(statusCode,message)
    self.statusCode = statusCode

class AlreadyGotOneToo(AlreadyGotOne):
    pass

#Raise an exception
def grail():
  try:
    raise AlreadyGotOneToo(200,"expression cannot be evaluated (no value found)")
    print("TestPoint_grail")
  except AlreadyGotOneToo as e:
    print(e)

#main program
def main():
  try:
    grail()
  except AlreadyGotOneToo: #Catch the class name
    print("exception was raised")
 
if __name__ == "__main__":
  main()