Advertisement
JPDG13

Tutorial Part 3

Apr 7th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const x = 4;
  2.  
  3. if(x === 10){  //=== will match the data type, as well.
  4.     console.log('x is 10');
  5. } else if(x > 10){
  6.     console.log('x is greater than 10');
  7. } else{
  8.     console.log('x is less than 10');
  9. }
  10.  
  11. const x1 = 4;
  12. const y1 = 11;
  13.  
  14. if(x1 > 5 || y1 > 10){  //or statement
  15.     console.log('x is more than 5 or y is more than 10');
  16. }
  17.  
  18. const x2 = 6;
  19. const y2 = 11;
  20.  
  21. if(x2 > 5 && y2 > 10){  //and statement
  22.     console.log('x is more than 5 or y is more than 10');
  23. }
  24.  
  25. const z = 11; //Turnurary operator (?? not sure about these yet)
  26. const color = z > 10 ? 'red' : 'blue';
  27. console.log(color);
  28.  
  29. switch(color){
  30.     case 'red':
  31.         console.log('Color is red.');
  32.         break;
  33.     case 'blue':
  34.         console.log('Color is blue');
  35.         break;
  36.     default:
  37.         console.log('Color is not red or blue');
  38.  
  39. }
  40. //Basic Functions
  41. function addNums(num1 = 1, num2 = 1){  //default values can be overridden
  42.     return num1 + num2;
  43. }
  44. console.log(addNums(5,6));  //passing numbers through this function
  45.  
  46. // Arrow functions
  47. const addNums2 = (num1 = 1, num2 = 1) => num1 + num2;  //default values can be overridden
  48. console.log(addNums2(5,5));
  49.    
  50. // Arrow functions
  51. const addNums3 = num1 => num1 + 5;  //function addNum3 = a number and returns that number + 5
  52. console.log(addNums3(3));
  53.  
  54. //Object Oriented Programming
  55.  
  56. //Constructor Functions
  57.  
  58. /*
  59. function Person(firstName, lastName, dob){
  60.     this.firstName = firstName;
  61.     this.lastName = lastName;
  62.     this.dob = new Date(dob);  //can put in different date forms.
  63.     // this.getBirthYear = function(){
  64.     //  return this.dob.getFullYear();
  65.     //}
  66.     //this.getFullName = function(){
  67.     //  return `${this.firstName} ${this.lastName}`;
  68.     //}
  69. }
  70.  
  71. Person.prototype.getBirthYear = function(){
  72.     return this.dob.getFullYear();
  73. }
  74. Person.prototype.getFullName = function(){
  75.     return `${this.firstName} ${this.lastName}`;
  76. }
  77.  
  78. */
  79.  
  80. //Class.  Does the same as above, just prettier.  
  81.  
  82. class Person {
  83.     constructor(firstName, lastName, dob){
  84.         this.firstName = firstName;
  85.         this.lastName = lastName;
  86.         this.dob = new Date(dob);
  87.     }
  88.     getBirthYear(){
  89.         return this.dob.getFullYear();
  90.     }
  91.     getFullName(){
  92.         return `${this.firstName} ${this.lastName}`;
  93.     }
  94. }
  95. //instantiate object with constructor function
  96.  
  97. const person1 = new Person('John', 'Doe', '19 March 2019');
  98. const person2 = new Person('James', 'Dobbs', '4-25-2000');
  99.         // console.log(person1);
  100.         // console.log(person2.dob); // gets only dob
  101.         //console.log(person1.getBirthYear());
  102.         //console.log(person2.getFullName());
  103. console.log(person2.getFullName());
  104. console.log(person1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement