js.html 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="x-ua-compatible" content="ie=edge">
  6. <title>TermaDev - Javascript</title>
  7. <meta name="description" content="">
  8. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9. <link rel="stylesheet" href="resources/css/html_style.css" type="text/css" media="screen" />
  10. </head>
  11. <body>
  12. <div class="front">
  13. <header>
  14. <nav class="site-name">
  15. <a href="/">TermaDev</a>
  16. </nav>
  17. <nav class="links">
  18. <a href="/html.html">HTML</a>
  19. <a href="/css.html">CSS</a>
  20. <a href="/js.html" class="current">JS</a>
  21. </nav>
  22. </header>
  23. <main class="content">
  24. <span>
  25. <nav class="sidebar">
  26. <ul>
  27. <li><a href="#fundamentals">Fundamentals</a></li>
  28. <li><a href="#functions">Functions</a></li>
  29. <li><a href="#iterators">Iterators</a></li>
  30. <li><a href="#requests">Requests</a></li>
  31. </ul>
  32. </nav>
  33. </span>
  34. <span class="content">
  35. <h1>Javascript (JS)</h1>
  36. <section id="fundamentals">
  37. <h2>Fundamentals</h2>
  38. <article>
  39. <h3>console.log()</h3>
  40. <p>
  41. The console.log() method is used to log or print messages to the console. It can also be
  42. used
  43. to print objects and
  44. other info.
  45. </p>
  46. <code class="center">
  47. console.log('Hi there!');
  48. // Prints: Hi there!
  49. </code>
  50. </article>
  51. <article>
  52. <h3>JavaScript</h3>
  53. <p>
  54. JavaScript is a programming language that powers the dynamic behavior on most websites.
  55. Alongside HTML and CSS, it
  56. is a core technology that makes the web run.
  57. </p>
  58. </article>
  59. <article>
  60. <h3>Methods</h3>
  61. <p>
  62. Methods return information about an object, and are called by appending an instance with a
  63. period ., the method
  64. name, and parentheses.
  65. </p>
  66. <code class="center">
  67. // Returns a number between 0 and 1
  68. Math.random();
  69. </code>
  70. </article>
  71. <article>
  72. <h3>Built-in Objects</h3>
  73. <p>
  74. Built-in objects contain methods that can be called by appending the object name with a
  75. period ., the method name,
  76. and a set of parentheses.
  77. </p>
  78. <code class="center">
  79. Math.random();
  80. // ☝️ Math is the built-in object
  81. </code>
  82. </article>
  83. <article>
  84. <h3>Numbers</h3>
  85. <p>
  86. Numbers are a primitive data type. They include the set of all integers and floating point
  87. numbers.
  88. </p>
  89. <code class="center">
  90. let amount = 6;
  91. let price = 4.99;
  92. </code>
  93. </article>
  94. <article>
  95. <h3>String .length</h3>
  96. <p>
  97. The .length property of a string returns the number of characters that make up the string.
  98. </p>
  99. <code class="center">
  100. let message = 'good nite';
  101. console.log(message.length);
  102. // Prints: 9
  103. console.log('howdy'.length);
  104. // Prints: 5
  105. </code>
  106. </article>
  107. <article>
  108. <h3>Data Instances</h3>
  109. <p>
  110. When a new piece of data is introduced into a JavaScript program, the program keeps track of
  111. it in an instance of
  112. that data type. An instance is an individual case of a data type.
  113. </p>
  114. </article>
  115. <article>
  116. <h3>Booleans</h3>
  117. <p>
  118. Booleans are a primitive data type. They can be either true or false.
  119. </p>
  120. <code class="center">
  121. let lateToWork = true;
  122. </code>
  123. </article>
  124. <article>
  125. <h3>Math.random()</h3>
  126. <p>
  127. The Math.random() method returns a floating-point, random number in the range from 0
  128. (inclusive) up to but not
  129. including 1.
  130. </p>
  131. <code class="center">
  132. console.log(Math.random());
  133. // Prints: 0 - 0.9999999999999999
  134. </code>
  135. </article>
  136. <article>
  137. <h3>Math.floor()</h3>
  138. <p>
  139. The Math.floor() function returns the largest integer less than or equal to the given
  140. number.
  141. </p>
  142. <code class="center">
  143. console.log(Math.floor(5.95));
  144. // Prints: 5
  145. </code>
  146. </article>
  147. <article>
  148. <h3>Single Line Comments</h3>
  149. <p>
  150. In JavaScript, single-line comments are created with two consecutive forward slashes //.
  151. </p>
  152. <code class="center">
  153. // This line will denote a comment
  154. </code>
  155. </article>
  156. <article>
  157. <h3>Null</h3>
  158. <p>
  159. Null is a primitive data type. It represents the intentional absence of value. In code, it
  160. is represented as null.
  161. </p>
  162. <code class="center">
  163. let x = null;
  164. </code>
  165. </article>
  166. <article>
  167. <h3>Strings</h3>
  168. <p>
  169. Strings are a primitive data type. They are any grouping of characters (letters, spaces,
  170. numbers, or symbols)
  171. surrounded by single quotes ' or double quotes ".
  172. </p>
  173. <code class="center">
  174. let single = 'Wheres my bandit hat?';
  175. let double = "Wheres my bandit hat?";
  176. </code>
  177. </article>
  178. <article>
  179. <h3>Arithmetic Operators</h3>
  180. <p>
  181. JavaScript supports arithmetic operators for:
  182. </p>
  183. <ul>
  184. <li>+ addition</li>
  185. <li>- substraction</li>
  186. <li>* multiplication</li>
  187. <li>/ division</li>
  188. <li>% modulo</li>
  189. </ul>
  190. <code class="center">
  191. // Addition
  192. 5 + 5
  193. // Subtraction
  194. 10 - 5
  195. // Multiplication
  196. 5 * 10
  197. // Division
  198. 10 / 5
  199. // Modulo
  200. 10 % 5
  201. </code>
  202. </article>
  203. <article>
  204. <h3>Multi-line Comments</h3>
  205. <p>
  206. In JavaScript, multi-line comments are created by surrounding the lines with /* at the
  207. beginning and */ at the end.
  208. Comments are good ways for a variety of reasons like explaining a code block or indicating
  209. some hints, etc.
  210. </p>
  211. <code class="center">
  212. /*
  213. The below configuration must be
  214. changed before deployment.
  215. */
  216. let baseUrl = 'localhost/taxwebapp/country';
  217. </code>
  218. </article>
  219. <article>
  220. <h3>Remainder / Modulo Operator</h3>
  221. <p>
  222. The remainder operator, sometimes called modulo, returns the number that remains after the
  223. right-hand number divides
  224. into the left-hand number as many times as it evenly can.
  225. </p>
  226. <code class="center">
  227. // calculates # of weeks in a year, rounds down to nearest integer
  228. const weeksInYear = Math.floor(365/7);
  229. // calcuates the number of days left over after 365 is divded by 7
  230. const daysLeftOver = 365 % 7 ;
  231. console.log("A year has " + weeksInYear + " weeks and " + daysLeftOver + " days");
  232. </code>
  233. </article>
  234. <article>
  235. <h3>Assignment Operators</h3>
  236. <p>
  237. An assignment operator assigns a value to its left operand based on the value of its right
  238. operand. Here are some of
  239. them:
  240. </p>
  241. <ul>
  242. <li>+= addition assignment</li>
  243. <li>-= substraction assignment</li>
  244. <li>*= multiplication assignment</li>
  245. <li>/= division assignment</li>
  246. </ul>
  247. <code class="center">
  248. let number = 100;
  249. // Both statements will add 10
  250. number = number + 10;
  251. number += 10;
  252. console.log(number);
  253. // Prints: 120
  254. </code>
  255. </article>
  256. <article>
  257. <h3>String Interpolation</h3>
  258. <p>
  259. String interpolation is the process of evaluating string literals containing one or more
  260. placeholders (expressions,
  261. variables, etc).
  262. </p>
  263. <p>
  264. It can be performed using template literals: text ${expression} text.
  265. </p>
  266. <code class="center">
  267. let age = 7;
  268. // String concatenation
  269. 'Tommy is ' + age + ' years old.';
  270. // String interpolation
  271. `Tommy is ${age} years old.`;
  272. </code>
  273. </article>
  274. <article>
  275. <h3>Variables</h3>
  276. <p>
  277. Variables are used whenever there’s a need to store a piece of data. A variable contains
  278. data that can be used in
  279. the program elsewhere. Using variables also ensures code re-usability since it can be used
  280. to replace the same value
  281. in multiple places.
  282. </p>
  283. <code class="center">
  284. const currency = '$';
  285. let userIncome = 85000;
  286. console.log(currency + userIncome + ' is more than the average income.');
  287. // Prints: $85000 is more than the average income.
  288. </code>
  289. </article>
  290. <article>
  291. <h3>Undefined</h3>
  292. <p>
  293. undefined is a primitive JavaScript value that represents lack of defined value. Variables
  294. that are declared but not
  295. initialized to a value will have the value undefined.
  296. </p>
  297. <code class="center">
  298. var a;
  299. console.log(a);
  300. // Prints: undefined
  301. </code>
  302. </article>
  303. <article>
  304. <h3>Learn Javascript: Variables</h3>
  305. <p>
  306. A variable is a container for data that is stored in computer memory. It is referenced by a
  307. descriptive name that a
  308. programmer can call to assign a specific value and retrieve it.
  309. </p>
  310. <code class="center">
  311. // Examples of variables
  312. let name = "Tammy";
  313. const found = false;
  314. var age = 3;
  315. console.log(name, found, age);
  316. // Prints: Tammy false 3
  317. </code>
  318. </article>
  319. <article>
  320. <h3>Declaring Variables</h3>
  321. <p>
  322. To declare a variable in JavaScript, any of these three keywords can be used along with a
  323. variable name:
  324. </p>
  325. <ul>
  326. <li>var is used in pre-ES6 versions of JavaScript.</li>
  327. <li>let is the preferred way to declare a variable when it can be reassigned.</li>
  328. <li>const is the preferred way to declare a variable with a constant value.</li>
  329. </ul>
  330. <code class="center">
  331. var age;
  332. let weight;
  333. const numberOfFingers = 20;
  334. </code>
  335. </article>
  336. <article>
  337. <h3>Template Literals</h3>
  338. <p>
  339. Template literals are strings that allow embedded expressions, ${expression}. While regular
  340. strings use single ' or
  341. double " quotes, template literals use backticks instead.
  342. </p>
  343. <code class="center">
  344. let name = "Codecademy";
  345. console.log(`Hello, ${name}`);
  346. // Prints: Hello, Codecademy
  347. console.log(`Billy is ${6+8} years old.`);
  348. // Prints: Billy is 14 years old.
  349. </code>
  350. </article>
  351. <article>
  352. <h3>let Keyword</h3>
  353. <p>
  354. let creates a local variable in JavaScript & can be re-assigned. Initialization during the
  355. declaration of a let
  356. variable is optional. A let variable will contain undefined if nothing is assigned to it.
  357. </p>
  358. <code class="center">
  359. let count;
  360. console.log(count); // Prints: undefined
  361. count = 10;
  362. console.log(count); // Prints: 10
  363. </code>
  364. </article>
  365. <article>
  366. <h3>const Keyword</h3>
  367. <p>
  368. A constant variable can be declared using the keyword const. It must have an assignment. Any
  369. attempt of re-assigning
  370. a const variable will result in JavaScript runtime error.
  371. </p>
  372. <code class="center">
  373. const numberOfColumns = 4;
  374. numberOfColumns = 8;
  375. // TypeError: Assignment to constant variable.
  376. </code>
  377. </article>
  378. <article>
  379. <h3>String Concatenation</h3>
  380. <p>
  381. In JavaScript, multiple strings can be concatenated together using the + operator. In the
  382. example, multiple strings
  383. and variables containing string values have been concatenated. After execution of the code
  384. block, the displayText
  385. variable will contain the concatenated string.
  386. </p>
  387. <code class="center">
  388. let service = 'credit card';
  389. let month = 'May 30th';
  390. let displayText = 'Your ' + service + ' bill is due on ' + month + '.';
  391. console.log(displayText);
  392. // Prints: Your credit card bill is due on May 30th.
  393. </code>
  394. </article>
  395. </section>
  396. <section id="functions">
  397. <h2>Functions</h2>
  398. <article>
  399. <h3>Arrow Functions (ES6)</h3>
  400. <p>
  401. Arrow function expressions were introduced in ES6. These expressions are clean and concise.
  402. The syntax for an arrow
  403. function expression does not require the function keyword and uses a fat arrow => to
  404. separate the parameter(s) from
  405. the body.
  406. </p>
  407. <p>
  408. There are several variations of arrow functions:
  409. </p>
  410. <ul>
  411. <li>
  412. Arrow functions with a single parameter do not require () around the parameter list.
  413. </li>
  414. <li>
  415. Arrow functions with a single expression can use the concise function body which returns
  416. the result of the
  417. expression without the return keyword.
  418. </li>
  419. </ul>
  420. <code class="center">
  421. // Arrow function with two parameters
  422. const sum = (firstParam, secondParam) => {
  423. return firstParam + secondParam;
  424. };
  425. console.log(sum(2,5)); // Prints: 7
  426. // Arrow function with no parameters
  427. const printHello = () => {
  428. console.log('hello');
  429. };
  430. printHello(); // Prints: hello
  431. // Arrow functions with a single parameter
  432. const checkWeight = weight => {
  433. console.log(`Baggage weight : ${weight} kilograms.`);
  434. };
  435. checkWeight(25); // Prints: Baggage weight : 25 kilograms.
  436. // Concise arrow functions
  437. const multiply = (a, b) => a * b;
  438. console.log(multiply(2, 30)); // Prints: 60
  439. </code>
  440. </article>
  441. <article>
  442. <h3>Functions</h3>
  443. <p>
  444. Functions are one of the fundamental building blocks in JavaScript. A function is a reusable
  445. set of statements to
  446. perform a task or calculate a value. Functions can be passed one or more values and can
  447. return a value at the end of
  448. their execution. In order to use a function, you must define it somewhere in the scope where
  449. you wish to call it.
  450. </p>
  451. <p>
  452. The example code provided contains a function that takes in 2 values and returns the sum of
  453. those numbers.
  454. </p>
  455. <code class="center">
  456. // Defining the function:
  457. function sum(num1, num2) {
  458. return num1 + num2;
  459. }
  460. // Calling the function:
  461. sum(3, 6); // 9
  462. </code>
  463. </article>
  464. <article>
  465. <h3>Anonymous Functions</h3>
  466. <p>
  467. Anonymous functions in JavaScript do not have a name property. They can be defined using the
  468. function keyword, or as
  469. an arrow function. See the code example for the difference between a named function and an
  470. anonymous function.
  471. </p>
  472. <code class="center">
  473. // Named function
  474. function rocketToMars() {
  475. return 'BOOM!';
  476. }
  477. // Anonymous function
  478. const rocketToMars = function() {
  479. return 'BOOM!';
  480. }
  481. </code>
  482. </article>
  483. <article>
  484. <h3>Function Expressions</h3>
  485. <p>
  486. Function expressions create functions inside an expression instead of as a function
  487. declaration. They can be
  488. anonymous and/or assigned to a variable.
  489. </p>
  490. <code class="center">
  491. const dog = function() {
  492. return 'Woof!';
  493. }
  494. </code>
  495. </article>
  496. <article>
  497. <h3>Function Parameters</h3>
  498. <p>
  499. Inputs to functions are known as parameters when a function is declared or defined.
  500. Parameters are used as variables
  501. inside the function body. When the function is called, these parameters will have the value
  502. of whatever is passed in
  503. as arguments. It is possible to define a function without parameters.
  504. </p>
  505. <code class="center">
  506. // The parameter is name
  507. function sayHello(name) {
  508. return `Hello, ${name}!`;
  509. }
  510. </code>
  511. </article>
  512. <article>
  513. <h3>return Keyword</h3>
  514. <p>
  515. Functions return (pass back) values using the return keyword. return ends function execution
  516. and returns the
  517. specified value to the location where it was called. A common mistake is to forget the
  518. return keyword, in which case
  519. the function will return undefined by default.
  520. </p>
  521. <code class="center">
  522. // With return
  523. function sum(num1, num2) {
  524. return num1 + num2;
  525. }
  526. // Without return, so the function doesn't output the sum
  527. function sum(num1, num2) {
  528. num1 + num2;
  529. }
  530. </code>
  531. </article>
  532. <article>
  533. <h3>Function Declaration</h3>
  534. <p>
  535. Function declarations are used to create named functions. These functions can be called
  536. using their declared name.
  537. Function declarations are built from:
  538. </p>
  539. <ul>
  540. <li>The function keyword.</li>
  541. <li>The function name.</li>
  542. <li>An optional list of parameters separated by commas enclosed by a set of parentheses ().
  543. </li>
  544. <li>A function body enclosed in a set of curly braces {}.</li>
  545. </ul>
  546. <code class="center">
  547. function add(num1, num2) {
  548. return num1 + num2;
  549. }
  550. </code>
  551. </article>
  552. <article>
  553. <h3>Calling Functions</h3>
  554. <p>
  555. Functions can be called, or executed, elsewhere in code using parentheses following the
  556. function name. When a
  557. function is called, the code inside its function body runs. Arguments are values passed into
  558. a function when it is
  559. called.
  560. </p>
  561. <code class="center">
  562. // Defining the function
  563. function sum(num1, num2) {
  564. return num1 + num2;
  565. }
  566. // Calling the function
  567. sum(2, 4); // 6
  568. </code>
  569. </article>
  570. </section>
  571. <section id="iterators">
  572. <h2>Iterators</h2>
  573. <article>
  574. <h3>Functions Assigned to Variables</h3>
  575. <p>
  576. In JavaScript, functions are a data type just as strings, numbers, and arrays are data
  577. types. Therefore, functions
  578. can be assigned as values to variables, but are different from all other data types because
  579. they can be invoked.
  580. </p>
  581. <code class="center">
  582. let plusFive = (number) => {
  583. return number + 5;
  584. };
  585. // f is assigned the value of plusFive
  586. let f = plusFive;
  587. plusFive(3); // 8
  588. // Since f has a function value, it can be invoked.
  589. f(9); // 14
  590. </code>
  591. </article>
  592. <article>
  593. <h3>Callback Functions</h3>
  594. <p>
  595. In JavaScript, a callback function is a function that is passed into another function as an
  596. argument. This function
  597. can then be invoked during the execution of that higher order function (that it is an
  598. argument of).
  599. </p>
  600. <p>
  601. Since, in JavaScript, functions are objects, functions can be passed as arguments.
  602. </p>
  603. <code class="center">
  604. const isEven = (n) => {
  605. return n % 2 == 0;
  606. }
  607. let printMsg = (evenFunc, num) => {
  608. const isNumEven = evenFunc(num);
  609. console.log(`The number ${num} is an even number: ${isNumEven}.`)
  610. }
  611. // Pass in isEven as the callback function
  612. printMsg(isEven, 4);
  613. // Prints: The number 4 is an even number: True.
  614. </code>
  615. </article>
  616. <article>
  617. <h3>Higher-Order Functions</h3>
  618. <p>
  619. In Javascript, functions can be assigned to variables in the same way that strings or arrays
  620. can. They can be passed
  621. into other functions as parameters or returned from them as well.
  622. </p>
  623. <p>
  624. A “higher-order function” is a function that accepts functions as parameters and/or returns
  625. a function.
  626. </p>
  627. </article>
  628. <article>
  629. <h3>JavaScript Functions: First-Class Objects</h3>
  630. <p>
  631. JavaScript functions are first-class objects. Therefore:
  632. </p>
  633. <ul>
  634. <li>They have built-in properties and methods, such as the name property and the .toString()
  635. method.</li>
  636. <li>Properties and methods can be added to them.</li>
  637. <li>They can be passed as arguments and returned from other functions.</li>
  638. <li>They can be assigned to variables, array elements, and other objects.</li>
  639. </ul>
  640. <code class="center">
  641. //Assign a function to a variable originalFunc
  642. const originalFunc = (num) => { return num + 2 };
  643. //Re-assign the function to a new variable newFunc
  644. const newFunc = originalFunc;
  645. //Access the function's name property
  646. newFunc.name; //'originalFunc'
  647. //Return the function's body as a string
  648. newFunc.toString(); //'(num) => { return num + 2 }'
  649. //Add our own isMathFunction property to the function
  650. newFunc.isMathFunction = true;
  651. //Pass the function as an argument
  652. const functionNameLength = (func) => { return func.name.length };
  653. functionNameLength(originalFunc); //12
  654. //Return the function
  655. const returnFunc = () => { return newFunc };
  656. returnFunc(); //[Function: originalFunc]
  657. </code>
  658. </article>
  659. <article>
  660. <h3>The .reduce() Method</h3>
  661. <p>
  662. The .reduce() method iterates through an array and returns a single value.
  663. </p>
  664. <p>
  665. In the above code example, the .reduce() method will sum up all the elements of the array.
  666. It takes a callback
  667. function with two parameters (accumulator, currentValue) as arguments. On each iteration,
  668. accumulator is the value
  669. returned by the last iteration, and the currentValue is the current element. Optionally, a
  670. second argument can be
  671. passed which acts as the initial value of the accumulator.
  672. </p>
  673. <code class="center">
  674. const arrayOfNumbers = [1, 2, 3, 4];
  675. const sum = arrayOfNumbers.reduce((accumulator, currentValue) => {
  676. return accumulator + currentValue;
  677. });
  678. console.log(sum); // 10
  679. </code>
  680. </article>
  681. <article>
  682. <h3>The .forEach() Method</h3>
  683. <p>
  684. The .forEach() method executes a callback function on each of the elements in an array in
  685. order.
  686. </p>
  687. <p>
  688. In the above example code, the callback function containing a console.log() method will be
  689. executed 5 times, once
  690. for each element.
  691. </p>
  692. <code class="center">
  693. const numbers = [28, 77, 45, 99, 27];
  694. numbers.forEach(number => {
  695. console.log(number);
  696. });
  697. </code>
  698. </article>
  699. <article>
  700. <h3>The .filter() Method</h3>
  701. <p>
  702. The .filter() method executes a callback function on each element in an array. The callback
  703. function for each of the
  704. elements must return either true or false. The returned array is a new array with any
  705. elements for which the
  706. callback function returns true.
  707. </p>
  708. <p>
  709. In the above code example, the array filteredArray will contain all the elements of
  710. randomNumbers but 4.
  711. </p>
  712. <code class="center">
  713. const randomNumbers = [4, 11, 42, 14, 39];
  714. const filteredArray = randomNumbers.filter(n => {
  715. return n > 5;
  716. });
  717. </code>
  718. </article>
  719. <article>
  720. <h3>The .map() Method</h3>
  721. <p>
  722. The .map() method executes a callback function on each element in an array. It returns a new
  723. array made up of the
  724. return values from the callback function.
  725. </p>
  726. <p>
  727. The original array does not get altered, and the returned array may contain different
  728. elements than the original
  729. array.
  730. </p>
  731. <p>
  732. In the example code above, the .map() method is used to add ' joined the contest.' string at
  733. the end of each element
  734. in the finalParticipants array.
  735. </p>
  736. <code class="center">
  737. const finalParticipants = ['Taylor', 'Donald', 'Don', 'Natasha', 'Bobby'];
  738. // add string after each final participant
  739. const announcements = finalParticipants.map(member => {
  740. return member + ' joined the contest.';
  741. })
  742. console.log(announcements);
  743. </code>
  744. </article>
  745. </section>
  746. <section id="requests">
  747. <h2>Requests</h2>
  748. <article>
  749. <h3>JSON-Formatted Response Body</h3>
  750. <p>
  751. The .json() method will resolve a returned promise to a JSON object, parsing the body text
  752. as JSON.
  753. </p>
  754. <p>
  755. In the example code, the .json() method is used on the response object which returns a
  756. promise to a JSON-formatted
  757. response body as jsonResponse.
  758. </p>
  759. <code class="center">
  760. fetch('url')
  761. .then(
  762. response => response.json()
  763. ).then(jsonResponse => {
  764. console.log(jsonResponse);
  765. });
  766. </code>
  767. </article>
  768. <article>
  769. <h3>HTTP GET Request</h3>
  770. <p>
  771. HTTP GET requests are made with the intention of retrieving information or data from a
  772. source (server) over the web.
  773. </p>
  774. <p>
  775. GET requests have no body, so the information that the source requires, in order to return
  776. the proper response, must
  777. be included in the request URL path or query string.
  778. </p>
  779. </article>
  780. <article>
  781. <h3>The fetch() Function</h3>
  782. <p>
  783. The JavaScript Fetch API is used to write HTTP requests using Promises. The main fetch()
  784. function accepts a URL
  785. parameter and returns a promise that resolves to a response object or rejects with an error
  786. message if a network
  787. error occurs.
  788. </p>
  789. <p>
  790. The example code begins by calling the fetch() function. Then a then() method is chained to
  791. the end of the fetch().
  792. It ends with the response callback to handle success and the rejection callback to handle
  793. failure.
  794. </p>
  795. <code class="center">
  796. fetch('url')
  797. .then(
  798. response => {
  799. console.log(response);
  800. },
  801. rejection => {
  802. console.error(rejection.message);
  803. );
  804. </code>
  805. </article>
  806. <article>
  807. <h3>Customizing Fetch Requests</h3>
  808. <p>
  809. The fetch() function accepts an optional second argument, an options object, used to
  810. customize the request. This can
  811. be used to change the request type, headers, specify a request body, and much more.
  812. </p>
  813. <p>
  814. In the example code below, the fetch() function as a second argument—an object containing
  815. options for the fetch
  816. request specifying the method and the body.
  817. </p>
  818. <code class="center">
  819. fetch('https://api-to-call.com/endpoint', {
  820. method: 'POST',
  821. body: JSON.stringify({id: "200"})
  822. }).then(response => {
  823. if(response.ok){
  824. return response.json();
  825. }
  826. throw new Error('Request failed!');
  827. }, networkError => {
  828. console.log(networkError.message);
  829. }).then(jsonResponse => {
  830. console.log(jsonResponse);
  831. })
  832. </code>
  833. </article>
  834. <article>
  835. <h3>HTTP POST Request</h3>
  836. <p>
  837. HTTP POST requests are made with the intention of sending new information to the source
  838. (server) that will receive
  839. it.
  840. </p>
  841. <p>
  842. For a POST request, the new information is stored in the body of the request.
  843. </p>
  844. </article>
  845. <article>
  846. <h3>Using async…await with Fetch</h3>
  847. <p>
  848. The async…await syntax is used with the Fetch API to handle promises.
  849. </p>
  850. <p>
  851. In the example code, the async keyword is used to make the getSuggestions() function an
  852. async
  853. function. This means
  854. that the function will return a promise. The await keyword used before the fetch() call
  855. makes
  856. the code wait until
  857. the promise is resolved.
  858. </p>
  859. <code class="center">
  860. const getSuggestions = async () => {
  861. const wordQuery = inputField.value;
  862. const endpoint = `${url}${queryParams}${wordQuery}`;
  863. try{
  864. const response = await fetch(endpoint, {cache: 'no-cache'});
  865. if(response.ok){
  866. const jsonResponse = await response.json()
  867. }
  868. }
  869. catch(error){
  870. console.log(error)
  871. }
  872. }
  873. </code>
  874. </article>
  875. </section>
  876. </span>
  877. </main>
  878. <footer><span><a href="https://notabug.org/anton.puiu" target="_blank"><svg width="62px" height="62px"
  879. viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
  880. <style>
  881. .st0 {
  882. fill: #fff
  883. }
  884. </style>
  885. <g id="Icon">
  886. <circle id="Background" cx="512" cy="512" r="512" />
  887. <path class="st0"
  888. d="M560 256c.6-1.8 2.5-3.2 4.4-2.8 1.5.4 2.1 2 2.4 3.4 5.2 23.2 7 47.3 3.8 71-1.7-11.1-3.4-22.1-5-33.2-.6-7.8-2.5-15.5-3.6-23.2-5.4 21.1-12.1 42.2-23.9 60.7-1.2 2.1-2.9 3.9-3.3 6.3-3.5 42.9-7 85.8-10.5 128.8 8.9 4 16.1 11.3 19.7 20.4 27.8-29.3 54-60.3 76.1-94 50.8-9.7 96.9-33.8 144.4-53.1 13.6 13.2 26.9 26.8 39.2 41.2 3.9 4.9 8.3 9.5 10.7 15.4-17.2-16.1-34.2-32.6-51.4-48.8-32.6 12.9-61.7 33.3-94.4 46-11.6 4.6-23.7 7.9-35.9 10.4-2.3.7-5.3.5-6.7 2.9-6.4 8.7-12.3 17.8-19.3 26.1-18.6 26.3-39.5 50.8-58.6 76.7 0 .8 0 1.7.1 2.5 23.4-6.8 46.5-14.2 69.8-21.1 5.7-.6 11.5-.1 17.3-.4 3.3-.4 5.8 2.1 8.6 3.4 24.1 12.7 51 18.8 77.5 23.8 5.2 1.2 10.7 1.6 15.9 3.2 3.4 2.2 7.2 4.3 9.5 7.7 11.5 15.5 22.6 32.3 26 51.6-6.5-14.7-16.1-27.9-27.2-39.4-2.2-2.1-4.1-5-7.2-5.9-33-12.5-67.7-19.1-102.1-26.6-6.1-1.2-12.1-3.2-18.4-3.6-4.8 1.2-9.2 3.4-13.8 5.1-21.2 8-42.3 16.1-63.6 23.9-1.7.5-1.8 2.7-2.5 4.1 11.2 1 22.5 1.7 33.5 4 .9.3 2.5.3 2.5 1.7 3.4 15 10.7 28.8 18.6 41.9 6.3 10.6 13.5 20.6 20.1 31 2.1 3.5 4.4 7 5.2 11-.3.5-1 1.7-1.4 2.2 7.4 16.6 12 34.4 13.6 52.5.4 3.7.7 7.3 1.6 10.9-.5-.9-1.3-1.7-1.6-2.7l-.1-.8c-.3-4.4-1.8-8.7-2.7-13-3.6-13.2-7.5-26.6-14.9-38.2-.7-1.9-3.4-1-4.3-2.7-8.2-10.2-13-22.5-19-34-7.8-15.3-15-31-25.4-44.7-10-3.2-20-6.2-30-9.4-.6 1.3-1.1 2.6-1.6 3.9 4.1 1.6 8.3 2.9 12.4 4.4-4 19.6-6.9 39.3-10.6 58.9-2 9.7-3.7 19.5-7.1 28.9-1 2.5-2.1 5.4-4.9 6.4-6.5 16.8-13.8 33.3-21 49.8-8.7 19.7-17.5 39.3-27 58.7-2.4 4.7-4.6 9.5-7.7 13.8 14-40.3 31.6-79.3 45.8-119.6-2-6.1.2-12.5 1.7-18.4 7.4-24.6 18.3-48 26.1-72.5-3.7-1.5-7.3-3.2-11-4.4-6.7 8.3-10.8 18.4-18.1 26.2-4.3 4.8-8.8 9.7-14.3 13.2-9.5 6.6-19.9 11.9-30.4 17-10.6 4.7-22.1 8.4-33.8 7.9-9.4-.8-19.1-4.3-25-11.9-9.2-11.6-12.9-26.4-14.9-40.8-2.9-3.9-6.3-7.6-7.7-12.4-3.4-9.6-1.1-20.9 5.7-28.5 1.8-2.3 4.3-3.7 6.3-5.8 4.5-12.6 12.2-23.9 21.6-33.5-12.5-8.8-24.2-18.9-38.1-25.5-17.7 10.4-34.1 22.8-50.9 34.5-22.3 15.9-45.2 31.5-71.1 41.1-2.3.4-2.1 2.9-2.3 4.6-.7 18.3-2.4 36.6-3.3 54.9-.7 6.9-.3 14-1.9 20.8-.9 3.3-3.1 6-5.1 8.7-.4-16.8.7-33.7.9-50.5 1.2-19.7 1.5-39.5 4.7-59 15.2-5.3 30.3-11 44.8-18.2 27.8-13.2 52.9-31.3 77.5-49.6 6.4-.4 12.7 1.6 18.5 4.2 8.4 3.5 16 8.6 23.7 13.4 4.7 2.9 8.9 6.6 14 8.9 1.7-.8 2.9-2.3 4.3-3.5-13.1-11.8-27.2-22.5-39.4-35.3-2.7-2.5-4.9-5.5-7.7-7.8-16.7-12.6-33-25.9-51.4-36-2.5-1-5.2-.8-7.8-1.2-14.7-1.6-29.2-4.5-44-6-4.3-.3-8.4-1.5-12.7-1.3-4.2.1-8.3-1.6-11.1-4.8 12 .5 23.9 2.1 35.9 2.9 13.6 1.4 27.3 2 40.8 3.6 13 6.8 25.1 15.1 37.7 22.6 10.2 5.9 20.4 12.2 31.6 16 2.3.8 4.7 1.3 7.2 1.6 7.8 8.9 14.5 19 23.9 26.4 3.9 2.9 6.5 7.2 7.7 11.9 9.1-5.7 20.1-6.5 30.6-7-5.7-25.5-12.1-50.7-18.7-76-.7-2.3-2.6-3.8-3.9-5.7-13.1-17.2-21.4-37.5-30.6-56.9-5.8-7.7-11.5-15.6-16.2-24.1-1.2-2.3-2.6-4.6-2.7-7.2 8.1 8.4 17.5 15.4 25.7 23.6 2.7 2.6 4.4 6 6.3 9.1 8.5 14.3 15.6 29.3 24 43.6 1.4 2.6 3.3 4.9 4.3 7.7 8.7 29.1 19.7 57.5 29.4 86.3 5.6.5 11.1 1.3 16.6 2.4 5.8-42 10.6-84.2 16.8-126.1 1.6-5.2 4.9-9.8 7-14.8 10.3-21.7 17.5-44.2 26.8-65.8zM406.2 521.1c-4.8 1.1-9.3 3.5-12.9 6.9-9.4 8.6-11.3 24-4.2 34.5 7 11.4 22.9 16 34.9 9.9 11.7-5.4 18.2-19.6 14.5-32-3.5-13.6-18.6-22.7-32.3-19.3zm62 10c-4.8 1.1-9.3 3.5-12.9 6.9-9.4 8.6-11.2 23.9-4.2 34.5 7 11.4 22.9 16 34.9 9.9 11.7-5.4 18.2-19.6 14.5-32-3.5-13.5-18.6-22.7-32.3-19.3z" />
  889. <path class="st0"
  890. d="M424.4 531.9c6.2-2.5 13.2 4.1 11.2 10.4-1.4 6.3-10.2 8.6-14.5 3.8-4.5-4.1-2.6-12.5 3.3-14.2zm5.4 6.3c-1.9 1.1-1.5 4.2.6 4.9 2 .9 4.4-1.5 3.5-3.5-.5-1.6-2.6-2.4-4.1-1.4zm55.7 5.5c6.2-2.5 13.3 4 11.2 10.4-1.4 6.2-10.1 8.6-14.5 3.9-4.5-4.1-2.6-12.5 3.3-14.3zm5.3 6.4c-1.5 1-1.5 3.5 0 4.5 1.7 1.3 4.3-.2 4.3-2.3.1-2.1-2.5-3.6-4.3-2.2z" />
  891. </g>
  892. </svg>
  893. </a>
  894. </span>
  895. <span>
  896. <a href="mailto:anton.puiu@email.com" target="_blank">
  897. <img src="resources/img/mail.png" width="62px">
  898. </a>
  899. </span>
  900. <span>
  901. <a href="https://github.com/antonpuiu" target="_blank">
  902. <img src="resources/img/github.png" width="62px">
  903. </a>
  904. </span>
  905. </footer>
  906. </div>
  907. </body>
  908. </html>