Skip to content
Open
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
*.log
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# JavaScript Exercises
# JavaScript Exercises!!!

Complete the exercises in this repo and push your solutions up to a branch (`firstname-lastname`).

Expand Down
3 changes: 2 additions & 1 deletion exercise-4.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const reduce = (array, fn, init) => {
}

// recursive case
return reduce(array.slice(1), fn, fn(init, array[0]));
// return reduce(array.slice(1), fn, fn(init, array[0])); // my way
return reduce(array, fn, fn(init, array.shift())); // another way
};

const sum = (acc, val) => acc + val;
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@
"homepage": "https://github.com/bridge-school/js-exercises#readme",
"devDependencies": {
"jest": "^22.1.4"
},
"main": "exercise-1.js",
"dependencies": {
"es6-promise": "^4.2.4",
"node-fetch": "^2.0.0"
}
}
16 changes: 16 additions & 0 deletions promises/exercise-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require('es6-promise');

// Create a promise. Have it fulfilled with a value of 'FULFILLED!' in executor after a delay of 300ms, using setTimeout.
// Then, print the contents of the promise after it has been fulfilled by passing console.log to then.

const promise = new Promise(function(resolve, reject) {
setTimeout(() => {
resolve('FULFILLED!');
}, 300);
});

const onResolve = data => {
console.log(data);
};

promise.then(onResolve);
21 changes: 21 additions & 0 deletions promises/exercise-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require("es6-promise");

// Create a promise that after a delay of 300ms, rejects with an Error object. The Error object should be constructed with parameter 'REJECTED!', which is the textual message of the error.
// Create a function onReject to print error.message using console.log. Pass this function as a rejection handler to the then method of your promise.

const promise = new Promise(function(resolve, reject) {
// Your solution here
setTimeout(() => {
reject(new Error('REJECTED!'));
}, 1000);
});

const onReject = value => {
// Your solution here
console.log(value.message);

};

promise.catch(
onReject
);
10 changes: 10 additions & 0 deletions promises/exercise-3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const fetch = require('node-fetch');

// Make a fetch request to https://next.json-generator.com/api/json/get/EJPkuFBIV

fetch("https://next.json-generator.com/api/json/get/EJPkuFBIV")
.then((res) => res.json())
.then(response => {
response.map(item => console.log(item.first))
});

62 changes: 62 additions & 0 deletions promises/exercise-4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
require("es6-promise");

const listOfWines = [
{id: 'someId', name: 'Black Tower', type: 'White', num: 1},
{id: 'someOtherId', name: 'Merlot', type: 'Red', num: 2},
{id: 'somethingElse', name: 'Yellow Tail', type: 'Red', num: 3},
];
// const listOfWines = [];

/* 1. Create a promise. Have it fulfilled with the list of wines after a delay of 3000ms, using setTimeout.
2. Then, print the contents of the promise after it has been fulfilled by passing console.log to then.
3. Create a failedPromise that rejects with the error message 'something is broken' after a delay of 3000ms, using setTimeout
4. Then, using the contents of the promise after it returns, console.log the error message 'Oh no, the request failed becasuse' and append the error message;

Extra challenge:
A. Update the onSuccess function so that takes the returned wines, filters the list to only include the red wines and then print that list
B. Refactor your code so that instead of seperate named callback functions 'onSuccess' and 'onReject', you write the functions inline */


const myPromise = new Promise((resolve, reject) => {
// Your solution for #1 here
if (listOfWines.length === 0) {
reject('something is broken')
} else {
setTimeout(() => {
resolve(listOfWines);
}, 3000);
}
});

const onSuccess = value => {
// Your solution here
const newlist = value.filter(thing => thing.type == 'Red');
console.log(newlist);
};

// Use myPromise here with onSuccess
myPromise
.then((data) => {
console.log(data.filter(thing => thing.type == 'Red'));
}).catch((err) => {
console.log('Oh no, the request failed becasuse', err)
});



// const myFailedPromise = new Promise((resolve, reject) => {
// // Your solution for #3 here
// setTimeout(() => {
// reject('something is broken');
// }, 3000);
// });

// const onReject = value => {
// // Your solution here
// console.log('Oh no, the request failed becasuse', value)
// };

// use myFailedPromise here with onReject
// myFailedPromise.catch((err) => {
// onReject(err);
// });