Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.29 KB | None | 0 0
  1. // this worksheet was generated from the 'Javascript Language Fundamentals' section of Brad Traversy's Modern Javascript Udemy course
  2.  
  3. // Variables **********************************************************************
  4.  
  5. // name 3 keywords for initializing and declaring a variable? var, let, const
  6.  
  7. // which 2 JS variable keywords are block scoped? let and const
  8.  
  9. // declare a variable but don't initialize a value.
  10.  
  11. // let name
  12. // console.log(name)
  13.  
  14. // assign the initialized variable a value.
  15.  
  16. // name = 'John Doe'
  17. // console.log(name)
  18.  
  19. // which type of JS variable must be both declared and initialized at the same time? constant
  20.  
  21. // JS variables can contain what? letters,numbers,underscores,and $
  22.  
  23. // JS variables connot start with what? numbers
  24.  
  25. // Multi-word-vars. Give an example of Comel case, Underscore, and Pascal case. which should you in JS? camelcase: firstName, underscore: first_name, pascal: FirstName
  26.  
  27. // when using const in JS, are mutatable data structures such as arrays and abjects, still mutatable? yes
  28.  
  29. // Data-Types ************************************************************************
  30.  
  31. // Name the six primitive data types in JS:
  32. // string
  33. // number
  34. // boolean
  35. // undefined
  36. // null
  37. // symbol
  38.  
  39. // (true or false) Null is an intentional empty value? true
  40.  
  41. // (true or false) Undefined is variable that has not been assigned a value? true
  42.  
  43. // (true or false) arrays, Object Literals, Functions, and Dates are considered Reference Data Types? true
  44.  
  45. // what can be used to find out a variable's type? typeof()
  46.  
  47. // Type conversion *******************************************************************
  48.  
  49. // Use the String function to convert a number, boolean, date, and an array to a string
  50. // let item = 5
  51. // item = true
  52. // item = ['mon', 'tues', 'wed']
  53. // item = new Date()
  54.  
  55. // item = String(item)
  56.  
  57. // console.log(item)
  58. // console.log(typeof item)
  59.  
  60. // Use the toString method to convert a number to a string
  61.  
  62. // let item = 8
  63. // item = item.toString()
  64. // console.log(item)
  65. // console.log(typeof item)
  66.  
  67. // Use the Number function to convert a string-number, both booleans, and a word string to a number
  68.  
  69. // let it = '9'
  70. // it = true
  71. // it = false
  72. // it = 'word'
  73.  
  74. // it = Number(it)
  75.  
  76. // console.log(it)
  77. // console.log(typeof it)
  78.  
  79. // (true or false) NaN is a value that means not a number. true
  80.  
  81. // (true or false) NaN is what is output when a value can't be converted to a number. true
  82.  
  83. // besides the Number() function, show 2 more ways to convert to a number, using the functions parseInt() and parseFloat()
  84.  
  85. // it = '33'
  86. // it = '39.3376'
  87. // it = parseInt(it)
  88. // it = parseFloat(it)
  89. // console.log(it, typeof it)
  90. // console.log(it.toFixed(2), typeof it)
  91.  
  92. // (true or false) the toFixed method can be used to add decimal places to a number. true
  93.  
  94. // Type coersion *******************************************************************
  95.  
  96. // (true or false) type coersion is when JS automatically does type conversion on a value. this can be unwanted, so you need to be alert to this possibility when coding? true
  97.  
  98. // The Math Object *****************************************************************
  99.  
  100. // use the following methods on the Math object to generate results.
  101.  
  102. // Math.round()
  103. // Math.ceil()
  104. // Math.floor()
  105. // Math.sqrt()
  106. // Math.abs()
  107. // Math.pow()
  108. // Math.min()
  109. // Math.max()
  110. // Math.random()
  111.  
  112. // let num
  113.  
  114. // num = Math.round(12.7)
  115. // num = Math.ceil(12.1)
  116. // num = Math.floor(12.9)
  117. // num = Math.sqrt(64)
  118. // num = Math.abs(-59)
  119. // num = Math.pow(8, 2)
  120. // num = Math.min(64, 55, 33)
  121. // num = Math.max(64, 55, 33)
  122. // num = Math.random() * 100
  123. // num = parseInt(num)
  124.  
  125. // console.log(num)
  126.  
  127. // String methods & Concatenation ******************************************************
  128.  
  129. // give an example of string concatenation
  130.  
  131. // let name = 'Tom'
  132. // let secondName = 'Smith'
  133.  
  134. // console.log(name + ' ' + secondName)
  135.  
  136. // append 2 string variables using the += (addition assignment operator) which means x = x + y
  137.  
  138. // name += secondName
  139. // console.log(name)
  140.  
  141. // give an example of escaping characters
  142.  
  143. // let phrase = 'Tom replied: Let\'s study at my house'
  144. // console.log(phrase)
  145.  
  146. // use the following methods and properties on strings
  147.  
  148. // length
  149.  
  150. // let email = 'john@yahoo.com'
  151. // console.log(email + ' is ' + email.length + ' letters long.')
  152.  
  153. // concat()
  154.  
  155. // let work = ' driver'
  156. // console.log(email.concat(work))
  157.  
  158. // toUpperCase()
  159.  
  160. // console.log(email.toUpperCase())
  161.  
  162. // toLowerCase()
  163.  
  164. // let caps = 'HELLO'
  165. // let smalls = caps.toLowerCase()
  166. // console.log(smalls)
  167.  
  168. // get character using []
  169.  
  170. // console.log(smalls[2])
  171.  
  172. // indexOf()
  173.  
  174. // let big = 'supercalafragilistic'
  175. // console.log(big.indexOf('c'))
  176.  
  177. // // lastIndexof() (this comes from the end of the string)
  178.  
  179. // console.log(big.lastIndexOf('c'))
  180.  
  181. // // charAt()
  182.  
  183. // console.log(big.charAt(9))
  184.  
  185. // // Get last character of a string by using charAt(yourstring.length -1)
  186.  
  187. // console.log(big.charAt(big.length - 1))
  188.  
  189. // // substring(0, 4)
  190.  
  191. // let sub = big.substring(0, 4)
  192. // console.log(sub)
  193.  
  194. // // slice(0, 4) slice works with strings and arrays... with slice you can use negative numbers and it will begin at the back of the string or array
  195.  
  196. // let slice = big.slice(0, -4)
  197. // console.log(slice)
  198.  
  199. // split() split will turn the string into an array. you can split on any character including spaces.
  200.  
  201. // let phrase = 'this is my string'
  202. // let newPhrase = phrase.split(' ')
  203. // console.log(newPhrase)
  204.  
  205. // // replace()
  206.  
  207. // console.log(phrase.replace('my', 'your'))
  208.  
  209. // // includes() check if the string or char exists inside of a string. returns true or false
  210.  
  211. // console.log(phrase.includes('string'))
  212.  
  213. // // Template Literals *****************************************************************
  214.  
  215. // // take the following data and input it as an unordered list in html (do this using string concatenation and template literals)
  216.  
  217. // const obj = {
  218. // name: 'John',
  219. // age: 30,
  220. // job: 'Web Developer',
  221. // city: 'Miami'
  222. // }
  223.  
  224. // let concat = '<ul> <li>name: ' + obj.name + '</li> <li>age: ' + obj.age + '</li> <li>job ' + obj.job + '</li> <li>city: ' + obj.city + '</li> </ul>'
  225.  
  226. // console.log(concat)
  227.  
  228. // let data = `<ul>
  229. // <li>name: ${obj.name}</li>
  230. // <li>age: ${obj.age}</li>
  231. // <li>job: ${obj.job}</li>
  232. // <li>city: ${obj.city}</li>
  233. // </ul>`
  234.  
  235. // console.log(data)
  236.  
  237. // // output an expression using template literals
  238.  
  239. // let add = `5 + 5 = ${5 + 5}`
  240. // console.log(add)
  241.  
  242. // // output a function using template literals
  243.  
  244. // const hello = function () {
  245. // return 'hello'
  246. // }
  247. // const literal = `I'm literally saying ${hello()}`
  248.  
  249. // console.log(literal)
  250.  
  251. // // output conditional or 'if statement' using template literals. You must use ternary operator syntax or wrap the 'if statement in a called anonymous function
  252.  
  253. // let age = 40
  254. // let sentence = `Bob is ${age < 39 ? 'young' : 'old'}`
  255. // console.log(sentence)
  256.  
  257. // // ex. ternary-operator: `${age > 30 ? 'Over 30' : 'Under 30'}
  258.  
  259. // // ex. invoked anon function if-statement:
  260. // // const age = 30
  261. // // let html = `<div>
  262. // // ${
  263. // // (age => {
  264. // // if(age > 30) {
  265. // // return 'Over 30'
  266. // // } else {
  267. // // return 'Under 30'
  268. // // }
  269. // // })()
  270. // // }
  271. // // </div>`
  272. // // document.body.innerHTML = html
  273.  
  274. // let sentence2 = `Bob is ${
  275. // (age => {
  276. // if (age < 39) {
  277. // return 'young'
  278. // } else {
  279. // return 'old'
  280. // }
  281. // })()
  282. // }`
  283.  
  284. // console.log(sentence2)
  285.  
  286. // Arrays and Array Methods *************************************************************
  287.  
  288. /* ~~~~~~~~~~~~ examples
  289. ex. const numbers = [43,56,13,81,61]
  290. ex. const numbers2 = new Array(22,45,33,17,12)
  291. ex. numbers.length
  292. ex. Array.isArray(numbers)
  293. ex. numbers[3]
  294. ex. numbers[2] = 100
  295. ex numbers.indexOf(61)
  296. ex. numbers.push(250)
  297. ex. numbers.unshift(120)
  298. ex. numbers.pop()
  299. ex. numbers.shift()
  300. ex. numbers.splice(1,1)
  301. ex. numbers.reverse()
  302. ex. numbers.concat(numbers2)
  303. ex. fruit.sort()
  304. ex. numbers.sort()
  305. ex. numbers.sort(function (x, y){
  306. return x - y
  307. })
  308. ex. numbers.sort(function (x, y){
  309. return y - x
  310. })
  311. ex. finds the 1st number under 50
  312. function under50(num) {
  313. return num < 50
  314. }
  315. numbers.find(under50)
  316. ex. finds the 1st number over 50
  317. function overr50(num) {
  318. return num > 50
  319. }
  320. numbers.find(over50)
  321. ~~~~~~~~~~~~~~~~~~~~~~~~~ */
  322.  
  323. // // create an array using an array literal:
  324.  
  325. // const list = ['eggs', 'bacon', 'ham', 'bread']
  326. // console.log(list)
  327.  
  328. // // create an array using an array constructor:
  329.  
  330. // const groceries = new Array('eggs', 'bacon', 'tomatoes', 'gravy')
  331. // console.log(groceries)
  332.  
  333. // // Create some arrays with numbers, strings, mixed data types:
  334.  
  335. // const nums = [3, 5, 17, 33]
  336. // const strings = ['rock', 'opera', 'bluegrass', 'country', 'hip-hop']
  337. // const mixed = [7, 'eleven', {car: 'chevy'}, ['stop', 'drop', 'roll'], 79, true, null, 'end']
  338.  
  339. // console.log(nums)
  340. // console.log(strings)
  341. // console.log(mixed)
  342.  
  343. // // Get array length:
  344.  
  345. // console.log(mixed.length)
  346.  
  347. // // Check if is array:
  348.  
  349. // console.log(Array.isArray(mixed[3]))
  350.  
  351. // // Get single value:
  352.  
  353. // console.log(nums[2])
  354.  
  355. // // Insert into array:
  356.  
  357. // nums[2] = 19
  358. // console.log(nums)
  359.  
  360. // // Find index of value:
  361.  
  362. // let singleNum = nums.indexOf(33)
  363. // console.log('index: ', singleNum)
  364.  
  365. // // ------MUTATING ARRAYS----------
  366.  
  367. // // Add on to end:
  368.  
  369. // nums.push(99)
  370. // console.log(nums)
  371.  
  372. // // Add on the front:
  373.  
  374. // nums.unshift(45)
  375. // console.log(nums)
  376.  
  377. // // Take off from end:
  378.  
  379. // nums.pop()
  380. // console.log(nums)
  381.  
  382. // // Take off from front:
  383.  
  384. // nums.shift()
  385. // console.log(nums)
  386.  
  387. // // Splice values:
  388.  
  389. // let miniNums = nums.splice(1, 1, 6, 7, 8)
  390. // console.log(miniNums + '\n' + nums)
  391.  
  392. // // Reverse:
  393.  
  394. // nums.reverse()
  395. // console.log(nums)
  396.  
  397. // // Concatenate array:
  398.  
  399. // let newNums = nums.concat(4, 11)
  400. // console.log(newNums)
  401.  
  402. // // ------Sorting arrays------
  403.  
  404. // // string array, sorts by alpha:
  405.  
  406. // strings.sort()
  407. // console.log(strings)
  408. // strings.reverse()
  409. // console.log(strings)
  410.  
  411. // // num array, sorts by first number:
  412.  
  413. // nums.sort()
  414. // console.log(nums)
  415.  
  416. // // use the "compare function" for least to greatest:
  417.  
  418. // nums.sort((x, y) => {
  419. // return x - y
  420. // })
  421. // console.log(nums)
  422.  
  423. // // use the "compare function" for greatest to least:
  424.  
  425. // nums.sort((x, y) => {
  426. // return y - x
  427. // })
  428. // console.log(nums)
  429.  
  430. // // Find: find the 1st number under 50
  431.  
  432. // function under50 (num) {
  433. // return num < 50
  434. // }
  435. // console.log(nums.find(under50))
  436.  
  437. // // Find: find the 1st number over 50
  438.  
  439. // nums.push(79)
  440.  
  441. // function over50 (num) {
  442. // return num > 50
  443. // }
  444. // console.log(nums.find(over50))
  445.  
  446. // Object Literals *****************************************************************
  447.  
  448. // // Create an object literal containing the following property-types: string, number, array, abject, function
  449.  
  450. // let person = {
  451. // firstName: 'Rob',
  452. // LastName: 'Love',
  453. // age: '37',
  454. // interests: ['sports', 'motorcycles', 'carpentry'],
  455. // address: {
  456. // city: 'Chicago',
  457. // state: 'IL'
  458. // },
  459. // sayName: function () {
  460. // return `Hi, my name is ${this.firstName}`
  461. // }
  462. // }
  463. // console.log(person)
  464.  
  465. // // Get a specific value from each property using dot-notation. give an example of bracket-notation:
  466.  
  467. // console.log(person.LastName)
  468. // console.log(person.age)
  469. // console.log(person['firstName'])
  470. // console.log(person.interests)
  471. // console.log(person.interests[1])
  472. // console.log(person.address)
  473. // console.log(person.address.city)
  474. // console.log(person.sayName())
  475. // console.log(person['interests'][[2]])
  476.  
  477. // // Create a method on the object that uses the this keyword:
  478.  
  479. // person.sayAge = function () {
  480. // console.log(`I am ${this.age} years old`)
  481. // }
  482. // person.sayAge()
  483.  
  484. // // Create an array of object literals:
  485.  
  486. // let team = [{name: 'Jerry', position: 'Manager'}, {name: 'Rena', position: 'clerk'}, {name: 'Ron', position: 'clerk'}]
  487.  
  488. // console.log(team)
  489.  
  490. // Dates and Times ******************************************************************
  491.  
  492. // If Statements and Comparison operators *******************************************
  493.  
  494. // // Create example if, if/else, and else if statements with the following...
  495. // // Equal To: ==
  496.  
  497. // let one = 1
  498. // if (one == true) {
  499. // console.log('this is true')
  500. // }
  501.  
  502. // // Not Equal To: !=
  503.  
  504. // if (one != true) {
  505. // console.log('this is true')
  506. // } else {
  507. // console.log('this is not true')
  508. // }
  509.  
  510. // // Equal to value and Type: ===
  511.  
  512. // let one2 = '1'
  513. // if (one === one2) {
  514. // console.log(`${one} is equal to ${one2} in both value and type`)
  515. // } else {
  516. // console.log(`one, value:${one} type:${typeof one}`)
  517. // console.log(`one2, value:${one2} type:${typeof one2}`)
  518. // }
  519.  
  520. // // Not Equal to value and Type: !==
  521.  
  522. // let word = 'big'
  523. // let word2 = 'big'
  524.  
  525. // if (typeof word !== typeof word2) {
  526. // console.log('the variables are not equal in type')
  527. // } else {
  528. // console.log('the variables are equal in both value and type')
  529. // }
  530. // // Check to see if variable undefined: typeof varName !== 'undefined'
  531.  
  532. // const person = undefined
  533.  
  534. // if (person !== undefined) {
  535. // console.log('the variable is not equal to undefined')
  536. // } else {
  537. // console.log('they are equal')
  538. // }
  539.  
  540. // // Greater or Less than:
  541.  
  542. // if (57 < 33) {
  543. // console.log('57 is less than 33')
  544. // } else {
  545. // console.log('57 is greater than 33')
  546. // }
  547.  
  548. // // Else if:
  549.  
  550. // let myFavCar = 'gmc'
  551.  
  552. // let car1 = 'ford'
  553. // let car2 = 'honda'
  554. // let car3 = 'gmc'
  555.  
  556. // if (car1 === myFavCar) {
  557. // console.log(`${car1} is the car for me`)
  558. // } else if (car2 === myFavCar) {
  559. // console.log(`${car2} is the car for me`)
  560. // } else if (car3 === myFavCar) {
  561. // console.log(`${car3} is the car for me`)
  562. // }
  563.  
  564. // // Logical Operators:
  565. // // AND &&
  566.  
  567. // if (car3 === myFavCar && car1 !== myFavCar) {
  568. // console.log('that\'s right')
  569. // }
  570.  
  571. // // OR ||
  572.  
  573. // if (car1 === car2 || car2 === car3) {
  574. // console.log('one of these statements is true')
  575. // } else {
  576. // console.log('both of these statements are false')
  577. // }
  578.  
  579. // // Shorthand:
  580. // // TERNARY OPERATOR: ? (if), : (else)
  581.  
  582. // let scoreOne = 99
  583. // let scoreTwo = 79
  584.  
  585. // scoreOne > scoreTwo ? console.log('scoreOne!') : console.log('scoreTwo!')
  586.  
  587. // // if/else Without Braces:
  588.  
  589. // if (scoreOne === scoreTwo)
  590. // console.log('Tie')
  591. // else
  592. // console.log('We have a winner')
  593.  
  594. // // Switch Statements ******************************************************************
  595.  
  596. // // Create a switch statement with several cases:
  597.  
  598. // let day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
  599.  
  600. // switch (day[2]) {
  601. // case 'Monday':
  602. // console.log('work @ 7am')
  603. // break
  604. // case 'Tuesday':
  605. // console.log('work @ 8am')
  606. // break
  607. // case 'Wednesday':
  608. // console.log('work @ 9am')
  609. // break
  610. // }
  611.  
  612. // // Functions, function Declarations and function Expressions **************************
  613.  
  614. // // Function Declarations example:
  615.  
  616. // function Callme () {
  617. // return 'Hello there, what can I do for you?'
  618. // }
  619.  
  620. // console.log(Callme())
  621.  
  622. // // function with parameters & multiple parameters:
  623.  
  624. // function double (val) {
  625. // if (typeof val === typeof 'string') {
  626. // return val.append(' ' + val)
  627. // } else if (typeof val === typeof 5) {
  628. // return val * 2
  629. // } else {
  630. // return 'I can\'t handle this type'
  631. // }
  632. // }
  633.  
  634. // console.log(double(8))
  635.  
  636. // // default values for parameters example:
  637.  
  638. // const fullName = (first = 'John', last = 'Doe') => {
  639. // return `Congratulations, your full name is ${first} ${last}!`
  640. // }
  641. // console.log(fullName('Ben', 'Faught'))
  642.  
  643. // // function expression example:
  644.  
  645. // const hello = function () {
  646. // console.log('hello world!')
  647. // }
  648. // hello()
  649.  
  650. // // named function expression:
  651.  
  652. // const multiply = function multiply (num, multiplier) {
  653. // return num * multiplier
  654. // }
  655. // console.log(multiply(7, 3))
  656.  
  657. // // Immediately Invoked Function Expressions - IIFEs:
  658.  
  659. // (function () {
  660. // console.log('I am an IIFE :)')
  661. // })();
  662.  
  663. // // IIFE with params:
  664.  
  665. // (function (name) {
  666. // console.log('I am an IIFE with params, my name is ' + name)
  667. // })('Biffie');
  668.  
  669. // // TRUE or False, in modules, IIFEs can provide private methods and properties, by keeping everything scoped to that module? true
  670.  
  671. // // Property Methods... Create a method within an object:
  672.  
  673. // let myObj = {
  674. // name: 'Larry',
  675. // age: 30,
  676. // iam: function () {
  677. // console.log(`my name is ${this.name} and I am ${this.age} years old.`)
  678. // }
  679. // }
  680.  
  681. // myObj.iam()
  682.  
  683. // // Create a method on an object from outside the object:
  684.  
  685. // myObj.hello = function () {
  686. // console.log(`this is ${this.name}, just saying hello!`)
  687. // }
  688.  
  689. // myObj.hello()
  690.  
  691. // General Loops ************************************************************************
  692.  
  693. // // create a general for loop:
  694.  
  695. // for (let i = 0; i < 5; i++) {
  696. // console.log('the count is: ' + i)
  697. // }
  698.  
  699. // // for loop with a condition inside that uses the continue keyword:
  700.  
  701. // for (let i = 0; i < 15; i++) {
  702. // if (i >= 5 && i < 10) {
  703. // console.log('continue: ' + i)
  704. // continue
  705. // }
  706. // console.log('the count is: ' + i)
  707. // }
  708.  
  709. // // for loop with a condition inside that uses the break keyword:
  710.  
  711. // for (let i = 0; i < 10; i++) {
  712. // if (i === 6) {
  713. // console.log('I\'m leaving this loop at 6')
  714. // break
  715. // }
  716. // console.log('looping @: ' + i)
  717. // }
  718.  
  719. // // create a while loop:
  720.  
  721. // let m = 0
  722.  
  723. // while (m <= 3) {
  724. // console.log('value is: ' + m)
  725. // m++
  726. // }
  727.  
  728. // // true or false, a do-while loop will run it's first iteration regardless of the conditional. true
  729.  
  730. // // create a do while loop:
  731.  
  732. // do {
  733. // console.log('hello')
  734. // m++
  735. // } while (m < 1)
  736.  
  737. // // use a for loop to iterate through an array: hint: array.length
  738.  
  739. // let list = ['eggs', 'milk', 'bread', 'bologna']
  740.  
  741. // for (let i = 0; i < list.length; i++) {
  742. // console.log(list[i])
  743. // }
  744.  
  745. // // use forEach() to loop through an array:
  746.  
  747. // const users = [
  748. // {id: '7', first: 'Tom', last: 'Brady'},
  749. // {id: '8', first: 'Mary', last: 'Smith'},
  750. // {id: '9', first: 'Jake', last: 'Paul'}
  751. // ]
  752.  
  753. // users.forEach(function (user) {
  754. // console.log(user.last)
  755. // console.log(user)
  756. // })
  757.  
  758. // // use forEach() to loop through an array, using the index & array keyword as a parameter in the callback:
  759.  
  760. // users.forEach(function (user, index, array) {
  761. // console.log(user.first)
  762. // console.log(index)
  763. // console.log(array)
  764. // })
  765.  
  766. // // use map() to loop through an existing array of objects, and create an new array from one of the properties of that object:
  767.  
  768. // const ids = users.map(function (user) {
  769. // return user.id
  770. // })
  771. // console.log(ids)
  772.  
  773. // // create a for-in loop on an object literal (print out both the key and value):
  774.  
  775. // const person = {
  776. // name: 'Bob',
  777. // age: 'White',
  778. // dob: '11-3-1977',
  779. // city: 'New York'
  780. // }
  781.  
  782. // for (let x in person) {
  783. // console.log(x)
  784. // console.log(person[x])
  785. // console.log(`${x}: ${person[x]}`)
  786. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement