Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.93 KB | None | 0 0
  1. // Try opening up your terminal and typing 'node'.
  2. // Then in the node REPL, copy and paste the commands in this docuemnt.
  3. // You can copy and paste the entire line of code including the comments into the REPL.
  4.  
  5. // Intro: REPLS
  6. // A REPL is a 'Read Evaluate Print Loop'. It allows you to run commands in JS in your
  7. // terminal and then see the 'printed' result back in your screen.
  8. console.log('You can', 'chain multiple messages', 'inside console.log');
  9.  
  10. // I. Basic data types
  11. // 1. Numbers
  12. 1 + 1; // add
  13. 1 - 1; // subtract
  14. 5 * 5; // multiply
  15. 10 / 2; // divide
  16. 6 % 3; // modulo: the remainder of division
  17.  
  18. // 2. Strings
  19. 'hello world!';
  20. // Provide the number of chartacters in the string
  21. 'hello world!'.length;
  22. // Slice a piece of the string
  23. 'hello world!'.substring(1,5);
  24. // convert the string to upper case
  25. 'hello world!'.toUpperCase();
  26. // convert the string to lower case
  27. 'hello world!'.toLowerCase();
  28. // access a specific character in the string
  29. 'hello world!'[0];
  30.  
  31. //2.1 String interpolation
  32. const message = 'world';
  33. const say = `Hello, ${message}!`; //you must use the ` sign. The ' sign will not allow interpolation.
  34. console.log(say);
  35.  
  36. // 3. Boolean
  37. true;
  38. false;
  39.  
  40. // 4. Null and undefined
  41. // Both these values can be used interchangeably. They describe a variable that has no content.
  42. null;
  43. undefined;
  44.  
  45. // 4. Checking the data type
  46. typeof 1;
  47. typeof 'hello';
  48. typeof true;
  49. typeof false;
  50.  
  51. // 5. Type conversion
  52. // 5.1 convert a string '5' into a number 5.
  53. parseInt('5', 10);
  54.  
  55. // 5.2 convert an integer 5 into a string '5'.
  56. String(5);
  57.  
  58.  
  59.  
  60.  
  61. // II. Variables
  62. // 1. let
  63. // We use let when we want to re-assign a variable. Re-assignment is wehn we use '='.
  64. let fruit = 'banana';
  65. fruit = [1,2,3];
  66.  
  67. // 2. const
  68. // We use const when we do not want to re-assign a variable. It has a unchanging value.
  69. const tropicalFruit = 'pineapple';
  70. tropicalFruit = 'mango'; // This line of code cannot be executed!
  71.  
  72. // Note: In JS we use camelCase when naming variables. In ruby we used snake_case.
  73. // camelcase
  74. anotherVariable = undefined;
  75. // snake_case
  76. another_variable = undefined;
  77.  
  78.  
  79.  
  80.  
  81.  
  82. // III. Arrays
  83. // 1.1 CRUD on arrays
  84. // Create an array
  85. const numbers = [1,2,3,4,5];
  86. // Read from an array
  87. numbers[0];
  88. // Update an array
  89. numbers.push(99); // Add 99 to the array
  90. numbers[0] = 99; // reassigned value at index 0 to 99
  91. numbers.splice(0,0,99); // at index 0, delete 0 items, and insert a number 99.
  92. // Delete from an array
  93. numbers.splice(0,1); // at index 0, delete 1 item.
  94. numbers.splice(0,2); // at index 0, delete 2 items.
  95.  
  96. // 1.2 Other array operations
  97. // Split a string into an array of characters
  98. 'hello world'.split('');
  99.  
  100. // Give an array length
  101. numbers.length;
  102.  
  103. // The spread operator
  104. const male = ['hamlet', 'falstaff'];
  105. const female = ['gertrude', 'ophelia'];
  106. const people = [...male, ...female];
  107. console.log(people);
  108.  
  109. // 1.3 Looping through an array
  110. numbers.forEach( (number, index) => {
  111. console.log(`${number} is at index: ${index}`);
  112. });
  113.  
  114.  
  115.  
  116.  
  117. // III. Control Flow
  118. // 1. If-Else statements
  119. const age = 12;
  120. if (age > 18) {
  121. console.log('can vote');
  122. } else if (age > 99) {
  123. console.log('can vote, and also very old.')l;
  124. } else {
  125. console.log('Too young to vote');
  126. }
  127.  
  128. // 2. Ternary operator
  129. // This operation has two stages:
  130. // stage 1: check if the condition is true (is the variable 'age' > 18?)
  131. // stage 2: depending on the result, assign the value to 'result').
  132. // If the condition passes, result is set to the value to the left ('can vote')
  133. // If the condition does not passe, result is set to the value to the right ('cannot vote')
  134. const result = (age > 18) ? 'can vote' : 'cannot vote';
  135. console.log(result);
  136.  
  137. // 2.2 Falsy and Truthy values
  138. // JS allows some values to function as if they were boolean 'false'.
  139. 1 == false
  140. // => true
  141.  
  142. '' == false
  143. // => true
  144.  
  145.  
  146.  
  147.  
  148. // IV. Objects
  149. // 1. CRUD on objects
  150. // Create an object
  151. const characters = { hamlet: 'hello.', harry: 'hi.'};
  152. // read from an object
  153. chracters.hamlet
  154. characters['hamlet'] // the dot notation is preferred
  155. // update an object
  156. chracters.hamlet = 'hello world';
  157. // delete from an object
  158. delete characters.hamlet
  159.  
  160. // 1.1 Looping through an object
  161. Object.keys(characters).forEach( key => {
  162. console.log(key);
  163. });
  164. Object.values(characters).forEach( value => {
  165. console.log(value);
  166. });
  167.  
  168.  
  169.  
  170.  
  171.  
  172. // V. Maps
  173. // A map is like a JS Object, except you can set any data type as the key.
  174. // In a JS object, the kety MUST be a string.
  175. // 1. CRUD on maps
  176. // Create a map
  177. const myMap = new Map();
  178. myMap.set('hello', 'world'); // create a new key value pair in the map such that ['hello', 'world']
  179. // read from a map
  180. myMap.get('hello');
  181. // update a map
  182. myMap.set('hello', 'some new value');
  183. // delete from an object
  184. myMap.delete('hamlet');
  185.  
  186. // 1.1 Looping through a map
  187. // A map is treated like an array: the first parameter is the 'value', the second parameter is the 'index'.
  188. myMap.forEach( (value, key) => {
  189. console.log(value, key);
  190. });
  191.  
  192.  
  193.  
  194.  
  195.  
  196. // VI. Functions
  197. // 1. Arrow Functions
  198. // I define the function here
  199. const sayHello = () => {
  200. console.log('hello!'); // this is the 'block' passed into the function
  201. };
  202. sayHello(); // I execute the function here
  203.  
  204. // 1.1 Single Line Functions
  205. // If your block (the { ... } of the function) contains 1 line of code, you can write your
  206. // function like this:
  207. const sayHi = () => console.log('hi!');
  208. sayHi();
  209.  
  210.  
  211. // 2. Building a Capitalise function
  212. // JS does not include a .capitalize function in its standards library. Let's make one.
  213. const capitalize = (string) => {
  214. // capitalize the firct character of the word and save it to a variable
  215. const firstLetter = string[0].toUpperCase();
  216. // make sure the rest of the string excluding the first character is lower case, and save it to a variable
  217. const restOfWord = string.substring(1, string.length).toLowerCase();
  218. // return the capitalized first letter + the rest of the word in lower case
  219. return firstLetter + restOfWord;
  220. };
  221. console.log(capitalize('all lower case.'));
  222.  
  223. // 3. Going further: Let's build a function that will capitalize every word in a sentence.
  224. // Try saving this code in a file and run the following command in your terminal:
  225. // node --inspect-brk file_name.js
  226. // This will allow you to pause the script during execution so you can see what is happenning
  227. // in it line by line.
  228. const capitalizeSentence = (string) => {
  229. // Define an array that will collect our capitalised words.
  230. let arr = [];
  231. // Split the string into an array of words
  232. string.split(' ').forEach( word => {
  233. // capitalize the firct character of the word and save it to a variable
  234. let firstLetter = word[0].toUpperCase();
  235. // make sure the rest of the string excluding the first character is lower case, and save it to a variable
  236. let restOfTheWord = word.substring(1).toLowerCase();
  237. // create a new variable that combines the two new variables we just created
  238. let newWord = firstLetter + restOfTheWord;
  239. // push this new variable to the array we defined earlier
  240. arr.push(newWord);
  241. });
  242. // convert the array of capitalized words into a string again
  243. return arr.join(' ');
  244. }
  245. console.log(capitalizeSentence('all lower case.'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement