Skip to content

Develop #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions closure/closure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Immidiately Call the blab function
let str = "ABCD";

function nonsense(string) {
let blab = (function () {
alert(string);
})();
}

nonsense(str);




// Call blab using setTimeout

let str = "ABCD";

function nonsense(string) {
let blab = function () {
alert(string);
};

window.setTimeout(blab, 2000);
}

nonsense(str);

// Return blab

let str = "ABCD";
let str2 = "AltCampus";

function nonsense(string) {
let blab = function () {
alert(string);
};

return blab;
}
let blabLater = nonsense(str);
let blabAgainLater = nonsense(str2);

// Display firstName and lastName both

var lastNameTrier = function(firstName){

var innerFunction = function(lastName) {
console.log(firstName +" "+lastName);
};
return innerFunction;
};

var firstNameFarmer = lastNameTrier('Farmer'); //logs nothing
firstNameFarmer('Brown'); //logs 'Farmer Brown'




// Write or erase a story

function storyWriter() {
let obj = {

story : "",

addWords : function(words) {
obj.story += words;
return obj.story;
},

erase : function() {
obj.story = "";
return obj.story;
}
}

return obj;
}


var farmLoveStory = storyWriter();
farmLoveStory.addWords('There was once a lonely cow.'); // 'There was once a lonely cow.'
farmLoveStory.addWords('It saw a friendly face.'); //'There was once a lonely cow. It saw a friendly face.'

var storyOfMyLife = storyWriter();
storyOfMyLife.addWords('My code broke.'); // 'My code broke.'
storyOfMyLife.addWords('I ate some ice cream.'); //'My code broke. I ate some ice cream.'
storyOfMyLife.erase(); // ''
15 changes: 10 additions & 5 deletions closure/closure.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
var blab = function(){
alert(string);
};
```
```

1. In your function, `nonsense`, change the immediate call to a setTimeout so that the call to `blab` comes after 2 seconds. The `blab` function itself should stay the same as before.

Expand All @@ -16,7 +16,9 @@


1. Write a function with a closure. The first function should only take one argument, someone's first name, and the inner function should take one more argument, someone's last name. The inner function should console.log both the first name and the last name.
```javascript

```javascript

var lastNameTrier = function(firstName){
//does stuff

Expand All @@ -27,7 +29,9 @@
};
var firstNameFarmer = lastNameTrier('Farmer'); //logs nothing
firstNameFarmer('Brown'); //logs 'Farmer Brown'
```

```

This function is useful in case you want to try on different last names. For example, I could use firstName again with another last name:

```javascript
Expand All @@ -37,7 +41,8 @@


1. Create a `storyWriter` function that returns an object with two methods. One method, `addWords` adds a word to your story and returns the story while the other one, `erase`, resets the story back to an empty string. Here is an implementation:
```javascript
```javascript

var farmLoveStory = storyWriter();
farmLoveStory.addWords('There was once a lonely cow.'); // 'There was once a lonely cow.'
farmLoveStory.addWords('It saw a friendly face.'); //'There was once a lonely cow. It saw a friendly face.'
Expand All @@ -47,4 +52,4 @@
storyOfMyLife.addWords('I ate some ice cream.'); //'My code broke. I ate some ice cream.'
storyOfMyLife.erase(); // ''

```
```
22 changes: 22 additions & 0 deletions higher-order-functions/eloquent/exercise.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@
let arrays = [[1, 2, 3], [4, 5], [6]];

// Your code here.

function flatArray(array) {
return array.reduce( (acc, cv) => {
acc = acc.concat(cv);
return acc;
},[] );
}

// → [1, 2, 3, 4, 5, 6]

// Challenge 2. Your own loop
// Your code here.
function loop(limit, conditionCheck, decrement, display) {

while(conditionCheck(limit)) {
display(limit);
limit = decrement(limit);
}

}

loop(3, n => n > 0, n => n - 1, console.log);
// → 3
Expand All @@ -15,6 +31,12 @@ loop(3, n => n > 0, n => n - 1, console.log);
// Challenge 3. Everything
function every(array, test) {
// Your code here.
for(let i = 0; i < array.length; i++) {
if( !test(array[i]) ) {
return false;
}
}
return true;
}

console.log(every([1, 3, 5], n => n < 10));
Expand Down
149 changes: 135 additions & 14 deletions higher-order-functions/exercise.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,57 @@
// Challenge 1
function addTwo(num) {}
function addTwo(num) {
return num + 2;
}

// To check if you've completed it, uncomment these console.logs!
// console.log(addTwo(3));
// console.log(addTwo(10));
console.log(addTwo(3));
console.log(addTwo(10));

// Challenge 2
function addS(word) {}
function addS(word) {
return word + 's';
}

// uncomment these to check your work
// console.log(addS('pizza'));
// console.log(addS('bagel'));

// Challenge 3
function map(array, callback) {}
function map(array, callback) {
let index = 0;
let newArray = [];
while(newArray.length < array.length) {
newArray.push(callback(array[index], index, array));
index++;
}
return newArray;
}

function addTwo(num) {
return num + 2;
}


// console.log(map([1, 2, 3], addTwo));

// Challenge 4
function forEach(array, callback) {}

let alphabet = "";
function forEach(array, callback) {
let index = 0;

while(index < array.length) {
callback(array[index],index, array);
index++;
}

}

var letters = ['a', 'b', 'c', 'd'];

forEach(letters,(char) => alphabet += char);

console.log(alphabet)

// see for yourself if your forEach works!

Expand All @@ -27,31 +60,119 @@ function forEach(array, callback) {}
//--------------------------------------------------

//Extension 1
function mapWith(array, callback) {}
function mapWith(array, callback) {
let newArray = [];
array.forEach(el => newArray.push( callback(el) ));
return newArray;
}

function multiplyByTwo(el) {
return el * 2;
}

var arr = [1, 2, 3, 4, 5];


console.log(mapWith(arr,multiplyByTwo));

//Extension 2
function reduce(array, callback, initialValue) {}
function reduce(array, callback, initialValue = array[0]) {
let ans;
for(let i = 0; i < array.length; i++) {
initialValue = callback(initialValue, array[i], i, array);
}
ans = initialValue;
return ans;
}

function add(acc, cv) {
return acc+cv;
}


let array = [1, 2, 3, 4];

console.log(reduce(array, add, 0));

//Extension 3
function intersection(arrays) {}

//Extension 3
function intersection(arrays) {
let ans;
let arr = arrays.flat();
ans = arr.reduce((acc, cv, index) => {

if(arrays.every(el => el.includes(cv)) && !acc.includes(cv) ) {
acc.push(cv);
}
return acc;
}, []);
return ans;
}
// console.log(intersection([5, 10, 15, 20], [15, 88, 1, 5, 7], [1, 10, 15, 5, 20]));
// should log: [5, 15]

//Extension 4
function union(arrays) {}

// console.log(union([5, 10, 15], [15, 88, 1, 5, 7], [100, 15, 10, 1, 5]));
function union(arrays) {
let ans;
let arr = arrays.flat();
ans = arr.reduce((acc, cv, index) => {

if(!acc.includes(cv)) {
acc.push(cv);
}
return acc;
}, []);
return ans;
}

// should log: [5, 10, 15, 88, 1, 7, 100]

//Extension 5
function objOfMatches(array1, array2, callback) {}

function objOfMatches(array1, array2, callback) {

let obj = callback(array1, array2);
return obj;
}

function getObj(array1, array2) {


return array1.reduce( (acc, cv) => {

for(let i = 0; i < array2.length; i++) {

if(cv.toUpperCase() == array2[i]) {
acc[cv] = array2[i];
break;
}

}

return acc;
},{});

}

let array1 = ['hi', 'howdy', 'bye', 'later', 'hello'];
let array2 = ['HI', 'Howdy', 'BYE', 'LATER', 'hello'];

console.log(objOfMatches(array1, array2, getObj));


// console.log(objOfMatches(['hi', 'howdy', 'bye', 'later', 'hello'], ['HI', 'Howdy', 'BYE', 'LATER', 'hello'], function(str) { return str.toUpperCase(); }));
// should log: { hi: 'HI', bye: 'BYE', later: 'LATER' }

//Extension 6
function multiMap(arrVals, arrCallbacks) {}
function multiMap(arrVals, arrCallbacks) {
let obj = arrVals.reduce( (acc, cv) => {
acc[cv] = arrCallbacks.map(el=> el(cv))
return acc;
},{})

return obj
}

// console.log(multiMap(['catfood', 'glue', 'beer'], [function(str) { return str.toUpperCase(); }, function(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }, function(str) { return str + str; }]));
// should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }
Loading