Guest User

Untitled

a guest
Jan 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.39 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. //--------------------------------------------------------------------------------
  12. //Name: Kat Powell
  13. //--------------------------------------------------------------------------------
  14.  
  15. //--------------------------------------------------------------------------------
  16. //Exercise: Primitives, Objects, and Type Checking
  17. //--------------------------------------------------------------------------------
  18.  
  19. //Primitives:
  20. // Strings: 'Hello'
  21. // Numbers: 9
  22. // Boolean Values (True, False)
  23. // Null (The absense of a value)
  24.  
  25. //Objects:
  26. // In real life, a car is an object. In Javascript, the car (object) has properties (weight and color) and //methods (start and stop);
  27.  
  28. //Type checking:
  29. // Operator that returns a string indicating the type of the unevaluated operand-
  30. //Undefined, Null, Boolean, Number, String, Symbol (ES6), Function Object, Any other object
  31. //var a = 'Hello';
  32. //typeof a
  33. //output: string
  34.  
  35. //--------------------------------------------------------------------------------
  36. //Exercise: Debugging and Documentation
  37. //--------------------------------------------------------------------------------
  38.  
  39. // Console.log - used to lof code to the console
  40. // Comments - used to communicate intentio to other devs
  41. // Inline comments are with the // symbol and block-level is with the /* .. */ method
  42.  
  43.  
  44. //--------------------------------------------------------------------------------
  45. //Exercise: Math Operations
  46. //--------------------------------------------------------------------------------
  47.  
  48. // Basic Math:
  49.  
  50. var x = 1;
  51.  
  52. x += 1;
  53. console.log(x);
  54.  
  55. x -= 1;
  56. console.log(x);
  57.  
  58. x *= 1;
  59. console.log(x);
  60.  
  61. x % 1;
  62. console.log(x);
  63.  
  64. x /= 1;
  65. console.log(x);
  66.  
  67. //--------------------------------------------------------------------------------
  68. //Exercise: String Operations
  69. //--------------------------------------------------------------------------------
  70.  
  71. var foo = ['Mountain Climbing', 'Hiking', 'Rowing', 'Bird Watching'];
  72. console.log(foo);
  73.  
  74. console.log(outdoorAdventures.slice(2));
  75.  
  76. console.log(outdoorAdventures.toUpperCase(0));
  77.  
  78. console.log(outdoorAdventures.charAt());
  79.  
  80. //--------------------------------------------------------------------------------
  81. //Exercise: Control Flow and Loops
  82. //--------------------------------------------------------------------------------
  83.  
  84. // If/ Else/ Else If
  85.  
  86. for (i = 1; i <= 100; i++){
  87. if (i % 3 === 0 && i % 5 === 0){
  88. console.log("Fizzbuzz");
  89. } else if( i % 3 === 0 ) {
  90. console.log("Fizz");
  91. } else if (i % 5 === 0){
  92. console.log("Buzz");
  93. } else {
  94. console.log(i);
  95. }
  96. }
  97.  
  98.  
  99.  
  100. //--------------------------------------------------------------------------------
  101. //Exercise: Recursive Function
  102. //--------------------------------------------------------------------------------
  103.  
  104.  
  105. //Recursion is a technique for iterating over an operation by having a function call itself repeatedly //until is arrives at a result. Most loops can be rewritten in a recursive style, and in some
  106. //functional languages, this approach to looping is the default.
  107.  
  108. // Example 1:
  109.  
  110. let total = 150;
  111.  
  112. for (let i = 1; i <=150 ; i++) { // use the for loop to set up the parameters
  113. total += i;
  114. }
  115.  
  116. console.log(total);
  117.  
  118.  
  119. //Example 2:
  120.  
  121. function factorial(n) {
  122. if (n === 0) {
  123. return 1;
  124. }
  125.  
  126. //Recursion starts here:
  127. return n * factorial (n-1);
  128. }
  129.  
  130. factorial(10);
  131.  
  132. // output: 3628800
  133.  
  134.  
  135.  
  136. //--------------------------------------------------------------------------------
  137. //Exercise: Constructors
  138. //--------------------------------------------------------------------------------
  139.  
  140. class Surgeon {
  141. constructor(name, department) {
  142. // method on a class that runs everytime the clas is called- it's purpose is to CONSTRUCT the class when //you instantiate.
  143.  
  144. class HospitalEmployee {
  145. constructor(name) {
  146. this._name = name; // The this keyword acts like a pronoun does in an English sentence
  147. this._remainingVacationDays = 20;
  148. }
  149.  
  150. get name() {
  151. return this._name;
  152. }
  153.  
  154. get remainingVacationDays() {
  155. return this._remainingVacationDays;
  156. }
  157.  
  158. takeVacationDays(daysOff) {
  159. this._remainingVacationDays -= daysOff;
  160. }
  161.  
  162. static generatePassword() {
  163. return Math.floor(Math.random()*10000);
  164. }
  165. }
  166.  
  167. class Nurse extends HospitalEmployee {
  168. constructor(name, certifications) {
  169. super(name);
  170. this._certifications = certifications;
  171. }
  172.  
  173. get certifications() {
  174. return this._certifications;
  175. }
  176.  
  177. addCertification(newCertification) {
  178. this.certifications.push(newCertification);
  179. }
  180. }
  181.  
  182. const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
  183. nurseOlynyk.takeVacationDays(5);
  184. console.log(nurseOlynyk.remainingVacationDays);
  185. nurseOlynyk.addCertification('Genetics');
  186. console.log(nurseOlynyk.certifications);
  187.  
  188.  
  189. //--------------------------------------------------------------------------------
  190. //Exercise: Arrays
  191. //--------------------------------------------------------------------------------
  192.  
  193. var cars = [
  194. 'Saab',
  195. 'Volvo',
  196. 'BMW'
  197. ];
  198.  
  199. cars.push('honda'); // pushes honda into the end of the array
  200.  
  201. console.log(cars[3]); // calls an element from the array
  202.  
  203. console.log(cars.length); // logs the length of the array
  204.  
  205.  
  206. //--------------------------------------------------------------------------------
  207. //Exercise: Functions
  208. //--------------------------------------------------------------------------------
  209.  
  210. // A Function is a block of code designed to perform a task.
  211.  
  212.  
  213. // Function Declarations - a function that is bound to an identifier or name.
  214. const isGreaterThan = (numberOne, numberTwo)=> {
  215. if (numberOne > numberTwo){
  216. return true;
  217. } else {
  218. return false;
  219. }
  220. }
  221.  
  222. console.log(isGreaterThan(16, 20));
  223.  
  224. //Function Expressions - similar to function declaration, but the identifier can be omitted, which
  225. //creates an anonymous function. Function expressions are also often stored in a variable.
  226. const square = function (number) {
  227. return number * number;
  228. };
  229.  
  230. console.log(square(5));
  231. // Output: 25.
  232.  
  233. //Arrow Functions - Refactored function syntax
  234. const isGreaterThan = (numberOne, numberTwo)=> {
  235. if (numberOne > numberTwo){
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. }
  241.  
  242. console.log(isGreaterThan(16, 20));
  243. </script>
  244.  
  245.  
  246.  
  247. <script id="jsbin-source-javascript" type="text/javascript">//--------------------------------------------------------------------------------
  248. //Name: Kat Powell
  249. //--------------------------------------------------------------------------------
  250.  
  251. //--------------------------------------------------------------------------------
  252. //Exercise: Primitives, Objects, and Type Checking
  253. //--------------------------------------------------------------------------------
  254.  
  255. //Primitives:
  256. // Strings: 'Hello'
  257. // Numbers: 9
  258. // Boolean Values (True, False)
  259. // Null (The absense of a value)
  260.  
  261. //Objects:
  262. // In real life, a car is an object. In Javascript, the car (object) has properties (weight and color) and //methods (start and stop);
  263.  
  264. //Type checking:
  265. // Operator that returns a string indicating the type of the unevaluated operand-
  266. //Undefined, Null, Boolean, Number, String, Symbol (ES6), Function Object, Any other object
  267. //var a = 'Hello';
  268. //typeof a
  269. //output: string
  270.  
  271. //--------------------------------------------------------------------------------
  272. //Exercise: Debugging and Documentation
  273. //--------------------------------------------------------------------------------
  274.  
  275. // Console.log - used to lof code to the console
  276. // Comments - used to communicate intentio to other devs
  277. // Inline comments are with the // symbol and block-level is with the /* .. */ method
  278.  
  279.  
  280. //--------------------------------------------------------------------------------
  281. //Exercise: Math Operations
  282. //--------------------------------------------------------------------------------
  283.  
  284. // Basic Math:
  285.  
  286. var x = 1;
  287.  
  288. x += 1;
  289. console.log(x);
  290.  
  291. x -= 1;
  292. console.log(x);
  293.  
  294. x *= 1;
  295. console.log(x);
  296.  
  297. x % 1;
  298. console.log(x);
  299.  
  300. x /= 1;
  301. console.log(x);
  302.  
  303. //--------------------------------------------------------------------------------
  304. //Exercise: String Operations
  305. //--------------------------------------------------------------------------------
  306.  
  307. var foo = ['Mountain Climbing', 'Hiking', 'Rowing', 'Bird Watching'];
  308. console.log(foo);
  309.  
  310. console.log(outdoorAdventures.slice(2));
  311.  
  312. console.log(outdoorAdventures.toUpperCase(0));
  313.  
  314. console.log(outdoorAdventures.charAt());
  315.  
  316. //--------------------------------------------------------------------------------
  317. //Exercise: Control Flow and Loops
  318. //--------------------------------------------------------------------------------
  319.  
  320. // If/ Else/ Else If
  321.  
  322. for (i = 1; i <= 100; i++){
  323. if (i % 3 === 0 && i % 5 === 0){
  324. console.log("Fizzbuzz");
  325. } else if( i % 3 === 0 ) {
  326. console.log("Fizz");
  327. } else if (i % 5 === 0){
  328. console.log("Buzz");
  329. } else {
  330. console.log(i);
  331. }
  332. }
  333.  
  334.  
  335.  
  336. //--------------------------------------------------------------------------------
  337. //Exercise: Recursive Function
  338. //--------------------------------------------------------------------------------
  339.  
  340.  
  341. //Recursion is a technique for iterating over an operation by having a function call itself repeatedly //until is arrives at a result. Most loops can be rewritten in a recursive style, and in some
  342. //functional languages, this approach to looping is the default.
  343.  
  344. // Example 1:
  345.  
  346. let total = 150;
  347.  
  348. for (let i = 1; i <=150 ; i++) { // use the for loop to set up the parameters
  349. total += i;
  350. }
  351.  
  352. console.log(total);
  353.  
  354.  
  355. //Example 2:
  356.  
  357. function factorial(n) {
  358. if (n === 0) {
  359. return 1;
  360. }
  361.  
  362. //Recursion starts here:
  363. return n * factorial (n-1);
  364. }
  365.  
  366. factorial(10);
  367.  
  368. // output: 3628800
  369.  
  370.  
  371.  
  372. //--------------------------------------------------------------------------------
  373. //Exercise: Constructors
  374. //--------------------------------------------------------------------------------
  375.  
  376. class Surgeon {
  377. constructor(name, department) {
  378. // method on a class that runs everytime the clas is called- it's purpose is to CONSTRUCT the class when //you instantiate.
  379.  
  380. class HospitalEmployee {
  381. constructor(name) {
  382. this._name = name; // The this keyword acts like a pronoun does in an English sentence
  383. this._remainingVacationDays = 20;
  384. }
  385.  
  386. get name() {
  387. return this._name;
  388. }
  389.  
  390. get remainingVacationDays() {
  391. return this._remainingVacationDays;
  392. }
  393.  
  394. takeVacationDays(daysOff) {
  395. this._remainingVacationDays -= daysOff;
  396. }
  397.  
  398. static generatePassword() {
  399. return Math.floor(Math.random()*10000);
  400. }
  401. }
  402.  
  403. class Nurse extends HospitalEmployee {
  404. constructor(name, certifications) {
  405. super(name);
  406. this._certifications = certifications;
  407. }
  408.  
  409. get certifications() {
  410. return this._certifications;
  411. }
  412.  
  413. addCertification(newCertification) {
  414. this.certifications.push(newCertification);
  415. }
  416. }
  417.  
  418. const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
  419. nurseOlynyk.takeVacationDays(5);
  420. console.log(nurseOlynyk.remainingVacationDays);
  421. nurseOlynyk.addCertification('Genetics');
  422. console.log(nurseOlynyk.certifications);
  423.  
  424.  
  425. //--------------------------------------------------------------------------------
  426. //Exercise: Arrays
  427. //--------------------------------------------------------------------------------
  428.  
  429. var cars = [
  430. 'Saab',
  431. 'Volvo',
  432. 'BMW'
  433. ];
  434.  
  435. cars.push('honda'); // pushes honda into the end of the array
  436.  
  437. console.log(cars[3]); // calls an element from the array
  438.  
  439. console.log(cars.length); // logs the length of the array
  440.  
  441.  
  442. //--------------------------------------------------------------------------------
  443. //Exercise: Functions
  444. //--------------------------------------------------------------------------------
  445.  
  446. // A Function is a block of code designed to perform a task.
  447.  
  448.  
  449. // Function Declarations - a function that is bound to an identifier or name.
  450. const isGreaterThan = (numberOne, numberTwo)=> {
  451. if (numberOne > numberTwo){
  452. return true;
  453. } else {
  454. return false;
  455. }
  456. }
  457.  
  458. console.log(isGreaterThan(16, 20));
  459.  
  460. //Function Expressions - similar to function declaration, but the identifier can be omitted, which
  461. //creates an anonymous function. Function expressions are also often stored in a variable.
  462. const square = function (number) {
  463. return number * number;
  464. };
  465.  
  466. console.log(square(5));
  467. // Output: 25.
  468.  
  469. //Arrow Functions - Refactored function syntax
  470. const isGreaterThan = (numberOne, numberTwo)=> {
  471. if (numberOne > numberTwo){
  472. return true;
  473. } else {
  474. return false;
  475. }
  476. }
  477.  
  478. console.log(isGreaterThan(16, 20));
  479.  
  480.  
  481.  
  482.  
  483.  
  484. </script></body>
  485. </html>
Add Comment
Please, Sign In to add comment