From 192f88fb64396a6cc93bf831532d2777d3646b51 Mon Sep 17 00:00:00 2001 From: cfissell50 Date: Thu, 2 Nov 2017 11:57:41 -0500 Subject: [PATCH 1/5] almost f'd my branch up again --- 04week/loop.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/04week/loop.js b/04week/loop.js index e69de29bb..c6deee023 100644 --- a/04week/loop.js +++ b/04week/loop.js @@ -0,0 +1,61 @@ +'use strict'; + +// Create a new file called loops.js in the /04week folder of your workbook. +// Complete each of the following exercises. +// for loop + +// Use a for loop to console.log each item in the array carsInReverse. +const carsInReverse = ['Ford', 'Chevy', 'Tesla', 'Nissan', 'Toyota']; +for (i = 0; i < carsInReverse.length; i++) { + console.log(carsInReverse[i]); +} + +// Create an object (an array with keys and values) called persons with the following data: +// firstName: "Jane" +// lastName: "Doe" +// birthDate: "Jan 5, 1925" +// gender: "female" +const persons = { + firstName: "Jane", + lastName: "Doe", + birthDate: "Jan 5, 1925", + gender: "female" +}; + +// Use a for...in loop to console.log each key. +for (const keys in persons) { + console.log(Object.keys(persons)); +} + +// Then use a for...in loop and if state to console.log the value associated with the key birthDate. +for (const keys in persons) { + if (keys === 'birthDate') { + console.log(persons[keys]); + } +} + +// Use a while loop to console log numbers 1 to 1000 +let num = 1; +while(num <= 1000) { + console.log(num); + num++; +} + +// Use a do...while loop to console.log the numbers from 1 to 1000. +do { + console.log(num); + num++; +} +while(num <= 1000); + +// When is a for loop better than a while loop? + // A forLoop is better when you know the amount of loops you need to do. When you know the cap. + +// How is the readability of the code affected? + // A while loop can break easier because of the open ended autonomy. The for loop has a set limit which can be easier to control. + +// What is the difference between a for loop and a for...in loop? + // for in loops are used mostly for objects + +// What is the difference between a while loop and a do...while loop? + // A while loop goes until something is the argument is false and a do while goes while the condition is true. From 5c770291ace07b6d4f13af36779d63dac0a34876 Mon Sep 17 00:00:00 2001 From: cfissell50 Date: Wed, 8 Nov 2017 12:25:19 -0600 Subject: [PATCH 2/5] map funkadelic --- 06week/higherOrder.js | 20 +++++++++++++++++++- package-lock.json | 30 +++++++++++++++--------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 73926e3dc..78d23813d 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -2,14 +2,32 @@ const assert = require('assert'); +// Create a forEach() function that takes an array of items and a function that runs the function arr.length number of times. +// +// Create a map() function that takes an array of items and a function that returns an array with each item manipulated by that function. +// +// Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. + function forEach(arr, callback) { // Your code here } +// map consists of an array, a function, and a return array. + function map(arr, callback) { - // Your code here + const newArr = []; + arr.forEach((item) => { + newArr.push(callback(item)); + }) + return newArr; } +const numArray = [2, 4, 6, 8]; + +map(numArray, (item)=> item + 2); + +// + function filter(arr, callback) { // Your code here } diff --git a/package-lock.json b/package-lock.json index 62353c45d..f28cfa5c3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,16 +4,6 @@ "lockfileVersion": 1, "requires": true, "dependencies": { - "JSONStream": { - "version": "0.8.4", - "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-0.8.4.tgz", - "integrity": "sha1-kWV9/m/4V0gwZhMrRhi2Lo9Ih70=", - "dev": true, - "requires": { - "jsonparse": "0.0.5", - "through": "2.3.8" - } - }, "abab": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/abab/-/abab-1.0.4.tgz", @@ -2707,6 +2697,16 @@ "integrity": "sha1-T9kss04OnbPInIYi7PUfm5eMbLk=", "dev": true }, + "JSONStream": { + "version": "0.8.4", + "resolved": "https://registry.npmjs.org/JSONStream/-/JSONStream-0.8.4.tgz", + "integrity": "sha1-kWV9/m/4V0gwZhMrRhi2Lo9Ih70=", + "dev": true, + "requires": { + "jsonparse": "0.0.5", + "through": "2.3.8" + } + }, "jsprim": { "version": "1.4.1", "resolved": "https://registry.npmjs.org/jsprim/-/jsprim-1.4.1.tgz", @@ -4367,6 +4367,11 @@ "through": "2.3.8" } }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", + "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" + }, "string-to-stream": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/string-to-stream/-/string-to-stream-1.1.0.tgz", @@ -4427,11 +4432,6 @@ } } }, - "string_decoder": { - "version": "0.10.31", - "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", - "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=" - }, "stringstream": { "version": "0.0.5", "resolved": "https://registry.npmjs.org/stringstream/-/stringstream-0.0.5.tgz", From 82af18c8139e12c25723c86a0bd677dcf15196e9 Mon Sep 17 00:00:00 2001 From: cfissell50 Date: Thu, 9 Nov 2017 14:06:39 -0600 Subject: [PATCH 3/5] gotcha foreach --- 06week/higherOrder.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 78d23813d..47fba0f05 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -8,10 +8,16 @@ const assert = require('assert'); // // Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. -function forEach(arr, callback) { - // Your code here +// a functon that takes an array and a function that runs the function arr.length +const numArray = [2, 4, 6, 8]; +function forEach(arr) { + for (i = 0; i <= arr.length; i ++){ + console.log(arr[i] + 2); +} } +forEach(numArray); + // map consists of an array, a function, and a return array. function map(arr, callback) { @@ -22,8 +28,6 @@ function map(arr, callback) { return newArr; } -const numArray = [2, 4, 6, 8]; - map(numArray, (item)=> item + 2); // From 6791b0de35852ab5590a53c6c7cfb14409abcf44 Mon Sep 17 00:00:00 2001 From: cfissell50 Date: Thu, 9 Nov 2017 18:19:56 -0600 Subject: [PATCH 4/5] filter --- 06week/higherOrder.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 47fba0f05..9e6d5ee4f 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -9,11 +9,13 @@ const assert = require('assert'); // Create a filter() function that takes an array of items and a function that returns an array with only the items that return true in the function. // a functon that takes an array and a function that runs the function arr.length + const numArray = [2, 4, 6, 8]; + function forEach(arr) { for (i = 0; i <= arr.length; i ++){ - console.log(arr[i] + 2); -} + console.log(arr[i] + 2); + } } forEach(numArray); @@ -30,12 +32,23 @@ function map(arr, callback) { map(numArray, (item)=> item + 2); -// +const sortingFunction = (name) => { + if(name.length > 4) { + return name; + } +} -function filter(arr, callback) { - // Your code here +function filter( arr, callback) { + const newFilterArr = []; + arr.forEach((item) => { + newFilterArr.push(callback(item)); + }) + return newFilterArr; } +filter (numArray, sortingFunction); + + function some(arr, callback) { // Your code here } From cc0a84e93c1d4ae87ed7af76ac42f0e5a88c780b Mon Sep 17 00:00:00 2001 From: cfissell50 Date: Thu, 9 Nov 2017 18:37:25 -0600 Subject: [PATCH 5/5] donezo --- 06week/higherOrder.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/06week/higherOrder.js b/06week/higherOrder.js index 9e6d5ee4f..3e25fe612 100644 --- a/06week/higherOrder.js +++ b/06week/higherOrder.js @@ -32,21 +32,23 @@ function map(arr, callback) { map(numArray, (item)=> item + 2); -const sortingFunction = (name) => { - if(name.length > 4) { - return name; +const numTwoArray = [1,2,3,4,5,6,7,8] + +const sortingFunction = (num) => { + if(num > 4) { + return num; } } -function filter( arr, callback) { +function filter(arr, callback) { const newFilterArr = []; - arr.forEach((item) => { - newFilterArr.push(callback(item)); + arr.forEach((num) => { + newFilterArr.push(callback(num)); }) return newFilterArr; } -filter (numArray, sortingFunction); +filter (numTwoArray, sortingFunction); function some(arr, callback) {