100 Days of Code - Day 9 Classes in Java Script

Hello, if you are catching this for the first time, my name is Rick and I'm writing every day for 100 days about my journey as an aspiring developer as I apply for jobs and continue to learn.

The process of applying for a job after attending a code bootcamp is quite discouraging but I'm still at it! I've been taking my time with each application, looking up each company I apply for to tailor fit a cover letter to the company. Majority of the companies don't even bother responding. I spoke with a developer today that seems to think I need to just pump out a lot more applications. “¯(ツ)_/¯“

First thing I did this morning was to try and recreate the sliding-window algo pattern I learned about two days ago, and like magic... I remembered. Hopefully I can apply this in a different way soon to some other challenge and it will feel like I'm not just memorizing things for the sake of memorizing. Here it is below...

function maxSubArray(arr, n){
  if(arr.length < num){
    return 'check the arguments'
  }
  let total = 0
  for(let i = 0; i < num; i++){
    total += arr[i]
  }
  let newTotal = total
  for(let i = n; i<arr.length;i++){
    newTotal += arr[i] - arr[i - n]
    total = Math.max(total, newTotal)
  }
  return total
}

What is a class?

According to Wikipedia, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods).

Think of it as a blue print for cookie cutter houses in a neighborhood and the houses are objects. The class essentially defines the structure of our objects though the data may not be the exact same. For example, all the houses may have siding but the color of the siding may change.

// this is what a class looks like
class User{
  constructor(username, email, password){
     this.username = username
     this.email = email
     this.password = password
  }
  // This is called a static method, hence the key word static.
  static countUsers(){
    console.log('There are 50 users.')
  }
  // A method is just a function that belongs to a class
  register(){
    console.log(this.username+' is now registered.')
  }
}
// Instantiate the bob object 
let bob = new User('Bob', 'bob@gmail.com', '123456')

// Call the method
bob.register()
// Call static method
User.countUsers()

// Inherit/extend classes

class Member extends User{
  constructor(username, email, password, memberPackage){
   super(username, email, password);
   this.package = memberPackage;
  }
  getPackage(){
    console.log(this.username+' is subscribed to the '+this.package+ ' package')
  }
}

// Instantiate the mike object using the new extended class 
let mike = new Member('mike', 'mike@email.com', '123213', 'standard')

// Call the getPackage method using the mike object
mike.getPackage()