Created
June 6, 2019 13:24
-
-
Save benfaught/21e048792f8efadbca40365605fa3e62 to your computer and use it in GitHub Desktop.
Self created JS quiz with answers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this worksheet was generated from the 'Javascript Language Fundamentals' section of Brad Traversy's Modern Javascript Udemy course | |
// Variables ********************************************************************** | |
// name 3 keywords for initializing and declaring a variable? var, let, const | |
// which 2 JS variable keywords are block scoped? let and const | |
// declare a variable but don't initialize a value. | |
// let name | |
// console.log(name) | |
// assign the initialized variable a value. | |
// name = 'John Doe' | |
// console.log(name) | |
// which type of JS variable must be both declared and initialized at the same time? constant | |
// JS variables can contain what? letters,numbers,underscores,and $ | |
// JS variables connot start with what? numbers | |
// 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 | |
// when using const in JS, are mutatable data structures such as arrays and abjects, still mutatable? yes | |
// Data-Types ************************************************************************ | |
// Name the six primitive data types in JS: | |
// string | |
// number | |
// boolean | |
// undefined | |
// null | |
// symbol | |
// (true or false) Null is an intentional empty value? true | |
// (true or false) Undefined is variable that has not been assigned a value? true | |
// (true or false) arrays, Object Literals, Functions, and Dates are considered Reference Data Types? true | |
// what can be used to find out a variable's type? typeof() | |
// Type conversion ******************************************************************* | |
// Use the String function to convert a number, boolean, date, and an array to a string | |
// let item = 5 | |
// item = true | |
// item = ['mon', 'tues', 'wed'] | |
// item = new Date() | |
// item = String(item) | |
// console.log(item) | |
// console.log(typeof item) | |
// Use the toString method to convert a number to a string | |
// let item = 8 | |
// item = item.toString() | |
// console.log(item) | |
// console.log(typeof item) | |
// Use the Number function to convert a string-number, both booleans, and a word string to a number | |
// let it = '9' | |
// it = true | |
// it = false | |
// it = 'word' | |
// it = Number(it) | |
// console.log(it) | |
// console.log(typeof it) | |
// (true or false) NaN is a value that means not a number. true | |
// (true or false) NaN is what is output when a value can't be converted to a number. true | |
// besides the Number() function, show 2 more ways to convert to a number, using the functions parseInt() and parseFloat() | |
// it = '33' | |
// it = '39.3376' | |
// it = parseInt(it) | |
// it = parseFloat(it) | |
// console.log(it, typeof it) | |
// console.log(it.toFixed(2), typeof it) | |
// (true or false) the toFixed method can be used to add decimal places to a number. true | |
// Type coersion ******************************************************************* | |
// (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 | |
// The Math Object ***************************************************************** | |
// use the following methods on the Math object to generate results. | |
// Math.round() | |
// Math.ceil() | |
// Math.floor() | |
// Math.sqrt() | |
// Math.abs() | |
// Math.pow() | |
// Math.min() | |
// Math.max() | |
// Math.random() | |
// let num | |
// num = Math.round(12.7) | |
// num = Math.ceil(12.1) | |
// num = Math.floor(12.9) | |
// num = Math.sqrt(64) | |
// num = Math.abs(-59) | |
// num = Math.pow(8, 2) | |
// num = Math.min(64, 55, 33) | |
// num = Math.max(64, 55, 33) | |
// num = Math.random() * 100 | |
// num = parseInt(num) | |
// console.log(num) | |
// String methods & Concatenation ****************************************************** | |
// give an example of string concatenation | |
// let name = 'Tom' | |
// let secondName = 'Smith' | |
// console.log(name + ' ' + secondName) | |
// append 2 string variables using the += (addition assignment operator) which means x = x + y | |
// name += secondName | |
// console.log(name) | |
// give an example of escaping characters | |
// let phrase = 'Tom replied: Let\'s study at my house' | |
// console.log(phrase) | |
// use the following methods and properties on strings | |
// length | |
// let email = 'john@yahoo.com' | |
// console.log(email + ' is ' + email.length + ' letters long.') | |
// concat() | |
// let work = ' driver' | |
// console.log(email.concat(work)) | |
// toUpperCase() | |
// console.log(email.toUpperCase()) | |
// toLowerCase() | |
// let caps = 'HELLO' | |
// let smalls = caps.toLowerCase() | |
// console.log(smalls) | |
// get character using [] | |
// console.log(smalls[2]) | |
// indexOf() | |
// let big = 'supercalafragilistic' | |
// console.log(big.indexOf('c')) | |
// // lastIndexof() (this comes from the end of the string) | |
// console.log(big.lastIndexOf('c')) | |
// // charAt() | |
// console.log(big.charAt(9)) | |
// // Get last character of a string by using charAt(yourstring.length -1) | |
// console.log(big.charAt(big.length - 1)) | |
// // substring(0, 4) | |
// let sub = big.substring(0, 4) | |
// console.log(sub) | |
// // 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 | |
// let slice = big.slice(0, -4) | |
// console.log(slice) | |
// split() split will turn the string into an array. you can split on any character including spaces. | |
// let phrase = 'this is my string' | |
// let newPhrase = phrase.split(' ') | |
// console.log(newPhrase) | |
// // replace() | |
// console.log(phrase.replace('my', 'your')) | |
// // includes() check if the string or char exists inside of a string. returns true or false | |
// console.log(phrase.includes('string')) | |
// // Template Literals ***************************************************************** | |
// // take the following data and input it as an unordered list in html (do this using string concatenation and template literals) | |
// const obj = { | |
// name: 'John', | |
// age: 30, | |
// job: 'Web Developer', | |
// city: 'Miami' | |
// } | |
// let concat = '<ul> <li>name: ' + obj.name + '</li> <li>age: ' + obj.age + '</li> <li>job ' + obj.job + '</li> <li>city: ' + obj.city + '</li> </ul>' | |
// console.log(concat) | |
// let data = `<ul> | |
// <li>name: ${obj.name}</li> | |
// <li>age: ${obj.age}</li> | |
// <li>job: ${obj.job}</li> | |
// <li>city: ${obj.city}</li> | |
// </ul>` | |
// console.log(data) | |
// // output an expression using template literals | |
// let add = `5 + 5 = ${5 + 5}` | |
// console.log(add) | |
// // output a function using template literals | |
// const hello = function () { | |
// return 'hello' | |
// } | |
// const literal = `I'm literally saying ${hello()}` | |
// console.log(literal) | |
// // output conditional or 'if statement' using template literals. You must use ternary operator syntax or wrap the 'if statement in a called anonymous function | |
// let age = 40 | |
// let sentence = `Bob is ${age < 39 ? 'young' : 'old'}` | |
// console.log(sentence) | |
// // ex. ternary-operator: `${age > 30 ? 'Over 30' : 'Under 30'} | |
// // ex. invoked anon function if-statement: | |
// // const age = 30 | |
// // let html = `<div> | |
// // ${ | |
// // (age => { | |
// // if(age > 30) { | |
// // return 'Over 30' | |
// // } else { | |
// // return 'Under 30' | |
// // } | |
// // })() | |
// // } | |
// // </div>` | |
// // document.body.innerHTML = html | |
// let sentence2 = `Bob is ${ | |
// (age => { | |
// if (age < 39) { | |
// return 'young' | |
// } else { | |
// return 'old' | |
// } | |
// })() | |
// }` | |
// console.log(sentence2) | |
// Arrays and Array Methods ************************************************************* | |
/* ~~~~~~~~~~~~ examples | |
ex. const numbers = [43,56,13,81,61] | |
ex. const numbers2 = new Array(22,45,33,17,12) | |
ex. numbers.length | |
ex. Array.isArray(numbers) | |
ex. numbers[3] | |
ex. numbers[2] = 100 | |
ex numbers.indexOf(61) | |
ex. numbers.push(250) | |
ex. numbers.unshift(120) | |
ex. numbers.pop() | |
ex. numbers.shift() | |
ex. numbers.splice(1,1) | |
ex. numbers.reverse() | |
ex. numbers.concat(numbers2) | |
ex. fruit.sort() | |
ex. numbers.sort() | |
ex. numbers.sort(function (x, y){ | |
return x - y | |
}) | |
ex. numbers.sort(function (x, y){ | |
return y - x | |
}) | |
ex. finds the 1st number under 50 | |
function under50(num) { | |
return num < 50 | |
} | |
numbers.find(under50) | |
ex. finds the 1st number over 50 | |
function overr50(num) { | |
return num > 50 | |
} | |
numbers.find(over50) | |
~~~~~~~~~~~~~~~~~~~~~~~~~ */ | |
// // create an array using an array literal: | |
// const list = ['eggs', 'bacon', 'ham', 'bread'] | |
// console.log(list) | |
// // create an array using an array constructor: | |
// const groceries = new Array('eggs', 'bacon', 'tomatoes', 'gravy') | |
// console.log(groceries) | |
// // Create some arrays with numbers, strings, mixed data types: | |
// const nums = [3, 5, 17, 33] | |
// const strings = ['rock', 'opera', 'bluegrass', 'country', 'hip-hop'] | |
// const mixed = [7, 'eleven', {car: 'chevy'}, ['stop', 'drop', 'roll'], 79, true, null, 'end'] | |
// console.log(nums) | |
// console.log(strings) | |
// console.log(mixed) | |
// // Get array length: | |
// console.log(mixed.length) | |
// // Check if is array: | |
// console.log(Array.isArray(mixed[3])) | |
// // Get single value: | |
// console.log(nums[2]) | |
// // Insert into array: | |
// nums[2] = 19 | |
// console.log(nums) | |
// // Find index of value: | |
// let singleNum = nums.indexOf(33) | |
// console.log('index: ', singleNum) | |
// // ------MUTATING ARRAYS---------- | |
// // Add on to end: | |
// nums.push(99) | |
// console.log(nums) | |
// // Add on the front: | |
// nums.unshift(45) | |
// console.log(nums) | |
// // Take off from end: | |
// nums.pop() | |
// console.log(nums) | |
// // Take off from front: | |
// nums.shift() | |
// console.log(nums) | |
// // Splice values: | |
// let miniNums = nums.splice(1, 1, 6, 7, 8) | |
// console.log(miniNums + '\n' + nums) | |
// // Reverse: | |
// nums.reverse() | |
// console.log(nums) | |
// // Concatenate array: | |
// let newNums = nums.concat(4, 11) | |
// console.log(newNums) | |
// // ------Sorting arrays------ | |
// // string array, sorts by alpha: | |
// strings.sort() | |
// console.log(strings) | |
// strings.reverse() | |
// console.log(strings) | |
// // num array, sorts by first number: | |
// nums.sort() | |
// console.log(nums) | |
// // use the "compare function" for least to greatest: | |
// nums.sort((x, y) => { | |
// return x - y | |
// }) | |
// console.log(nums) | |
// // use the "compare function" for greatest to least: | |
// nums.sort((x, y) => { | |
// return y - x | |
// }) | |
// console.log(nums) | |
// // Find: find the 1st number under 50 | |
// function under50 (num) { | |
// return num < 50 | |
// } | |
// console.log(nums.find(under50)) | |
// // Find: find the 1st number over 50 | |
// nums.push(79) | |
// function over50 (num) { | |
// return num > 50 | |
// } | |
// console.log(nums.find(over50)) | |
// Object Literals ***************************************************************** | |
// // Create an object literal containing the following property-types: string, number, array, abject, function | |
// let person = { | |
// firstName: 'Rob', | |
// LastName: 'Love', | |
// age: '37', | |
// interests: ['sports', 'motorcycles', 'carpentry'], | |
// address: { | |
// city: 'Chicago', | |
// state: 'IL' | |
// }, | |
// sayName: function () { | |
// return `Hi, my name is ${this.firstName}` | |
// } | |
// } | |
// console.log(person) | |
// // Get a specific value from each property using dot-notation. give an example of bracket-notation: | |
// console.log(person.LastName) | |
// console.log(person.age) | |
// console.log(person['firstName']) | |
// console.log(person.interests) | |
// console.log(person.interests[1]) | |
// console.log(person.address) | |
// console.log(person.address.city) | |
// console.log(person.sayName()) | |
// console.log(person['interests'][[2]]) | |
// // Create a method on the object that uses the this keyword: | |
// person.sayAge = function () { | |
// console.log(`I am ${this.age} years old`) | |
// } | |
// person.sayAge() | |
// // Create an array of object literals: | |
// let team = [{name: 'Jerry', position: 'Manager'}, {name: 'Rena', position: 'clerk'}, {name: 'Ron', position: 'clerk'}] | |
// console.log(team) | |
// Dates and Times ****************************************************************** | |
// If Statements and Comparison operators ******************************************* | |
// // Create example if, if/else, and else if statements with the following... | |
// // Equal To: == | |
// let one = 1 | |
// if (one == true) { | |
// console.log('this is true') | |
// } | |
// // Not Equal To: != | |
// if (one != true) { | |
// console.log('this is true') | |
// } else { | |
// console.log('this is not true') | |
// } | |
// // Equal to value and Type: === | |
// let one2 = '1' | |
// if (one === one2) { | |
// console.log(`${one} is equal to ${one2} in both value and type`) | |
// } else { | |
// console.log(`one, value:${one} type:${typeof one}`) | |
// console.log(`one2, value:${one2} type:${typeof one2}`) | |
// } | |
// // Not Equal to value and Type: !== | |
// let word = 'big' | |
// let word2 = 'big' | |
// if (typeof word !== typeof word2) { | |
// console.log('the variables are not equal in type') | |
// } else { | |
// console.log('the variables are equal in both value and type') | |
// } | |
// // Check to see if variable undefined: typeof varName !== 'undefined' | |
// const person = undefined | |
// if (person !== undefined) { | |
// console.log('the variable is not equal to undefined') | |
// } else { | |
// console.log('they are equal') | |
// } | |
// // Greater or Less than: | |
// if (57 < 33) { | |
// console.log('57 is less than 33') | |
// } else { | |
// console.log('57 is greater than 33') | |
// } | |
// // Else if: | |
// let myFavCar = 'gmc' | |
// let car1 = 'ford' | |
// let car2 = 'honda' | |
// let car3 = 'gmc' | |
// if (car1 === myFavCar) { | |
// console.log(`${car1} is the car for me`) | |
// } else if (car2 === myFavCar) { | |
// console.log(`${car2} is the car for me`) | |
// } else if (car3 === myFavCar) { | |
// console.log(`${car3} is the car for me`) | |
// } | |
// // Logical Operators: | |
// // AND && | |
// if (car3 === myFavCar && car1 !== myFavCar) { | |
// console.log('that\'s right') | |
// } | |
// // OR || | |
// if (car1 === car2 || car2 === car3) { | |
// console.log('one of these statements is true') | |
// } else { | |
// console.log('both of these statements are false') | |
// } | |
// // Shorthand: | |
// // TERNARY OPERATOR: ? (if), : (else) | |
// let scoreOne = 99 | |
// let scoreTwo = 79 | |
// scoreOne > scoreTwo ? console.log('scoreOne!') : console.log('scoreTwo!') | |
// // if/else Without Braces: | |
// if (scoreOne === scoreTwo) | |
// console.log('Tie') | |
// else | |
// console.log('We have a winner') | |
// // Switch Statements ****************************************************************** | |
// // Create a switch statement with several cases: | |
// let day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'] | |
// switch (day[2]) { | |
// case 'Monday': | |
// console.log('work @ 7am') | |
// break | |
// case 'Tuesday': | |
// console.log('work @ 8am') | |
// break | |
// case 'Wednesday': | |
// console.log('work @ 9am') | |
// break | |
// } | |
// // Functions, function Declarations and function Expressions ************************** | |
// // Function Declarations example: | |
// function Callme () { | |
// return 'Hello there, what can I do for you?' | |
// } | |
// console.log(Callme()) | |
// // function with parameters & multiple parameters: | |
// function double (val) { | |
// if (typeof val === typeof 'string') { | |
// return val.append(' ' + val) | |
// } else if (typeof val === typeof 5) { | |
// return val * 2 | |
// } else { | |
// return 'I can\'t handle this type' | |
// } | |
// } | |
// console.log(double(8)) | |
// // default values for parameters example: | |
// const fullName = (first = 'John', last = 'Doe') => { | |
// return `Congratulations, your full name is ${first} ${last}!` | |
// } | |
// console.log(fullName('Ben', 'Faught')) | |
// // function expression example: | |
// const hello = function () { | |
// console.log('hello world!') | |
// } | |
// hello() | |
// // named function expression: | |
// const multiply = function multiply (num, multiplier) { | |
// return num * multiplier | |
// } | |
// console.log(multiply(7, 3)) | |
// // Immediately Invoked Function Expressions - IIFEs: | |
// (function () { | |
// console.log('I am an IIFE :)') | |
// })(); | |
// // IIFE with params: | |
// (function (name) { | |
// console.log('I am an IIFE with params, my name is ' + name) | |
// })('Biffie'); | |
// // TRUE or False, in modules, IIFEs can provide private methods and properties, by keeping everything scoped to that module? true | |
// // Property Methods... Create a method within an object: | |
// let myObj = { | |
// name: 'Larry', | |
// age: 30, | |
// iam: function () { | |
// console.log(`my name is ${this.name} and I am ${this.age} years old.`) | |
// } | |
// } | |
// myObj.iam() | |
// // Create a method on an object from outside the object: | |
// myObj.hello = function () { | |
// console.log(`this is ${this.name}, just saying hello!`) | |
// } | |
// myObj.hello() | |
// General Loops ************************************************************************ | |
// // create a general for loop: | |
// for (let i = 0; i < 5; i++) { | |
// console.log('the count is: ' + i) | |
// } | |
// // for loop with a condition inside that uses the continue keyword: | |
// for (let i = 0; i < 15; i++) { | |
// if (i >= 5 && i < 10) { | |
// console.log('continue: ' + i) | |
// continue | |
// } | |
// console.log('the count is: ' + i) | |
// } | |
// // for loop with a condition inside that uses the break keyword: | |
// for (let i = 0; i < 10; i++) { | |
// if (i === 6) { | |
// console.log('I\'m leaving this loop at 6') | |
// break | |
// } | |
// console.log('looping @: ' + i) | |
// } | |
// // create a while loop: | |
// let m = 0 | |
// while (m <= 3) { | |
// console.log('value is: ' + m) | |
// m++ | |
// } | |
// // true or false, a do-while loop will run it's first iteration regardless of the conditional. true | |
// // create a do while loop: | |
// do { | |
// console.log('hello') | |
// m++ | |
// } while (m < 1) | |
// // use a for loop to iterate through an array: hint: array.length | |
// let list = ['eggs', 'milk', 'bread', 'bologna'] | |
// for (let i = 0; i < list.length; i++) { | |
// console.log(list[i]) | |
// } | |
// // use forEach() to loop through an array: | |
// const users = [ | |
// {id: '7', first: 'Tom', last: 'Brady'}, | |
// {id: '8', first: 'Mary', last: 'Smith'}, | |
// {id: '9', first: 'Jake', last: 'Paul'} | |
// ] | |
// users.forEach(function (user) { | |
// console.log(user.last) | |
// console.log(user) | |
// }) | |
// // use forEach() to loop through an array, using the index & array keyword as a parameter in the callback: | |
// users.forEach(function (user, index, array) { | |
// console.log(user.first) | |
// console.log(index) | |
// console.log(array) | |
// }) | |
// // use map() to loop through an existing array of objects, and create an new array from one of the properties of that object: | |
// const ids = users.map(function (user) { | |
// return user.id | |
// }) | |
// console.log(ids) | |
// // create a for-in loop on an object literal (print out both the key and value): | |
// const person = { | |
// name: 'Bob', | |
// age: 'White', | |
// dob: '11-3-1977', | |
// city: 'New York' | |
// } | |
// for (let x in person) { | |
// console.log(x) | |
// console.log(person[x]) | |
// console.log(`${x}: ${person[x]}`) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment