Skip to content

My code #7

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 2 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
7 changes: 6 additions & 1 deletion Exercises/1-callback.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
'use strict';

const iterate = (obj, callback) => null;
const iterate = (object, callback) => {
for (const key in object) {
if (Object.prototype.hasOwnProperty.call(object, key))
callback(key, object[key], object);
}
};

module.exports = { iterate };
2 changes: 1 addition & 1 deletion Exercises/2-closure.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

const store = x => null;
const store = x => () => x;

module.exports = { store };
16 changes: 15 additions & 1 deletion Exercises/3-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
'use strict';

const contract = (fn, ...types) => null;
const contract = (fn, ...types) => (...args) => {
for (const index in args) {
const arg = args[index];
if (arg !== types[index](arg)) throw new TypeError(
`type of argument '${arg}': '${typeof arg}'
does not match '${types[index]}'!`);
}
const res = fn(...args);
if (res !== types[types.length - 1](res))
throw new TypeError(
`type of result '${res}': '${typeof res}'
does not match '${types[types.length - 1]}'!`
);
return res;
};

module.exports = { contract };