- Implement function
iterate(object, callback)to iterate given object passing each element to callback with the following contractcallback(key, value, object). Example:
const obj = { a: 1, b: 2, c: 3 };
iterate(obj, (key, value) => {
console.log({ key, value });
});Output:
{ key: 'a', value: 1 }
{ key: 'b', value: 2 }
{ key: 'c', value: 3 }
- Implement function
store(value)to storevalueinside closure of returning function. After calling returning function it will return a value from closure, like in the following example:
const read = store(5);
const value = read();
console.log(value); // Output: 5- Implement function
contract(fn, ...types)to wrapfn(first argument) and check argument types (all arguments except first and last) and result type (last argument). GeneratesTypeErrorexception if wrong types detected. As in following example:
const add = (a, b) => a + b;
const addNumbers = contract(add, Number, Number, Number);
const res = addNumbers(2, 3);
console.dir(res); // Output: 5and
const concat = (s1, s2) => s1 + s2;
const concatStrings = contract(concat, String, String, String);
const res = concatStrings('Hello ', 'world!');
console.dir(res); // Output: Hello world!