diff --git a/morning_exercise_resources/100_goblins/goblins.md b/morning_exercise_resources/100_goblins/goblins.md new file mode 100644 index 0000000..9002d37 --- /dev/null +++ b/morning_exercise_resources/100_goblins/goblins.md @@ -0,0 +1,24 @@ +# 100 GOBLINS + +There are 100 Goblins suffering from acid reflux, and you are a sadistic Goblin doctor with +the cure. You want to torture the Goblins with a little game. + +You line up all the Goblins and +give each of them the cure in turn, all 100 Goblins. + +But then you start from the beginning of the line and go over them a second time, removing the cure for every second Goblin, starting with the second Goblin. + +Then you do it a third time for every third Goblin, starting at the third Goblin: if the Goblin you now administer was previously cured, they are now uncured, and if the Goblin was uncured, they are now cured. You do it a fourth time, and go to every fourth Goblin starting with the fourth. The fifth time, you go to every fifth Goblin starting with the fifth .... + +After you have gone down the line of Goblins 100 times, which Goblins are cured at the end? +Hint: set up an array of goblins first using a datatype good for setting either *true* or *false* +values. It's certainly *false* that all the all goblins are cured at the beginning . . . . + +### REQUIREMENTS: +1. Use for loops for this exercise! You will eventually need a loop within a loop. + +2. Once you have your goblins array populated with the final *true* and *false* values after having gone through the entire process of curing and uncuring, translate your final array to another array of numerical values of the *cured Goblins only*. If the first Goblin is cured, the first number in the array will be 1 (not zero). If the 100th Goblin is cured, the last number in the array will be 100. The expected output (solution) will be a list of all the perfect squares up to and including 100. + +``` +=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] +``` \ No newline at end of file diff --git a/morning_exercise_resources/100_goblins/goblins_solutions.js b/morning_exercise_resources/100_goblins/goblins_solutions.js new file mode 100644 index 0000000..5c1acd3 --- /dev/null +++ b/morning_exercise_resources/100_goblins/goblins_solutions.js @@ -0,0 +1,224 @@ +// ================================= +// THOM'S +// ================================= + +// make an array of goblins +var goblins = []; +for (var i=0; i < 100; i++) { + goblins.push(false); +} + +// do stuff +for (var i=0; i < goblins.length; i++) { + for (var j=i; j < goblins.length; j += (i + 1)) { + goblins[j] = !goblins[j]; + } +} + +// change the result to numbers +var result = []; +for (var i=0; i < goblins.length; i++) { + if (goblins[i]) { + result.push(i + 1); + } +} + +console.log(result); + + +//==================================== +// JESSE'S +//==================================== + +var goblins = []; +for (var i = 0; i < 100; i++) { + goblins.push(false); +}; + +var cureAllGoblins = function(goblins, callback) { + // Set all the goblins to true + for (var i = 0; i < goblins.length; i++) { + goblins[i] = true; + } + + // Set the starting position for the goblins and the rate + // So in this case, num = starting with the 2nd goblin and rate is + // curing every two goblins + var num = 1; + var rate = 2; + + // Use the callback passing in those starter values + return callback(num, rate) +} + +var fakeCureGoblins = function(num, rate) { + // After 100 loops + if (num >= 100) { + var array = []; + for (var i = 0; i < goblins.length; i++) { + // If it's true, push the index to the array + if (goblins[i]) { + // Shift index by 1 + array.push(i+1); + }; + }; + // And return it + return array; + }; + + // Loop over the goblins based off the num / rate passed in + for (var i = num; i < goblins.length; i+=rate) { + goblins[i] = !goblins[i]; + } + + // Increment the starting position for 1 goes to 2 to start at the 3rd goblin + // and rate goes from 2 to 3 to go every third goblin + num++; + rate++; + + return fakeCureGoblins(num, rate); +}; + +console.log(cureAllGoblins(goblins, fakeCureGoblins)); + + +//==================================== +// AMBER'S +//==================================== + +// first set up array of all goblins +// set up object with all values false iterating through array +goblinObject = {}; + +for (i = 1; i < 101; i++) { + goblinObject[i] = false; +} + +console.log('initial setup: ', goblinObject); + +// loop +for (i = 1; i < 101; i++) { + // now need another loop inside to iterate through the i's + for (j = i; j < 101; j = j + i) { + // now, need to run through and change all trues to falses and vice versa + if (goblinObject[j] == false) { + goblinObject[j] = true; + } + else if (goblinObject[j] == true) { + goblinObject[j] = false; + } + } +} + +console.log(goblinObject); + + +// delete objects from array +for (i = 1; i < 101; i++) { + if (goblinObject[i] == false) { + delete goblinObject[i]; + } +} + +// console.log(goblinObject); + +// make an array of true values only +var goblinArray = []; + +for (var key in goblinObject) { + goblinArray.push(key); +} + +console.log(goblinArray); + + +//==================================== +// DEREK'S +//==================================== + +var goblins = []; + +// pushing goblin objects into array +for (var i=1; i < 101; i++) { + var goblin = { + id: i, + cured: false, + }; + goblins.push(goblin); +}; + +// counter to increase nested loop properly +var count = 1; + +for (var i=0; i < goblins.length; i++) { + for (var j=i; j < goblins.length; j = j+count) { + if (goblins[j].cured == true) { + goblins[j].cured = false; + } else if (goblins[j].cured == false) { + goblins[j].cured = true; + } + } + count += 1; +}; + +var finalArray = []; + +for (var i=0; i < goblins.length; i++) { + if (goblins[i].cured == true) { + finalArray.push(goblins[i].id); + } +}; + +console.log(finalArray); + + +//==================================== +// JOHN'S +//==================================== + +goblinArray = []; + +for (var i = 1; i < 101; i++) { + goblinArray.push({ + number : i, + cured : false + }) +}; + +var count = 0; + +var cureAll = function () { + for (var i = 0; i < goblinArray.length; i++) { + goblinArray[i].cured = true; +} + count += 1; +}; + +cureAll(); + +var reviseCure = function () { + for (var i = count; i < goblinArray.length; i += (count+1)) { + if (goblinArray[i].cured === false) { + goblinArray[i].cured = true; + } else if (goblinArray[i].cured === true) { + goblinArray[i].cured = false; + } + } + count += 1; + // console.log(count); +} + +while (count < 100) { + reviseCure(); +} + +var curedArray = []; + +for (var i = 0; i < goblinArray.length; i++ ) { + if (goblinArray[i].cured === true) { + curedArray.push(goblinArray[i].number); + } +} + +console.log("Here are the cured goblin numbers " + curedArray); + diff --git a/morning_exercise_resources/arithmetic_geometric/arithmetic_geometric.md b/morning_exercise_resources/arithmetic_geometric/arithmetic_geometric.md new file mode 100644 index 0000000..3105367 --- /dev/null +++ b/morning_exercise_resources/arithmetic_geometric/arithmetic_geometric.md @@ -0,0 +1,23 @@ +# Arithmetic or Geometric sequence? + +Write a method `arith_geo` that takes an **array of numbers** and returns the string "Arithmetic" if the sequence in the array follows an arithmetic pattern, or returns "Geometric" if it follows a geometric pattern. If the sequence doesn't follow either pattern return -1. + +An **arithmetic sequence** is one where the difference between each of the numbers is consistent, eg. the difference between each number is `5`. + +Whereas in a **geometric sequence**, each term after the first is multiplied by some constant or common ratio. + +``` +Arithmetic example: [2, 4, 6, 8] +``` + +``` +Arithmetic example: [20, 28, 36, 44] +``` + +``` +Geometric example: [2, 6, 18, 54] +``` + +``` +Geometric example: [6, 12, 24, 48] +``` \ No newline at end of file diff --git a/morning_exercise_resources/caesar_cipher/caesar_cipher.md b/morning_exercise_resources/caesar_cipher/caesar_cipher.md new file mode 100644 index 0000000..2f43d1f --- /dev/null +++ b/morning_exercise_resources/caesar_cipher/caesar_cipher.md @@ -0,0 +1,17 @@ +## The Caesar Cipher + +From Wikipedia: + +> In cryptography, a Caesar cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. The method is named after Julius Caesar, who used it in his private correspondence. + +We're going to implement a simple Caesar Cipher called ROT13 ("rotate by 13 places"). The transformation can be represented by aligning two alphabets, like so: + +``` +Plain: abcdefghijklmnopqrstuvwxyz +Cipher: nopqrstuvwxyzabcdefghijklm +``` + +ROT13 is its own inverse; that is, to undo ROT13, the same algorithm is applied, so the same action can be used for encoding and decoding. The algorithm provides virtually no cryptographic security, and is often cited as a canonical example of weak encryption. ROT13 is used in online forums as a means of hiding spoilers, punchlines, and puzzle solutions from the casual glance. + +Write a method that will take a string as an input and encode / decode it using ROT13. + diff --git a/morning_exercise_resources/clock_hands/clock_solution.js b/morning_exercise_resources/clock_hands/clock_solution.js new file mode 100644 index 0000000..7a18883 --- /dev/null +++ b/morning_exercise_resources/clock_hands/clock_solution.js @@ -0,0 +1,67 @@ +// SEE BELOW FOR COMMENTED VERSION + +var clock = function(hourhand, minutehand) { + + if (hourhand > 12 || minutehand > 59) { return "out of range" } + + var hoursToMinutes = (hourhand * 5) + (minutehand / 12); + var minutes = minutehand; + + if (hoursToMinutes > 60) { hoursToMinutes -= 60 } + + var angle; + + if (hoursToMinutes > minutes) { + angle = (hoursToMinutes - minutes) * 6; + } else { + angle = (minutes - hoursToMinutes) * 6; + } + + return [angle, 360 - angle] + +} + + +// COMMENTED +var clock = function(hourhand, minutehand) { + + // If input is out of range, quit the function + if (hourhand > 12 || minutehand > 59) { return "out of range" } + + // Convert hours to minutes so that we have a direct comparison between input values. + // Values: + // - hourhand * 5: an hour times five equals the same position in minutes. + // Example: Hour 6 is at the 30 minute position. 6 * 5 = 30. + // - minutehand / 12: there are five 'ticks' of minutes between hour positions. Across + // sixty ticks, that leaves a quotient of 12. This will be added to the + // hoursToMinutes to account for 'drift' of the hour hand. + // Example: at 6:30, 30 minutes is at the halfway point between 6 and 7. + // Since there are five 'ticks' between 6 and 7, the halfway point is 2.5. + // 30 / 12 = 2.5. This gets added to the hour hand position. + var hoursToMinutes = (hourhand * 5) + (minutehand / 12); + + // Just rename minutehand to minutes for naming consistency with hoursToMinutes. + var minutes = minutehand; + + // if the addition of 'drift' pushes the hoursToMinutes above 60, take away 60 + // (the clock's position is after midnight/noon, so back to the beginning). + if (hoursToMinutes > 60) { hoursToMinutes -= 60 } + + var angle; + + // Ascertain which is bigger, hoursToMinutes or minutes, and deduct the smaller + // from the larger. Then, convert minutes into degrees with * 6. One minute is 6 degrees. + // There are 60 minutes in a 360 degree minute hand revolution. Divide the total + // degrees (360), by the number of minutes in a revolution (60) to see the quotient + // in degrees. 360 / 60 = 6 degrees for each minute. + if (hoursToMinutes > minutes) { + angle = (hoursToMinutes - minutes) * 6; + } else { + angle = (minutes - hoursToMinutes) * 6; + } + + // Return the difference between hoursToMinutes and minutes, and + // also return the other angle by deducting it from the 360 degrees total. + return [angle, 360 - angle] + +} \ No newline at end of file diff --git a/morning_exercise_resources/clock_hands/clockhands.md b/morning_exercise_resources/clock_hands/clockhands.md new file mode 100644 index 0000000..6b2547c --- /dev/null +++ b/morning_exercise_resources/clock_hands/clockhands.md @@ -0,0 +1,55 @@ +## CLOCK HAND ANGLE CALCULATOR + +![clock](http://orpheogroup.com/wp-content/uploads/2014/10/grand-central-clock-crop.jpg) + + +Write a function `clock` that takes two integers, `hour` and `minute`. The function should calculate the two angles in degrees between the **hour hand** and **minute hand** on a twelve-hour analog clock face. + +Note that the hour hand has 'drift'. If the time is **6:30**, the hour hand will be halfway through its travel between **6** and **7**. If the time is **9:45**, the hour hand will be three quarters of the way between **9** and **10**. + +Return an "out of range" message if an input is greater than the clock's range. + + +Expected output: + +``` +clock(6, 00) + +=> [180, 180] +``` + +``` +clock(12, 00) + +=> [360, 0] +``` + +``` +clock(12, 15) + +=> [82.5, 277.5] +``` + +``` +clock(9, 45) + +=> [22.5, 337.5] +``` + +``` +clock(1, 59) + +=> [294.5, 65.5] +``` + +``` +clock(500, 34) + +=> "out of range" +``` + + + + + + diff --git a/morning_exercise_resources/clocks_jquery/clock.js b/morning_exercise_resources/clocks_jquery/clock.js new file mode 100644 index 0000000..4a39cd2 --- /dev/null +++ b/morning_exercise_resources/clocks_jquery/clock.js @@ -0,0 +1,9 @@ +$(function(){ + clock(); +}); + +function clock() { + + // write some code here + +} \ No newline at end of file diff --git a/morning_exercise_resources/clocks_jquery/clock.md b/morning_exercise_resources/clocks_jquery/clock.md new file mode 100644 index 0000000..99805a2 --- /dev/null +++ b/morning_exercise_resources/clocks_jquery/clock.md @@ -0,0 +1,32 @@ +Morning Exercise 12-05 + +#### Learning Objectives +- Practice using jQuery to manipulate the DOM +- Practice using JavaScript Timers +- Practice using internal JavaScript Objects + +# Make a working virtual clock + +Today you are going to build a clock! + +Start by planning! Draw a clock, break down the problem. + +### Step 1 +Define 3 variables and grab the associated element from the DOM using jQuery: +- minuteHand +- secondHand +- hourHand + +### Step 2 +- Using setInterval and jQuery make the second hand do a full rotation in 60 seconds. +- You will have to utilize the css attribute `transform - rotate`. + +### Step 3 +- Make the minute hand do a full rotation in 1 hour + +### Step 4 +- Make the hour hand do a full rotation in 12 hours + +### BONUS +- Sync up your clock with the current time. +- Take a look at the JavaScript `Date` object diff --git a/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/clock.js b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/clock.js new file mode 100644 index 0000000..d46f430 --- /dev/null +++ b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/clock.js @@ -0,0 +1,33 @@ +$(function(){ + clock(); +}); + +function clock() { + + var secHand = $('#second-hand'); + var minHand = $('#minute-hand'); + var hourHand = $('#hour-hand'); + + var currentTime = new Date(); + var secDeg = currentTime.getSeconds() * 6; + var minDeg = currentTime.getMinutes() * 6 + currentTime.getSeconds() * 0.1; + var hourDeg = currentTime.getHours() * 30 + currentTime.getMinutes() * 0.5; + + setInterval(function() { + // Rotate the secondHand 6 degree every 1000 ms (1 second). + secDeg += 6; + console.log('seconds degrees: ', secDeg); + // Rotate the minuteHand - the minuteHand rotates 6 degrees every minute + // there are 60 seconds in each minute. + minDeg += (6/60); + console.log('minutes degrees: ', minDeg); + // Step 4 + // rotate the hourHand - the hourHand rotates 30 degrees every hour. There are + // 3600 hundred seconds in each hour. + hourDeg += (30/3600); + console.log('hours degrees: ', hourDeg); + secHand.css('transform', 'rotate(' + secDeg + 'deg)'); + minHand.css('transform', 'rotate(' + minDeg + 'deg)'); + hourHand.css('transform', 'rotate(' + hourDeg + 'deg)'); + }, 1000); +} \ No newline at end of file diff --git a/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/clock.md b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/clock.md new file mode 100644 index 0000000..99805a2 --- /dev/null +++ b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/clock.md @@ -0,0 +1,32 @@ +Morning Exercise 12-05 + +#### Learning Objectives +- Practice using jQuery to manipulate the DOM +- Practice using JavaScript Timers +- Practice using internal JavaScript Objects + +# Make a working virtual clock + +Today you are going to build a clock! + +Start by planning! Draw a clock, break down the problem. + +### Step 1 +Define 3 variables and grab the associated element from the DOM using jQuery: +- minuteHand +- secondHand +- hourHand + +### Step 2 +- Using setInterval and jQuery make the second hand do a full rotation in 60 seconds. +- You will have to utilize the css attribute `transform - rotate`. + +### Step 3 +- Make the minute hand do a full rotation in 1 hour + +### Step 4 +- Make the hour hand do a full rotation in 12 hours + +### BONUS +- Sync up your clock with the current time. +- Take a look at the JavaScript `Date` object diff --git a/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/hourhand.png b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/hourhand.png new file mode 100755 index 0000000..67d44a6 Binary files /dev/null and b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/hourhand.png differ diff --git a/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/minutehand.png b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/minutehand.png new file mode 100755 index 0000000..9ec502e Binary files /dev/null and b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/minutehand.png differ diff --git a/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/secondhand.png b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/secondhand.png new file mode 100755 index 0000000..9aeaa39 Binary files /dev/null and b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/img/secondhand.png differ diff --git a/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/index.html b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/index.html new file mode 100644 index 0000000..a86d87d --- /dev/null +++ b/morning_exercise_resources/clocks_jquery/clocks_jquery_solution/index.html @@ -0,0 +1,47 @@ + + + + + Clocks + + + + + + +

Clock.

+ +
+ + + +
+ + \ No newline at end of file diff --git a/morning_exercise_resources/clocks_jquery/img/hourhand.png b/morning_exercise_resources/clocks_jquery/img/hourhand.png new file mode 100755 index 0000000..67d44a6 Binary files /dev/null and b/morning_exercise_resources/clocks_jquery/img/hourhand.png differ diff --git a/morning_exercise_resources/clocks_jquery/img/minutehand.png b/morning_exercise_resources/clocks_jquery/img/minutehand.png new file mode 100755 index 0000000..9ec502e Binary files /dev/null and b/morning_exercise_resources/clocks_jquery/img/minutehand.png differ diff --git a/morning_exercise_resources/clocks_jquery/img/secondhand.png b/morning_exercise_resources/clocks_jquery/img/secondhand.png new file mode 100755 index 0000000..9aeaa39 Binary files /dev/null and b/morning_exercise_resources/clocks_jquery/img/secondhand.png differ diff --git a/morning_exercise_resources/clocks_jquery/index.html b/morning_exercise_resources/clocks_jquery/index.html new file mode 100644 index 0000000..f43b18f --- /dev/null +++ b/morning_exercise_resources/clocks_jquery/index.html @@ -0,0 +1,47 @@ + + + + + Clocks, Bro + + + + + + +

Clock.

+ +
+ + + +
+ + \ No newline at end of file diff --git a/morning_exercise_resources/js_iterators/10. js_iterators.md b/morning_exercise_resources/js_iterators/10. js_iterators.md new file mode 100644 index 0000000..81a03db --- /dev/null +++ b/morning_exercise_resources/js_iterators/10. js_iterators.md @@ -0,0 +1,127 @@ + + +# JAVASCRIPT ITERATORS + +#####`.forEach`, `.map`, and `.reduce` + +Javascript iterators are like loops. They step (or iterate) over each item in an array. Iterators perform operations according to conditions specified in a callback. + +# .forEach + +`.forEach` is the basic iterator, all it does is step through each item in an array. + +#### EXAMPLE: + +``` +arr = [8, 8, 8, 8, 8, 8, 8, 8]; + +arr.forEach(function(n) { + console.log(n + 1); +}); + +=> Prints a bunch of 9s to the console + +``` + + +##### EXERCISE: +- Log each word in `words` in upper case using `.forEach.` + +``` +words = ["Ho", "Chi", "Minh", "City", "was", "once", "known", "as", "Prey", "Nokor"]; +``` + + + + +# .map + +`.map` is like `.forEach` but it returns a new array with the selections and / or transformations applied in the iterator. `.map` saves to a new array and does not alter the original array. + + +EXAMPLE: + +``` +var new_arr = arr.map(function(n) { + return n += n / 2 +}); + +=> [12, 12, 12, 12, 12, 12, 12, 12] +``` + +####EXERCISE: + +``` +more_words = ["Joe", "Overreats", "Eggs"]; +``` + +- Map to a new array that stores only the first letter from each word in `more_words`. + +Expected result: + +``` +["J", "O", "E"] +``` + +... + +# .reduce + +`.reduce` iterates over the elements in an array and smooshes them together into a single variable according to the instructions in the iterator. Like `.map`, it returns a new value and does not alter the original array. + +#### EXAMPLE: + +Add together all numbers in an array: + +``` +new_value = arr.reduce(function(sum, n){ + return sum += n +}); + +=> 64 +``` + + +#### EXERCISE: +``` +arr = [8, 8, 8, 8, 8, 8, 8, 8]; +``` + +- Return the product of the numbers in `arr`. + + + +... + +#### EXERCISE: + +Using both `.map` and `.reduce`, find the total sheepCount of sheep shorn by sheep shearers at the 1623 Sheep Shearing Feast: + +``` +var sheepShearers = [ + { + name: "Tim", + age: 20, + sheepCount: 18 + }, + { + name: "C3PO", + age: 200, + sheepCount: 320 + }, + { + name: "Cousin It", + age: Infinity, + sheepCount: 2900 + } +]; +``` + +``` +=> 3238 +``` + + + + + diff --git a/morning_exercise_resources/js_iterators/solution.js b/morning_exercise_resources/js_iterators/solution.js new file mode 100644 index 0000000..b49d898 --- /dev/null +++ b/morning_exercise_resources/js_iterators/solution.js @@ -0,0 +1,31 @@ +console.log('w'); + + +var sheepShearers = [ + { + name: "Tim", + age: 20, + sheepCount: 18 + }, + { + name: "C3PO", + age: 200, + sheepCount: 320 + }, + { + name: "Cousin It", + age: Infinity, + sheepCount: 2900 + } +]; + + +var total = sheepShearers + .map(function(item) { + return item.sheepCount; + }) + .reduce(function(sum, num) { + return sum += num; + }); + +console.log(total); \ No newline at end of file diff --git a/morning_exercise_resources/luhn_algorithm/luhn_algorithm.md b/morning_exercise_resources/luhn_algorithm/luhn_algorithm.md new file mode 100644 index 0000000..fb8d619 --- /dev/null +++ b/morning_exercise_resources/luhn_algorithm/luhn_algorithm.md @@ -0,0 +1,35 @@ +# The Luhn Algorithm + +Credit card numbers can be validated with a process called the Luhn algorithm. The Luhn algorithm works like this: + +1. Starting with the next to last digit and continuing with every other digit going back to the beginning of the card number, double the digit. + +2. Sum all digits in the altered number. + +3. If that total is a multiple of 10, the number is valid. + +For example, given the card number 4408 0412 3456 7893: + +``` +Orig : 4 4 0 8 0 4 1 2 3 4 5 6 7 8 9 3 +Step 1: 8 4 0 8 0 4 2 2 6 4 10 6 14 8 18 3 +Step 2: 8+4+0+8+0+4+2+2+6+4+1+0+6+1+4+8+1+8+3 = 70 +Step 3: 70 % 10 == 0 +``` + +[Luhn Algorithm](http://en.wikipedia.org/wiki/Luhn_algorithm) + +### Code + +Write a function `validCard` that takes a number as an argument and returns true for a valid number and false for an invalid number. + +``` +console.log(validCard(1234567890123456)); #should be false +console.log(validCard(4408041234567893)); #should be true +console.log(validCard(38520000023237)); #should be true +console.log(validCard(4222222222222)); #should be true +``` + +### BONUS + +Refactor down to 16 lines or less of usable code not including lines of whitespace. \ No newline at end of file diff --git a/morning_exercise_resources/luhn_algorithm/luhn_solutions.js b/morning_exercise_resources/luhn_algorithm/luhn_solutions.js new file mode 100644 index 0000000..5d30f2c --- /dev/null +++ b/morning_exercise_resources/luhn_algorithm/luhn_solutions.js @@ -0,0 +1,68 @@ +// THOM'S + +var validCard = function(num) { + + var reversedArr = num.toString().split('').reverse(); + + for (var i=1; i < reversedArr.length; i+=2) { + reversedArr[i] = reversedArr[i] * 2; + } + + reversedArr = reversedArr.join('').split(''); + + var summed = reversedArr + .map(function(n) { return n = parseInt(n); }) + .reduce(function (sum, m) { return sum + m; }); + + return summed % 10 == 0; +} + + +// DEREK'S + +var validCard = function(n){ + var string = n.toString(); + var num = string.split('').map(function(data){ + return parseInt(data, 10); + }); + for (var i=num.length - 2; i >= 0; i = i-2) { + num[i] = num[i] * 2 + }; + var card = num.join('').split('').map(function(data){ + return parseInt(data, 10); + }); + var newNum = card.reduce(function(a, b){ + return a + b + }); + if (newNum % 10 == 0) { return true } else { return false }; +}; + + +// CHRISTINE'S + +var validCard = function (num) { + var creditNum = num.toString().split(""); + sum = 0; + + //for every other character, starting from the penultimate one + // turn it back into number and take the sum + for (var j = creditNum.length-2; j >= 0; j-=2) { + var double = parseInt(creditNum[j]) * 2; + + var stringify = double.toString().split(""); + + //sum up the double digit numbers + for (var k = 0; k < stringify.length; k++){ + sum += parseInt(stringify[k]); + } + }//ends forloop + + //add the rest of the 2n numbers + for (var m = creditNum.length-1; m >= 0; m-=2){ + sum += parseInt(creditNum[m]); + } + + //if divisible by 10, it is valid + return (sum % 10 === 0) +}; + diff --git a/morning_exercise_resources/more_nested_loops/more_nested_loops.md b/morning_exercise_resources/more_nested_loops/more_nested_loops.md new file mode 100644 index 0000000..fdbecfb --- /dev/null +++ b/morning_exercise_resources/more_nested_loops/more_nested_loops.md @@ -0,0 +1,34 @@ +## MORE NESTED LOOPS + + +``` +var months = ["January", "February", "March", "April", "May", "June", + "July", "August", "September", "October", "November", "December"]; +``` + + +Timothy D. Bowlcut is an events planner, and he has a list of `months` in the year that he uses for planning. When January has gone, he wants a new list without January. When February has gone, he wants a new list without January or February. He wants this all the way up until December is the only one left. + +Give Timothy all the lists in order that he wants. + +You will eventually need nested *for loops* to solve this, but start with one *for loop* to get all the months. + +Expected output: + +``` +=> + +["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] +["February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] +["March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] +["April", "May", "June", "July", "August", "September", "October", "November", "December"] +["May", "June", "July", "August", "September", "October", "November", "December"] +["June", "July", "August", "September", "October", "November", "December"] +["July", "August", "September", "October", "November", "December"] +["August", "September", "October", "November", "December"] +["September", "October", "November", "December"] +["October", "November", "December"] +["November", "December"] +["December"] +``` + diff --git a/morning_exercise_resources/nested_building/multi_nested_building.md b/morning_exercise_resources/nested_building/multi_nested_building.md new file mode 100644 index 0000000..f3624bf --- /dev/null +++ b/morning_exercise_resources/nested_building/multi_nested_building.md @@ -0,0 +1,46 @@ +# THE BUILDING + +![building](http://s3.amazonaws.com/downtownbrooklyn/data/Downtown_Brooklyn/releases/20130227172638/public/ckeditor_assets/pictures/103/content_header-81-willoughby.jpg) + +## Nested for loops +## Iterating over multi-dimensional arrays + + +``` +var building = [ + ["Aaron", "Agamemnon"], + ["Bob", "Blob"], + ["Catherine", "Carrot", "Chris"], + ["Dustin", "Drapes"], + ["Ekaterina", "Elbow-Bone"], + ["Francesca", "Flan", "Flipper", "Fozzy"], + ["Gustina"], + ["Harriet", "Harmony", "Ham"] + ]; +``` + + +The building array is a multi-dimensional array representing an eight-storie building. Each of the inner arrays contains the occupants of a particular floor of the building. + + +### Directions + + +Susan, the building manager, wants a couple of things from you. + +- Provide her with a list of everyone in the building-- an array of strings called `roster`. Using a for loop, iterate over the buildings array in order to access each floor. To access each occupant of each floor, you will need to use another loop inside this one. Iterate over the elements of each floor, and push each name into the new `roster` array. + +``` +Expected result: +=> ["Aaron", "Agamemnon", "Bob", "Blob", "Catherine", etc ... ] +``` + + +- Susan is neurotic and wants a list only of the occupants in the buildings array who live on an even-numbered index. She wants the names to be reversed. She also wants the names cased and capitalized properly as if they were not reversed. Example: "Aaron" would become "Noraa". Iterate over the buildings array and put the valid entries into a new array called `neurosis`. + +``` +Expected result: + +=> ["Noraa", "Nonmemaga", "Enirehtac", "Torrac", "Sirhc", "Aniretake", "Enob-woble", "Anitsug"] + +``` diff --git a/morning_exercise_resources/nested_building/solution.js b/morning_exercise_resources/nested_building/solution.js new file mode 100644 index 0000000..88a2e2e --- /dev/null +++ b/morning_exercise_resources/nested_building/solution.js @@ -0,0 +1,36 @@ +var building = [ + ["Aaron", "Agamemnon"], + ["Bob", "Blob"], + ["Catherine", "Carrot", "Chris"], + ["Dustin", "Drapes"], + ["Ekaterina", "Elbow-Bone"], + ["Francesca", "Flan", "Flipper", "Fozzy"], + ["Gustina"], + ["Harriet", "Harmony", "Ham"] + ]; + + +var neurosis = []; + +for (var i=0; i < building.length; i++) { + + if (i % 2 === 0) { + + for (var j=0; j < building[i].length; j++) { + + var reversed = building[i][j].split('').reverse().join(''); + var lowerCased = reversed.toLowerCase(); + var result = lowerCased[0].toUpperCase() + lowerCased.slice(1); + + neurosis.push(result); + + } + + } + +} + +console.log(neurosis); + + + diff --git a/morning_exercise_resources/pandigital_numbers/pandigital_numbers.md b/morning_exercise_resources/pandigital_numbers/pandigital_numbers.md new file mode 100644 index 0000000..649217a --- /dev/null +++ b/morning_exercise_resources/pandigital_numbers/pandigital_numbers.md @@ -0,0 +1,14 @@ +#PANDIGITAL NUMBERS + +A number of length n is pandigital if it makes use of all the digits 1 to n exactly once. + +- The number `15234` is 1 through 5 pandigital. + +- The number `333` is not pandigital. + +- The number `0` is not pandigital. + +- The number `987654321` is pandigital. + +###DIRECTIONS +Write a function that checks if a number is pandigital. \ No newline at end of file diff --git a/morning_exercise_resources/pandigital_numbers/pandigital_numbers_solutions.js b/morning_exercise_resources/pandigital_numbers/pandigital_numbers_solutions.js new file mode 100644 index 0000000..5ea1748 --- /dev/null +++ b/morning_exercise_resources/pandigital_numbers/pandigital_numbers_solutions.js @@ -0,0 +1,115 @@ +// THOM'S + +var pandigital = function(num) { + var len = num.toString().length; + var digits = num.toString().split(''); + + for (var i=0; i < len; i++) { + if (digits[i] == 0) { + return "Zero detected: cannot have number of length 0"; + } else if (digits[i] > len) { + return "Not pandigital because number(s) are greater than length: " + len; + } else { + for (var j=i + 1; j < len; j++) { + if (digits[j] == digits[i]) { + return "Not pandigital because numbers are not unique"; + } + } + } + } + + return "Pandigital number found!"; +} + + +// JOHN'S + +var checkNumber = function(num) { +//sets a counter, to be used in while loop +var count = 0; +//turns the num parameter into an array of strings +var numSplit = (''+num).split(''); + //sorts the array of strings +var sort = numSplit.sort(function(a, b){return a-b}); +//joins the sorted array into a number +var sortJoin = sort.join(''); +//sets an empty array for numbers that equal to i to be pushed into +var pan = []; +//var i, used to compare with numbers in the num array +var i = 1; +//loops through the for loop for however many digits are in the number, each time it goes through count goes up by one +while ( count < numSplit.length) { + //iterates through the num arrray, compares each sorted index position to i (from 1 to the array length) + for (var j = 0; j < numSplit.length; j ++) { + if ( i == sort[j] && sort[j] != pan[j]) { + pan.push(sort[j]); //will push the index position number to pan array if it equals to i & it's not already in pan + i += 1; //adds one to i each loop through + } else { + i += 1; //same + + } + } + i = 1; //resets i after the for loop has gone through + count += 1; + } + //joins the numbers in the pan array + var pandigital = pan.join(''); + //compares the joined pan number and the sorted input num + if (pandigital == sortJoin) { + console.log(num + " is a pandigital number"); //if equal + } else { + console.log(num + " is not a pandigital number"); //if not + } +}; + + +// ANDY'S + +var panCheck = function(num){ + + // num becomes a string and then the individual digits are pushed to an array + array = []; + sNumber = num.toString(); + console.log(sNumber) + for (var i = 0; i < sNumber.length; i ++) { + array.push(+sNumber.charAt(i)); + } + console.log(array) + // this thing checks for duplicates + var noDupes = true + array.sort(); + console.log(array) + for ( var i = 1; i < array.length; i++ ){ + if(array[i-1] == array[i]) + noDupes = false; + } + + if (noDupes === false){console.log("not pandigital because a digit occurs more than once"); return} + + var bingo = 0 + // bingos are the number of matches between j + // and items in the array + // an array of length 9 must find a matching '9' somewhere + // and then a matching '8' when j--, etc + + for (var j = array.length; j >= 0; j--){ + console.log('i am checking if ' + j) + for(var k = 0; k< array.length; k++){ + console.log('is equal to array position ' + k + " which is " + array[k]) + //eliminate previously used + + if (array[k] == j){ + // if there is a match, bingo +=1 + bingo += 1 + console.log("***Bingo*** The number of bingos is " + bingo +"."); + // if the number of bingos = length of the array + if (bingo == array.length && noDupes === true){ + console.log(num + ' is pandigital'); + return true + } + } + } + } + console.log(num + " is not pandigital") + return false +} \ No newline at end of file diff --git a/morning_exercise_resources/prime_numbers/primes.md b/morning_exercise_resources/prime_numbers/primes.md new file mode 100644 index 0000000..67d0b71 --- /dev/null +++ b/morning_exercise_resources/prime_numbers/primes.md @@ -0,0 +1,14 @@ +### PRIME NUMBERS + +A Prime number is a number that is not evenly divisible by another number except 1 and itself. + +To test whether a number is Prime, you only need to test as far as the square root of that number. This +is advisable for optimization and testing large numbers. + +## STEP ONE +1. Write a function called `isPrime` that will test whether a number is Prime. The function will return true if Prime, false if not. + +## STEP TWO (Bonus) +2. Write another function called `primes` that will print an array of Primes up to an arbitrary limit. For example, if you invoke your function with `primes(100)`, it will print all the Prime numbers up to and including 100. + +This function can call on the previous `isPrime` function. diff --git a/morning_exercise_resources/recursive_factorial/recursive_factorial.md b/morning_exercise_resources/recursive_factorial/recursive_factorial.md new file mode 100644 index 0000000..cc1bb7e --- /dev/null +++ b/morning_exercise_resources/recursive_factorial/recursive_factorial.md @@ -0,0 +1,105 @@ +# RECURSION with factorials + +## What's a factorial? + +``` +In mathematics, the factorial of a non-negative integer n is the product of all positive integers less than or equal to n. +``` + +The factorial of 5 is 120: + +5 x 4 x 3 x 2 x 1 = **120** + + +The factorial of 4 is 24: + +4 x 3 x 2 x 1 = **24** + +- You are going to write a recusive function that returns the factorial of a given number. + +## What's recursion? + +- A function that calls **itself** is a recursive function. It will keep calling itself in a loop. + +- A recursive function needs a **condition** by which it will +**exit**. Otherwise it will run indefinitely and destroy your computer from the inside out, forever. + +- A recursive function needs to pass its parameters to the next invocation without those parameters being reset, unless you want them to be. + +### NOTES +Here's an example of a recursive function that prints numbers from an arbitrary input down to 1: + +``` +var countdown = function(num) { + + // 1. exit condition + if (num === 1) { return } + + // 2. some kinda process + console.log(num); + num -= 1; + + // 3. call the function from within itself + return countdown(num); + +}; // end of function +``` + +In the above example, the operation on `num` could also be passed in the call argument: + +``` + +var countdown = function(num) { + + // 1. exit condition + if (num === 1) { return } + + // 2. some kinda process + console.log(num); + + // 3. call the function from within itself + return countdown(num - 1); + +}; // end of function +``` + +## DIRECTIONS + +1. Copy, paste, and run the `countdown` function. + +2. Using `countdown` as a template, write a **recursive function** `fac` that **returns** the factorial of a given number. Refrain from using **for loops** for this exercise. + +### Note: +In the `fac` function, you will want the product to be increased each time the function runs, not reset. So, you might not want to define `product = 1` within your function. You might want to set the product as an initial condition for when you call the function: + +``` +var fac = function(num, product) { + + // 1. exit condition + + // 2. stuff + + // 3. call fac + +} +``` + +Finding the factorial of 5, invoking the function with an initial product of 1: +`fac(5, 1);` + + + + + + + + + + + + + + + + + diff --git a/morning_exercise_resources/recursive_factorial/solution.js b/morning_exercise_resources/recursive_factorial/solution.js new file mode 100644 index 0000000..959477a --- /dev/null +++ b/morning_exercise_resources/recursive_factorial/solution.js @@ -0,0 +1,22 @@ +var fac = function(num, prod) { + + if (num === 0) { return prod } + + prod *= num; + + return fac(num - 1, prod); + +} + + + +// Sharon's solution +var fac = function(num) { + + if (num === 1) { + return 1; + } + + return num * fac(num-1); + +}; // end of function \ No newline at end of file diff --git a/morning_exercise_resources/ruby_methods/basic_ruby.md b/morning_exercise_resources/ruby_methods/basic_ruby.md new file mode 100644 index 0000000..3b55cfd --- /dev/null +++ b/morning_exercise_resources/ruby_methods/basic_ruby.md @@ -0,0 +1,107 @@ +# BASIC RUBY REFRESHER + +## Strings + +Perform the following tasks using string methods: + +1. Reverse the string, `"sarcophagus"`. +2. Return the substring, `"undulating"`, from the string, `"Are these artichokes undulating?"`. +3. Convert the string, `"90210"`, to an integer. +4. Capitalize the letter `"m"` in the string `"meeseeks"`. +5. Capitalize all of the letters in the string `"Capitalize all of the letters in the string"`. + + +## Arrays and Loops + +##### NOTES + +`.each` and `.map` + +You can use `.each` as a basic loop for pushing values into arrays or sending out transforms without altering the original array. For example, given the following dataset: + +``` +arr1 = ["moops", "cherish the cabin"] +arr2 = [] +``` + +`.each` for *pushing* into another array. +Note: `<<` is the same as `.push()` + +``` +arr1.each { |item| arr2 << item.upcase } +p arr2 + +=> ["MOOPS", "CHERISH THE CABIN"] +``` + +This is the expanded version using `do` and `end` instead of curlies: + +``` +arr1.each do |item| + arr2 << item.upcase +end + +=> ["MOOPS", "CHERISH THE CABIN"] +``` + +`.map`, by contrast, returns a transform of the dataset: + +``` +new_set = arr1.map { |item| item.upcase } +p new_set + +=> ["MOOPS, CHERISH THE CABIN"] +``` + +`.each` will not do a transform: + + +``` +new_set = arr1.each { |item| item.upcase } +p new_set + +=> ["moops", "cherish the cabin"] +``` + +##### EXERCISE + +Perform the following tasks using array methods (note: you might also need to use some string methods): Switch it up between `.map` and `.each` and any others you might find in the docs. + +1. Given the array `numbers = [1, 3, 5, 7]`, use `map` to create a new array with each element of `numbers` multiplied by three. Do it once with `do .. end` and once with curlies. + +2. Save a shuffled version of this array to a variable just to see how easy it is to do so. + +3. Give the array `names = ["Joe, Matt, Thom"]` use `each` to iterate over each word and print it to the console with " is my friend and contemporary." appended. Do it once with `do .. end` and again with curlies. + +4. Given the following array (of arrays): +``` +sandwiches_with_costs = [["blt", 5], ["grilled cheese", 7], ["turkey_club_with_pesto", 9], ["ramenburger", 6], ["portobello and garlic aioli", 10]] +``` +Create an empty hash (call it whatever you want) and then iterate over sandwiches_with_costs and add sandwich names as keys and cost amounts as values to the hash. + + +## Hashes + +Perform the following tasks using hash methods (note: you might also need to use some string and array methods). + +Given the hash: + +``` +breakfast_prefs = { + juice: "orange", + syrup: "maple", + potatoes: "mashed with molasses", + lettuce: "iceberg", + protein: "GNC Pro Performance AMP Amplified Wheybolic Extreme 60™ Original", + cups_of_coffee: 3, + pink_slime: false +} +``` + +1. Return an array containing only the keys of `breakfast_prefs`. +2. Return an array containing only the values of `breakfast_prefs`. +3. Change the value of `:syrup` to `"boysenberry"`. +4. Return a hash with the key-value pairs whose value is of type string. + + + diff --git a/morning_exercise_resources/ruby_methods/methods_solutions.rb b/morning_exercise_resources/ruby_methods/methods_solutions.rb new file mode 100644 index 0000000..5e3ebd5 --- /dev/null +++ b/morning_exercise_resources/ruby_methods/methods_solutions.rb @@ -0,0 +1,90 @@ +## USING RUBY METHODS + + +## PART ONE + +arr = [["Live", "Laugh", "Love"], {hello: 'hi back'}, false, 333.333, nil, nil, ["Joy", "Joke", "Jerk"]] + +arr.each do |item| + puts item.class +end + +# select and class +new_array = arr.select do |item| + item.class == Array +end + +new_arr.flatten.permutation.to_a + +new_array2 = arr.reject do |item| + item.class == Hash +end + +## PART TWO + +numbers = [4, 7, 8, 7, 9, 0, 4] + +#permuation.to_a +permutations = numbers.permutation.to_a + +# select, first, and last +perms = permutations.select do |item| + item.first == 7 && item.last == 7 +end + +result1 = numbers.reduce(:+) + +result2 = numbers.reduce(:*) + + +## PART THREE + +strings = ["Paloma", "Grits", "Ziti", "Carbohydrates", "Grits", "Corn", "Wizard_robe", "Ziti", "Corn", "Corn", "Maize"] + +hash = {} + +strings.uniq.each do |hashkey| + if hashkey == "Maize" then hash[hashkey] = "Not Fun" + else hash[hashkey] = "Fun" end +end + + +## PART FOUR + +students = [ + "Amber", + "Nicole", + "Christine", + "Dan", + "Ashleigh", + "Jordan", + "Alex", + "Emily", + "John", + "Sharon", + "Levi", + "Pauline", + "Masha", + "Matt", + "Andy", + "Sammy", + "Dominic", + "Vincent", + "Jesse", + "Juan", + "Josh", + "Derek" + ]; + +shuffled_students = students.shuffle + +p shuffled_students.each_slice(3) { |stuff| p stuff }; + +# Go to jail +p students.sample + + + + + + diff --git a/morning_exercise_resources/ruby_methods/ruby_methods.md b/morning_exercise_resources/ruby_methods/ruby_methods.md new file mode 100644 index 0000000..6b6a169 --- /dev/null +++ b/morning_exercise_resources/ruby_methods/ruby_methods.md @@ -0,0 +1,116 @@ +## SELECTED RUBY METHODS + +Here are some exercises to become more acquainted with the following Ruby methods: + +1. `.each` +2. `.class` +3. `.select` +4. `.reject` +5. `.flatten` +6. `.permutation.to_a` +7. `.first` +8. `.last` +9. `.count` +10. `.reduce` +11. `.uniq` +12. `.shuffle` +13. `.each_slice` +14. `.sample` +15. `.each_cons` + + +### 1 +Given the following array + +``` +arr = [["Live", "Laugh", "Love"], {hello: 'hi back'}, false, 333.333, nil, nil, ["Joy", "Joke", "Jerk"]] +``` + +1. Use `.each` and `.class` to print the class of each array element to the console + +2. Use `.select` and `.class` to return an array of only those elements whose class is Array. http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-select + +3. Use `.reject` and `.class` to return an array of only those elements whose class is not Hash. http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-reject + +4. Use `.flatten` on the result of your previous `.select` operation to flatten out the array + +5. Use `.permutation.to_a` on the flattened result to return all possible permutations of all the items within the inner arrays. + +6. Use `.count` to get the number of those permutations. + +### 2 + +Given the following array + +``` +numbers = [4, 7, 8, 7, 9, 0, 4] +``` + +1. Use `.permutation.to_a` and `.count` to return a count of all permutations + +2. `.select` only those permutations where the `.first` number is 7 and the `.last` number is also 7, and return a `.count` of those permutations. + +3. Use `.reduce` to get the sum of the array. + +4. Use `.reduce` to get the product of the array. + + + +### 3 + +Given the following array + +``` +strings = ["Paloma", "Grits", "Ziti", "Carbohydrates", "Grits", "Corn", "Wizard_robe", "Ziti", "Corn", "Corn", "Maize"] +``` + +and the empty hash + +``` +hash = {} +``` + +1. Using `.each` and `.uniq`, populate the hash using the elements in the array as keys. Remember that keys must be unique, so don't include any double-ups. Set the values of those keys to "Fun" except for Maize, which is "Not Fun". + + +### 4 + +``` +students = [ + "Amber", + "Nicole", + "Christine", + "Dan", + "Ashleigh", + "Jordan", + "Alex", + "Emily", + "John", + "Sharon", + "Levi", + "Pauline", + "Masha", + "Matt", + "Andy", + "Sammy", + "Dominic", + "Vincent", + "Jesse", + "Juan", + "Josh", + "Derek" + ]; +``` + +1. Pretend that group projects are coming up again, and you have to split the class up into random groups of three. Use `.shuffle` and `.each_slice` to generate groups of three (there will be one remainder). http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-each_slice. + +2. Use `.sample` to return a single random student to be taken to jail. http://ruby-doc.org/core-2.1.4/Array.html#method-i-sample + + +### 5. Make your own problem + +Look into the Ruby method `.each_cons`, http://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-each_cons + +Make your own problem where the answer should use `.each_cons`. + + diff --git a/morning_exercise_resources/stopwatch/css/style.css b/morning_exercise_resources/stopwatch/css/style.css new file mode 100644 index 0000000..9dbdf65 --- /dev/null +++ b/morning_exercise_resources/stopwatch/css/style.css @@ -0,0 +1,35 @@ +body { + text-align: center; + font-family: 'Orbitron', 'Courier New', monospace; +} + +#stopwatch { + width: 400px; + height: 100px; + margin: 0 auto; + border: 1px solid black; + padding: 12px; + + line-height: 100px; + font-size: 75px; + text-align: right; + + margin-bottom: 20px; +} + +#controls { + text-align: center; +} + +button { + padding: 24px 24px 16px 24px; + background: mediumseagreen; + border:none; + border-bottom: 2px solid forestgreen; + border-radius: 5px; + + color: white; + font-size: 24px; + font-family: 'Orbitron', 'Courier New', monospace; + font-weight: 700; +} diff --git a/morning_exercise_resources/stopwatch/index.html b/morning_exercise_resources/stopwatch/index.html new file mode 100644 index 0000000..a7918a8 --- /dev/null +++ b/morning_exercise_resources/stopwatch/index.html @@ -0,0 +1,20 @@ + + + + + + + + +
+ 0 +
+ +
+ + + + +
+ + diff --git a/morning_exercise_resources/stopwatch/js/app.js b/morning_exercise_resources/stopwatch/js/app.js new file mode 100644 index 0000000..501a6bb --- /dev/null +++ b/morning_exercise_resources/stopwatch/js/app.js @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/morning_exercise_resources/stopwatch/stopwatch_solution/css/style.css b/morning_exercise_resources/stopwatch/stopwatch_solution/css/style.css new file mode 100644 index 0000000..9dbdf65 --- /dev/null +++ b/morning_exercise_resources/stopwatch/stopwatch_solution/css/style.css @@ -0,0 +1,35 @@ +body { + text-align: center; + font-family: 'Orbitron', 'Courier New', monospace; +} + +#stopwatch { + width: 400px; + height: 100px; + margin: 0 auto; + border: 1px solid black; + padding: 12px; + + line-height: 100px; + font-size: 75px; + text-align: right; + + margin-bottom: 20px; +} + +#controls { + text-align: center; +} + +button { + padding: 24px 24px 16px 24px; + background: mediumseagreen; + border:none; + border-bottom: 2px solid forestgreen; + border-radius: 5px; + + color: white; + font-size: 24px; + font-family: 'Orbitron', 'Courier New', monospace; + font-weight: 700; +} diff --git a/morning_exercise_resources/stopwatch/stopwatch_solution/index.html b/morning_exercise_resources/stopwatch/stopwatch_solution/index.html new file mode 100644 index 0000000..a7918a8 --- /dev/null +++ b/morning_exercise_resources/stopwatch/stopwatch_solution/index.html @@ -0,0 +1,20 @@ + + + + + + + + +
+ 0 +
+ +
+ + + + +
+ + diff --git a/morning_exercise_resources/stopwatch/stopwatch_solution/js/app.js b/morning_exercise_resources/stopwatch/stopwatch_solution/js/app.js new file mode 100644 index 0000000..d18cebf --- /dev/null +++ b/morning_exercise_resources/stopwatch/stopwatch_solution/js/app.js @@ -0,0 +1,36 @@ +window.onload = function() { + document.getElementById("start").addEventListener("click", startTimer); + document.getElementById("stop").addEventListener("click", stopTimer); + document.getElementById("reset").addEventListener("click", resetTimer); + document.getElementById("countdown").addEventListener("click", countdownTimer); +}; + +var startTimer = function() { + var time = parseInt(document.getElementById("stopwatch").textContent); //move variable declarations outside the window.onload so that each function can have acess to the variables. + stopWatchHandle = setInterval(function() { + time += 1; + var timer = document.getElementById("stopwatch"); + timer.textContent = time; + }, 1000); +}; + +var resetTimer = function() { + clearInterval(stopWatchHandle); + var timer = document.getElementById("stopwatch"); + timer.textContent = "0"; +}; + +var countdownTimer = function () { + clearInterval(stopWatchHandle); + var time = parseInt(document.getElementById("stopwatch").textContent); + stopWatchHandle = setInterval(function() { + time -= 1; + var timer = document.getElementById("stopwatch"); + timer.textContent = time; + }, 1000); +}; + +var stopTimer = function() { + clearInterval(stopWatchHandle); +}; + \ No newline at end of file diff --git a/morning_exercise_resources/word_frequency/wordFreqSolution1.js b/morning_exercise_resources/word_frequency/wordFreqSolution1.js new file mode 100644 index 0000000..ba33c3b --- /dev/null +++ b/morning_exercise_resources/word_frequency/wordFreqSolution1.js @@ -0,0 +1,38 @@ +// FINDS FREQUENCIES OF WORDS IN A SENTENCE +wordFreq = function(string) { + + // split the sentence into individual words + var wordArray = string.split(' '); + + // set up default values for each unique word in an object + var words = {} + for (var i=0; i < wordArray.length; i++) { + wordArray[i] = wordArray[i].toLowerCase(); + words[wordArray[i]] = 0; + } + + // console.log(words); + + // increment values for each word by frequency + for (var j=0; j < wordArray.length; j++) { + words[wordArray[j]] += 1; + } + + return bigFreq(words); +} + +// RETURNS HIGHEST VALUE KEY-VALUE PAIR +bigFreq = function(obj) { + var value = -Infinity; + var word = null; + + for (var key in obj) { + if (obj[key] > value) { + value = obj[key] + word = key; + } + } + + return { [word] : value } +} + diff --git a/morning_exercise_resources/word_frequency/wordFreqSolution2.js b/morning_exercise_resources/word_frequency/wordFreqSolution2.js new file mode 100644 index 0000000..ae89a5e --- /dev/null +++ b/morning_exercise_resources/word_frequency/wordFreqSolution2.js @@ -0,0 +1,30 @@ +// DEREK'S + +var wordFreq = function(str) { + + var x = str.toLowerCase(); + words = x.split(" "); + words.sort(); + + var mostFrequent = function(arr) { + var uniqs = {}; + + for (var i=0; i < arr.length; i++) { + uniqs[arr[i]] = (uniqs[arr[i]] || 0) + 1; + } + + var max = { val: arr[0], count: 1 }; + + for (var u in uniqs) { + if (max.count < uniqs[u]) { + max = { val: u, count: uniqs[u] }; + } + } + + return max.val + ': ' + max.count; + } + + console.log(mostFrequent(words)); + +} + diff --git a/morning_exercise_resources/word_frequency/word_frequency.md b/morning_exercise_resources/word_frequency/word_frequency.md new file mode 100644 index 0000000..e0cc1d5 --- /dev/null +++ b/morning_exercise_resources/word_frequency/word_frequency.md @@ -0,0 +1,34 @@ +# WORD FREQUENCY + +### Find the most frequent word in a sentence + +Write a function or functions that takes in a sentence (string), and returns which word appears in the sentence with the highest frequency, along with the frequency of the word. + +Make it just for sentences without commas, apostrophes, and periods. Adjust for capital letters. + +If there is a tie between the two most frequent words, the first appearing one is returned. + +Example: + +``` +wordFreq("The world is all that is the case") +=> { the: 2 } +``` + +``` +wordFreq("That that is is that that is not is not") +=> { that: 4 } +``` + +``` +wordFreq("hi") +=> { hi: 1 } +``` + + + + + + + +