├── arrays_exercise └── solutions.js ├── boolean_logic_and_operators_exercise └── readme.md ├── functions_exercise └── solutions.js └── objects_exercise └── solutions.js /arrays_exercise/solutions.js: -------------------------------------------------------------------------------- 1 | let people = ["Greg", "Mary", "Devon", "James"]; 2 | 3 | // 1 4 | for(let i =0; i< people.length; i++){ 5 | console.log(people[i]); 6 | } 7 | 8 | // 2 9 | 10 | people.shift(); 11 | 12 | // 3 13 | 14 | people.pop(); 15 | 16 | // 4 17 | 18 | people.unshift("Matt"); 19 | 20 | // 5 21 | 22 | people.push("Elie"); 23 | 24 | // 6 25 | 26 | for(let i =0; i< people.length; i++){ 27 | if(i > 1){ 28 | break; 29 | } 30 | console.log(people[i]); 31 | } 32 | 33 | // 7 34 | 35 | people.slice(2); 36 | 37 | // 8 38 | 39 | people.indexOf("Mary"); 40 | 41 | // 9 42 | 43 | people.indexOf("Foo"); 44 | 45 | 46 | // 10 47 | 48 | people = ["Greg", "Mary", "Devon", "James"]; 49 | 50 | people.splice(2,1,"Elizabeth", "Artie"); 51 | 52 | // 11 53 | 54 | let withBob = people.concat("Bob"); 55 | 56 | -------------------------------------------------------------------------------- /boolean_logic_and_operators_exercise/readme.md: -------------------------------------------------------------------------------- 1 | ## Part I 2 | 3 | Write down what the following statements will return. Try to figure this out before putting the commands in the chrome console. 4 | 5 | 1. `2 == "2";` `true` 6 | 2. `2 === 2;` `true` 7 | 3. `10 % 3;` `1` 8 | 4. `10 % 3 === 1;` `true` 9 | 5. `true && false;` `false` 10 | 5. `false || true;` `true` 11 | 5. `true || false;` `true` 12 | 13 | ## Part II 14 | 15 | Answer the following questions about this code block: 16 | 17 | ```js 18 | let isLearning = true; 19 | if(isLearning){ 20 | console.log("Keep it up!"); 21 | } else { 22 | console.log("Pretty sure you are learning...."); 23 | } 24 | ``` 25 | 26 | 1. What should the above code console.log? **The code should console.log "Keep it up!" because the value of the isLearning variable is truthy. That means the code inside of the if {} will be evaluated and not the else {}** 27 | 2. Why do we not need to specify `if(isLearning === true)`? Why does `if(isLearning)` work on its own? **Since true is a "truthy" value, we can let the if statement turn the expression into a value that is true or false. True will evaluate into a truthy value** 28 | 29 | ```js 30 | let firstVariable; 31 | let secondVariable = ""; 32 | let thirdVariable = 1; 33 | let secretMessage = "Shh!"; 34 | 35 | if(firstVariable){ 36 | console.log("first"); 37 | } else if(firstVariable || secondVariable){ 38 | console.log("second"); 39 | } else if(firstVariable || thirdVariable){ 40 | console.log("third"); 41 | } else { 42 | console.log("fourth"); 43 | } 44 | ``` 45 | 46 | 1. What should the above code console.log? **The code should console.log "Third" because the thirdVariable is truthy. Even though the firstVariable is falsey, the or statement is only looking for one truthy statement.** 47 | 2. What is the value of `firstVariable` when it is initialized? **The value of firstVariable is `undefined`. Variables that are not assigned to any value are assigned to the value `undefined`** 48 | 3. Is the value of firstVariable a "truthy" value? **No, `undefined` is a falsey value** 49 | 4. Is the value of secondVariable a "truthy" value? **No, empty strings are falsey values as well** 50 | 4. Is the value of thirdVariable a "truthy" value? **Yes, all numbers except for 0 are truthy** 51 | 52 | ## Part III 53 | 54 | - Research `Math.random` [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) and write an if statement that console.log's "Over 0.5" if `Math.random` returns a number greater than 0.5. Otherwise console.log "Under 0.5". 55 | 56 | ```js 57 | if(Math.random() > .5){ 58 | console.log("Over 0.5"); 59 | } else { 60 | console.log("Under 0.5"); 61 | } 62 | ``` 63 | 64 | - What is a falsey value? List all the falsey values in JavaScript. 65 | 66 | **A falsey value is a value that evaluates to false when inside of a conditional statement (if, else if, case). Inside of these conditional statements, the result will always be true or false regardless of what values are passed to the condition. The falsey values (values that will always be evaluated to false inside of a condition) in JavaScript are 0, "", false, null, undefined and NaN.** 67 | -------------------------------------------------------------------------------- /functions_exercise/solutions.js: -------------------------------------------------------------------------------- 1 | function difference(a,b){ 2 | return a-b; 3 | } 4 | 5 | function product(a,b){ 6 | return a*b; 7 | } 8 | 9 | function printDay(num){ 10 | 11 | const DAYS_OF_WEEK = { 12 | 1: "Sunday", 13 | 2: "Monday", 14 | 3: "Tuesday", 15 | 4: "Wednesday", 16 | 5: "Thursday", 17 | 6: "Friday", 18 | 7: "Saturday", 19 | }; 20 | return DAYS_OF_WEEK[num]; 21 | } 22 | 23 | function lastElement(arr){ 24 | return arr[arr.length-1]; 25 | } 26 | 27 | function numberCompare(a,b){ 28 | if(a === b){ 29 | return 'Numbers are equal'; 30 | } else if(a > b){ 31 | return 'First is greater'; 32 | } 33 | return 'Second is greater'; 34 | } 35 | 36 | function singleLetterCount(str1, letter){ 37 | let finalCount = 0; 38 | for(let i=0; i< str1.length; i++){ 39 | if(str1[i].toLowerCase() === letter.toLowerCase()){ 40 | finalCount++; 41 | } 42 | } 43 | return finalCount; 44 | } 45 | 46 | function multipleLetterCount(str){ 47 | let finalObj = {}; 48 | for(let i =0; i< str.length; i++){ 49 | if (!(str[i] in finalObj)){ 50 | finalObj[str[i]] = 1; 51 | } else { 52 | finalObj[str[i]]++; 53 | } 54 | } 55 | return finalObj; 56 | } 57 | 58 | function arrayManipulation(arr, command, position, val){ 59 | if(command === 'remove'){ 60 | if(position === 'end'){ 61 | return arr.pop(); 62 | } 63 | return arr.shift(); 64 | } 65 | else if(command === 'add'){ 66 | if(position === 'end'){ 67 | arr.push(val) 68 | return arr; 69 | } 70 | arr.unshift(val) 71 | return arr; 72 | } 73 | } 74 | 75 | function isPalindrome(str){ 76 | return str.toLowerCase().split('').reverse().join('') === str.toLowerCase(); 77 | 78 | // for(let i =0; i < str.length/2; i++){ 79 | // if(str[i].toLowerCase() !== str[str.length-1-i].toLowerCase()){ 80 | // return false; 81 | // } 82 | // } 83 | // return true; 84 | } 85 | 86 | function RPS(){ 87 | 88 | function determineComputer(num){ 89 | if(num <= .33) return "rock"; 90 | else if(num <= .66) return "paper"; 91 | return "scissor"; 92 | } 93 | 94 | let userChoice = prompt("Choose rock / paper or scissor").toLowerCase(); 95 | let computerChoice = determineComputer(Math.random()); 96 | 97 | 98 | let answers = ["rock", "paper", "scissor"]; 99 | 100 | if(!userChoice || answers.indexOf(userChoice) === -1){ 101 | return "Please select a valid option"; 102 | } 103 | 104 | if(userChoice === computerChoice) return "Tie!"; 105 | 106 | if(userChoice === "rock" && computerChoice === "paper") return "You lose, computer picked " + computerChoice; 107 | if(userChoice === "paper" && computerChoice === "scissor") return "You lose, computer picked " + computerChoice; 108 | if(userChoice === "scissor" && computerChoice === "rock") return "You lose, computer picked " + computerChoice; 109 | 110 | return "You win! Computer picked " + computerChoice; 111 | } 112 | 113 | -------------------------------------------------------------------------------- /objects_exercise/solutions.js: -------------------------------------------------------------------------------- 1 | let programming = { 2 | languages: ["JavaScript", "Python", "Ruby"], 3 | isChallenging: true, 4 | isRewarding: true, 5 | difficulty: 8, 6 | jokes: "http://stackoverflow.com/questions/234075/what-is-your-best-programmer-joke" 7 | } 8 | 9 | // 1 10 | 11 | programming.languages.push("Go"); 12 | 13 | // 2 14 | 15 | programming.difficulty = 7; 16 | 17 | // 3 18 | 19 | delete programming.jokes; 20 | 21 | // 4 22 | 23 | programming.isFun = true; 24 | 25 | // 5 26 | 27 | for (let i = 0; i < programming.languages.length; i++) { 28 | console.log(programming.languages[i]); 29 | } 30 | 31 | // or 32 | 33 | for (let language of programming.languages) { 34 | console.log(language); 35 | } 36 | 37 | // 6 38 | 39 | for (let key in programming){ 40 | console.log(key); 41 | } 42 | 43 | // 7 44 | 45 | for (let key in programming){ 46 | console.log(programming[key]); 47 | } 48 | --------------------------------------------------------------------------------