From f0396843204df7bf4490425597ec53a13dd7c122 Mon Sep 17 00:00:00 2001 From: Daniel Krom Date: Mon, 19 Nov 2018 17:05:50 +0200 Subject: [PATCH 1/2] Added Mul v2 question 5 --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 391a54b..58b2f9a 100644 --- a/README.md +++ b/README.md @@ -165,6 +165,33 @@ In Javascript function defined inside has access to outer function variable and - A function can be pass as a parameter to another function - A function can be returned from another function +### Mul V2 Question: Write function that works for all the next cases: + +```javascript +// notice the ` +console.log(`${mul(3)(2)(3)(12)(64)}`); +console.log(`${mul(3)(2)(3)(12)(64)(98)(12)}`); +console.log(`${mul(1)(2)(3)(4)(5)(6)(7)(8)(8)(10)(11)(12)(13)(14)(15)(16)(17)}`); +``` + +### Mul v2 Answer +In javascript, any object can override `toString` method, we can return a closure with `toString` method + +```javascript +function mul(x) { + let res = x; + const fn = function(n) { + res*=n; + return fn; + } + + fn.toString = function() { + return res.toString(); + } + return fn; +} +``` + ## Question 6. How to empty an array in JavaScript? For instance: From fe820f51ccca1e9eabaa33521c8fa229f88c5f1a Mon Sep 17 00:00:00 2001 From: Daniel Krom Date: Mon, 19 Nov 2018 17:11:23 +0200 Subject: [PATCH 2/2] Better explanation for 5.V2 --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 58b2f9a..f5419d5 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ In Javascript function defined inside has access to outer function variable and - A function can be pass as a parameter to another function - A function can be returned from another function -### Mul V2 Question: Write function that works for all the next cases: +### Mul V2 Question - Write function that works for all the next cases: ```javascript // notice the ` @@ -175,7 +175,8 @@ console.log(`${mul(1)(2)(3)(4)(5)(6)(7)(8)(8)(10)(11)(12)(13)(14)(15)(16)(17)}`) ``` ### Mul v2 Answer -In javascript, any object can override `toString` method, we can return a closure with `toString` method +In javascript, any object can override `toString` method. +We can return a closure with custom `toString` method and `${obj}` will call `obj.toString()` ```javascript function mul(x) {