From 5c65a934e9665e091d52a02a53038a2a2ad245bb Mon Sep 17 00:00:00 2001 From: MoverLee Date: Wed, 18 Jul 2018 23:04:32 +0800 Subject: [PATCH 01/24] Init --- .DS_Store | Bin 0 -> 8196 bytes 08-coding-interview-prep/algorithms.json | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..266076d47015893c518dda68e8a41e9ef3ccc346 GIT binary patch literal 8196 zcmeHM-A)rh6g~r}Ta3Vk3f}FFi5D#?KWd3FsTD4`FfrEXg&Npx7Z|eLt=%mR)SBM- z4)_SZiErS8_yGEynW3}`#7IIgGLy`l`8(g4`OeI4&z6WprCnbjnj<0yndQt)6sIH} z=X@fKnVu;~0ezx?5^7RR5u{C)0n318z%pPNunhc*4B(y3$vNY_uSacZ8L$joNe1}& z;3Bgu8`;-VJvvaR5CC!l-72AuI6&#RMwX52YpGO$siOyBRD}sKgjUD6%hMstM)tMT z>Lj!}31eB92t_FC&~w#v5@juIX&JB#oMeDw_c&c@8`=W&#JG74HsS=IwgdH+^M^|(`s+fhAH zI8{C1I5{V`F*DokE|yApce&WhyWPhti+Q)SRO8*B~x?=I{vL30bU`CATK3k+@u`#$O&j`nRZJ^D}#OMtm*+T7 z7J&E&I1+o?QcO0ESA%{=t0~STIM^Su_;IlQqy9wgiFt|G_UK;J4LQu*kP`4}uv%C4 zx6L}z!43l4yaM*h9&2Xs*ki+Z)7Z(1!sGp?K-^Ls@EJJWM}MLHpWe`(#TYc0gg3s$Pt5Y};67!lk=lmSqMVoKaJl8V1d@_f1(X`1jaKQ|WYy3Hm z|JTm`{(r%%u<(`v%fMwaASPC;s}-!^=*Mh!F&t~V$eYNVcwS#irGi3*z>% literal 0 HcmV?d00001 diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 7f1dcb8..65fa866 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,6 +8,7 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ + "前要:对称差,", "Create a function that takes two or more arrays and returns an array of the symmetric difference ( or ) of the provided arrays.", "Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term \"symmetric difference\" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}). The resulting array must contain only unique values (no duplicates).", "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." @@ -584,4 +585,4 @@ } } ] -} \ No newline at end of file +} From ef29b827f24d136d50cbbc793f1a16ad4a2d55f4 Mon Sep 17 00:00:00 2001 From: MoverLee Date: Sun, 22 Jul 2018 11:39:21 +0800 Subject: [PATCH 02/24] Finished the Symmetric Difference --- 08-coding-interview-prep/algorithms.json | 64 ++++++++++++------------ 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 65fa866..83fb8fb 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,70 +8,70 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ - "前要:对称差,", - "Create a function that takes two or more arrays and returns an array of the symmetric difference ( or ) of the provided arrays.", - "Given two sets (for example set A = {1, 2, 3} and set B = {2, 3, 4}), the mathematical term \"symmetric difference\" of two sets is the set of elements which are in either of the two sets, but not in both (A △ B = C = {1, 4}). For every additional symmetric difference you take (say on a set D = {2, 3}), you should get the set with elements which are in either of the two the sets but not both (C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}). The resulting array must contain only unique values (no duplicates).", - "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." + "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。百科。", + "创建一个函数sym,输入两个或两个以上的数组作为参数,然后返回值为 **对称差** 的数组", + "思路:设定两个数组 (例如:A = {1, 2, 3}B = {2, 3, 4},)作为参数传入,返回对称差数组(A △ B = C = {1, 4}),且数组元素具有唯一性(没有重复项)。", + "如果您遇到了难题,请点击 帮助。请尽量编写属于您自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" ], "tests": [ { - "text": "sym([1, 2, 3], [5, 2, 1, 4]) should return [3, 4, 5].", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5], 'sym([1, 2, 3], [5, 2, 1, 4]) should return [3, 4, 5].');" + "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该是[3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5], 'sym([1, 2, 3], [5, 2, 1, 4])的返回值应该是[3, 4, 5]。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4]) should contain only three elements.", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3, 'sym([1, 2, 3], [5, 2, 1, 4]) should contain only three elements.');" + "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3, 'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" }, { - "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) should return [3, 4, 5].", - "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5], 'sym([1, 2, 3, 3], [5, 2, 1, 4]) should return [3, 4, 5].');" + "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5], 'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。');" }, { - "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) should contain only three elements.", - "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3, 'sym([1, 2, 3, 3], [5, 2, 1, 4]) should contain only three elements.');" + "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3, 'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) should return [3, 4, 5].", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5], 'sym([1, 2, 3], [5, 2, 1, 4, 5]) should return [3, 4, 5].');" + "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5], 'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) should contain only three elements.", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3, 'sym([1, 2, 3], [5, 2, 1, 4, 5]) should contain only three elements.');" + "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3, 'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。');" }, { - "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should return [1, 4, 5]", - "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], 'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should return [1, 4, 5]');" + "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], 'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。');" }, { - "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should contain only three elements.", - "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3, 'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) should contain only three elements.');" + "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3, 'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。');" }, { - "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should return [1, 4, 5].", - "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should return [1, 4, 5].');" + "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。');" }, { - "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should contain only three elements.", - "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3, 'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should contain only three elements.');" + "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。", + "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3, 'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) should return [2, 3, 4, 6, 7].", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7], 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) should return [2, 3, 4, 6, 7].');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。", + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7], 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) should contain only five elements.", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5, 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) should contain only five elements.');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。", + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5, 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) should return [1, 2, 4, 5, 6, 7, 8, 9].", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9], 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) should return [1, 2, 4, 5, 6, 7, 8, 9].');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。", + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9], 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) should contain only eight elements.", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8, 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) should contain only eight elements.');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。", + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8, 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。');" } ], "MDNlinks": [ From 53201c0ad7b5a25240873dc783b8c681ae26e7c0 Mon Sep 17 00:00:00 2001 From: MoverLee Date: Sun, 22 Jul 2018 11:43:59 +0800 Subject: [PATCH 03/24] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=BA=E7=A7=B0?= =?UTF-8?q?=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 08-coding-interview-prep/algorithms.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 83fb8fb..ed4593f 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -11,7 +11,7 @@ "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。百科。", "创建一个函数sym,输入两个或两个以上的数组作为参数,然后返回值为 **对称差** 的数组", "思路:设定两个数组 (例如:A = {1, 2, 3}B = {2, 3, 4},)作为参数传入,返回对称差数组(A △ B = C = {1, 4}),且数组元素具有唯一性(没有重复项)。", - "如果您遇到了难题,请点击 帮助。请尽量编写属于您自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", + "如果你遇到了难题,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" From cc924eaeed3be19ff483f39ac7d442c9bdb41bb2 Mon Sep 17 00:00:00 2001 From: MoverLee Date: Sun, 22 Jul 2018 13:31:07 +0800 Subject: [PATCH 04/24] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 08-coding-interview-prep/algorithms.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index ed4593f..decee34 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -11,7 +11,7 @@ "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。百科。", "创建一个函数sym,输入两个或两个以上的数组作为参数,然后返回值为 **对称差** 的数组", "思路:设定两个数组 (例如:A = {1, 2, 3}B = {2, 3, 4},)作为参数传入,返回对称差数组(A △ B = C = {1, 4}),且数组元素具有唯一性(没有重复项)。", - "如果你遇到了难题,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", + "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" @@ -101,7 +101,7 @@ "title": "Inventory Update", "description": [ "Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.", - "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." + "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", ], "solutions": [ "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // All inventory must be accounted for or you're fired!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// Example inventory lists\nvar curInv = [\n [21, 'Bowling Ball'],\n [2, 'Dirty Sock'],\n [1, 'Hair Pin'],\n [5, 'Microphone']\n];\n\nvar newInv = [\n [2, 'Hair Pin'],\n [3, 'Half-Eaten Apple'],\n [67, 'Bowling Ball'],\n [7, 'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" From ddb98749e54b3217a8422dd3b2bdaf5743d7af9a Mon Sep 17 00:00:00 2001 From: MoverLee Date: Sun, 22 Jul 2018 16:50:53 +0800 Subject: [PATCH 05/24] Finished Inventory Update --- 08-coding-interview-prep/algorithms.json | 27 ++++++++++++------------ 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index decee34..ca232b7 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -100,7 +100,8 @@ "id": "a56138aff60341a09ed6c480", "title": "Inventory Update", "description": [ - "Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in arr1). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.", + "", + "比较和更新存储在 2D数组 中的库存,并将其与新交付的第二个2D数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", ], "solutions": [ @@ -108,28 +109,28 @@ ], "tests": [ { - "text": "The function updateInventory should return an array.", - "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), 'The function updateInventory should return an array.');" + "text": "函数 updateInventory 的返回值应该是一个数组。", + "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), '函数 updateInventory 的返回值应该是一个数组。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) should return an array with a length of 6.", - "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6, 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) should return an array with a length of 6.');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。", + "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6, 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) should return [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]], 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) should return [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]], 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) should return [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) should return [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" }, { - "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) should return [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].", - "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]], 'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) should return [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" + "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]], 'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" }, { - "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) should return [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].", - "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]], 'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) should return [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" + "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]], 'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" } ], "MDNlinks": [ From 6228f6ec696be3cf7dd82e7bb35e440900048412 Mon Sep 17 00:00:00 2001 From: MoverLee Date: Sun, 22 Jul 2018 18:19:06 +0800 Subject: [PATCH 06/24] test --- 08-coding-interview-prep/algorithms.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index ca232b7..30a9b8e 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -174,9 +174,10 @@ "id": "a7bf700cd123b9a54eef01d5", "title": "No Repeats Please", "description": [ - "Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique.", + "把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数。连续重复只以单个字符为准。", + "例如:", "For example, aab should return 2 because it has 6 total permutations (aab, aab, aba, aba, baa, baa), but only 2 of them (aba and aba) don't have the same letter (in this case a) repeating.", - "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." + "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。" ], "solutions": [ "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: Global array which holds the list of permutations\n //usedChars: Global utility array which holds a list of \"currently-in-use\" characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\" from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" From 7a878ebb3612432e8ebd2b2a258c357d5076f07e Mon Sep 17 00:00:00 2001 From: MoverLee Date: Sun, 22 Jul 2018 23:11:24 +0800 Subject: [PATCH 07/24] Undone --- 08-coding-interview-prep/algorithms.json | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 30a9b8e..b2d84c3 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -105,7 +105,7 @@ "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", ], "solutions": [ - "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // All inventory must be accounted for or you're fired!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// Example inventory lists\nvar curInv = [\n [21, 'Bowling Ball'],\n [2, 'Dirty Sock'],\n [1, 'Hair Pin'],\n [5, 'Microphone']\n];\n\nvar newInv = [\n [2, 'Hair Pin'],\n [3, 'Half-Eaten Apple'],\n [67, 'Bowling Ball'],\n [7, 'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" + "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21, 'Bowling Ball'],\n [2, 'Dirty Sock'],\n [1, 'Hair Pin'],\n [5, 'Microphone']\n];\n\nvar newInv = [\n [2, 'Hair Pin'],\n [3, 'Half-Eaten Apple'],\n [67, 'Bowling Ball'],\n [7, 'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" ], "tests": [ { @@ -175,12 +175,11 @@ "title": "No Repeats Please", "description": [ "把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数。连续重复只以单个字符为准。", - "例如:", - "For example, aab should return 2 because it has 6 total permutations (aab, aab, aba, aba, baa, baa), but only 2 of them (aba and aba) don't have the same letter (in this case a) repeating.", + "例如:aab 应该返回 2 , 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。" ], "solutions": [ - "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: Global array which holds the list of permutations\n //usedChars: Global utility array which holds a list of \"currently-in-use\" characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\" from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" + "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表\n //usedChars: Global utility array which holds a list of \"currently-in-use\" characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\" from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" ], "tests": [ { From d8c8d1f10bc9dbe57eb3f64da3bb3b86ceded430 Mon Sep 17 00:00:00 2001 From: MoverLee Date: Mon, 23 Jul 2018 09:26:11 +0800 Subject: [PATCH 08/24] Format by pangu --- 08-coding-interview-prep/algorithms.json | 210 +++++++++++------------ 1 file changed, 105 insertions(+), 105 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index b2d84c3..dd2c0f9 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,70 +8,70 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ - "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。百科。", - "创建一个函数sym,输入两个或两个以上的数组作为参数,然后返回值为 **对称差** 的数组", - "思路:设定两个数组 (例如:A = {1, 2, 3}B = {2, 3, 4},)作为参数传入,返回对称差数组(A △ B = C = {1, 4}),且数组元素具有唯一性(没有重复项)。", - "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", + "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。 百科 。", + "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为 ** 对称差 ** 的数组", + "思路:设定两个数组 (例如:A = {1, 2, 3}B = {2, 3, 4},) 作为参数传入,返回对称差数组(A △ B = C = {1, 4}),且数组元素具有唯一性(没有重复项)。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" ], "tests": [ { - "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该是[3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5], 'sym([1, 2, 3], [5, 2, 1, 4])的返回值应该是[3, 4, 5]。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该是 < code>[3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该是 < code>[3, 4, 5]。');" }, { "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3, 'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" }, { "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5], 'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。');" + "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。');" }, { "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3, 'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" + "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" }, { "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5], 'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。');" + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。');" }, { "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3, 'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。');" + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。');" }, { "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5], 'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。');" + "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5],'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。');" }, { "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3, 'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。');" + "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3,'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。');" }, { "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5], 'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。');" + "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5],'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。');" }, { "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3, 'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。');" + "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3,'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。');" }, { "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7], 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。');" + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。');" }, { "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5, 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。');" + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。');" }, { "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9], 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。');" + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。');" }, { "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8, 'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。');" + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。');" } ], "MDNlinks": [ @@ -86,7 +86,7 @@ "name": "index", "contents": [ "function sym(args) {", - " return args;", + "return args;", "}", "", "sym([1, 2, 3], [5, 2, 1, 4]);" @@ -101,36 +101,36 @@ "title": "Inventory Update", "description": [ "", - "比较和更新存储在 2D数组 中的库存,并将其与新交付的第二个2D数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", - "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。", + "比较和更新存储在 二维数组 中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", ], "solutions": [ - "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21, 'Bowling Ball'],\n [2, 'Dirty Sock'],\n [1, 'Hair Pin'],\n [5, 'Microphone']\n];\n\nvar newInv = [\n [2, 'Hair Pin'],\n [3, 'Half-Eaten Apple'],\n [67, 'Bowling Ball'],\n [7, 'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" + "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21,'Bowling Ball'],\n [2,'Dirty Sock'],\n [1,'Hair Pin'],\n [5,'Microphone']\n];\n\nvar newInv = [\n [2,'Hair Pin'],\n [3,'Half-Eaten Apple'],\n [67,'Bowling Ball'],\n [7,'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" ], "tests": [ { "text": "函数 updateInventory 的返回值应该是一个数组。", - "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), '函数 updateInventory 的返回值应该是一个数组。');" + "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]),'函数 updateInventory 的返回值应该是一个数组。');" }, { "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。", - "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6, 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。');" + "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。');" }, { "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]], 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" }, { "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], 'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" }, { "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]], 'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" + "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" }, { "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]], 'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" + "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]],'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" } ], "MDNlinks": [ @@ -144,23 +144,23 @@ "name": "index", "contents": [ "function updateInventory(arr1, arr2) {", - " // All inventory must be accounted for or you're fired!", - " return arr1;", + "// All inventory must be accounted for or you're fired!", + "return arr1;", "}", "", "// Example inventory lists", "var curInv = [", - " [21, \"Bowling Ball\"],", - " [2, \"Dirty Sock\"],", - " [1, \"Hair Pin\"],", - " [5, \"Microphone\"]", + "[21, \"Bowling Ball\"],", + "[2, \"Dirty Sock\"],", + "[1, \"Hair Pin\"],", + "[5, \"Microphone\"]", "];", "", "var newInv = [", - " [2, \"Hair Pin\"],", - " [3, \"Half-Eaten Apple\"],", - " [67, \"Bowling Ball\"],", - " [7, \"Toothpaste\"]", + "[2, \"Hair Pin\"],", + "[3, \"Half-Eaten Apple\"],", + "[67, \"Bowling Ball\"],", + "[7, \"Toothpaste\"]", "];", "", "updateInventory(curInv, newInv);" @@ -176,51 +176,51 @@ "description": [ "把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数。连续重复只以单个字符为准。", "例如:aab 应该返回 2 , 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", - "如果你遇到了困难,请点击 帮助。请尽量编写属于你自己的代码。尝试一下 **结对编程** 会有意料不到的惊喜哦。" + "如果你遇到了困难,请点击 帮助 。请尽量编写属于你自己的代码。尝试一下 ** 结对编程 ** 会有意料不到的惊喜哦。" ], "solutions": [ - "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表\n //usedChars: Global utility array which holds a list of \"currently-in-use\" characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\" from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" + "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: Global utility array which holds a list of \"currently-in-use\"characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\"from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" ], "tests": [ { "text": "permAlone(\"aab\") should return a number.", - "testString": "assert.isNumber(permAlone('aab'), 'permAlone(\"aab\") should return a number.');" + "testString": "assert.isNumber(permAlone('aab'),'permAlone(\"aab\") should return a number.');" }, { "text": "permAlone(\"aab\") should return 2.", - "testString": "assert.strictEqual(permAlone('aab'), 2, 'permAlone(\"aab\") should return 2.');" + "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\") should return 2.');" }, { "text": "permAlone(\"aaa\") should return 0.", - "testString": "assert.strictEqual(permAlone('aaa'), 0, 'permAlone(\"aaa\") should return 0.');" + "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\") should return 0.');" }, { "text": "permAlone(\"aabb\") should return 8.", - "testString": "assert.strictEqual(permAlone('aabb'), 8, 'permAlone(\"aabb\") should return 8.');" + "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\") should return 8.');" }, { "text": "permAlone(\"abcdefa\") should return 3600.", - "testString": "assert.strictEqual(permAlone('abcdefa'), 3600, 'permAlone(\"abcdefa\") should return 3600.');" + "testString": "assert.strictEqual(permAlone('abcdefa'), 3600,'permAlone(\"abcdefa\") should return 3600.');" }, { "text": "permAlone(\"abfdefa\") should return 2640.", - "testString": "assert.strictEqual(permAlone('abfdefa'), 2640, 'permAlone(\"abfdefa\") should return 2640.');" + "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\") should return 2640.');" }, { "text": "permAlone(\"zzzzzzzz\") should return 0.", - "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0, 'permAlone(\"zzzzzzzz\") should return 0.');" + "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\") should return 0.');" }, { "text": "permAlone(\"a\") should return 1.", - "testString": "assert.strictEqual(permAlone('a'), 1, 'permAlone(\"a\") should return 1.');" + "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\") should return 1.');" }, { "text": "permAlone(\"aaab\") should return 0.", - "testString": "assert.strictEqual(permAlone('aaab'), 0, 'permAlone(\"aaab\") should return 0.');" + "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\") should return 0.');" }, { "text": "permAlone(\"aaabb\") should return 12.", - "testString": "assert.strictEqual(permAlone('aaabb'), 12, 'permAlone(\"aaabb\") should return 12.');" + "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\") should return 12.');" } ], "MDNlinks": [ @@ -235,7 +235,7 @@ "name": "index", "contents": [ "function permAlone(str) {", - " return str;", + "return str;", "}", "", "permAlone('aab');" @@ -255,7 +255,7 @@ "
Index01234
Value79111315
", "Below we'll take their corresponding indices and add them.", "7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6", - "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." + "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." ], "solutions": [ "function pairwise(arr, arg) {\n var sum = 0;\n arr.forEach(function(e, i, a) {\n if (e != null) { \n var diff = arg-e;\n a[i] = null;\n var dix = a.indexOf(diff);\n if (dix !== -1) {\n sum += dix;\n sum += i;\n a[dix] = null;\n } \n }\n });\n return sum;\n}\n\npairwise([1,4,2,3,0,5], 7);\n" @@ -263,23 +263,23 @@ "tests": [ { "text": "pairwise([1, 4, 2, 3, 0, 5], 7) should return 11.", - "testString": "assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11, 'pairwise([1, 4, 2, 3, 0, 5], 7) should return 11.');" + "testString": "assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11,'pairwise([1, 4, 2, 3, 0, 5], 7) should return 11.');" }, { "text": "pairwise([1, 3, 2, 4], 4) should return 1.", - "testString": "assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1, 'pairwise([1, 3, 2, 4], 4) should return 1.');" + "testString": "assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1,'pairwise([1, 3, 2, 4], 4) should return 1.');" }, { "text": "pairwise([1, 1, 1], 2) should return 1.", - "testString": "assert.deepEqual(pairwise([1, 1, 1], 2), 1, 'pairwise([1, 1, 1], 2) should return 1.');" + "testString": "assert.deepEqual(pairwise([1, 1, 1], 2), 1,'pairwise([1, 1, 1], 2) should return 1.');" }, { "text": "pairwise([0, 0, 0, 0, 1, 1], 1) should return 10.", - "testString": "assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10, 'pairwise([0, 0, 0, 0, 1, 1], 1) should return 10.');" + "testString": "assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10,'pairwise([0, 0, 0, 0, 1, 1], 1) should return 10.');" }, { "text": "pairwise([], 100) should return 0.", - "testString": "assert.deepEqual(pairwise([], 100), 0, 'pairwise([], 100) should return 0.');" + "testString": "assert.deepEqual(pairwise([], 100), 0,'pairwise([], 100) should return 0.');" } ], "MDNlinks": [ @@ -293,7 +293,7 @@ "name": "index", "contents": [ "function pairwise(arr, arg) {", - " return arg;", + "return arg;", "}", "", "pairwise([1,4,2,3,0,5], 7);" @@ -308,7 +308,7 @@ "title": "Implement Bubble Sort", "description": [ "This is the first of several challenges on sorting algorithms. Given an array of unsorted items, we want to be able to return a sorted array. We will see several different methods to do this and learn some tradeoffs between these different approaches. While most modern languages have built-in sorting methods for operations like this, it is still important to understand some of the common basic approaches and learn how they can be implemented.", - "Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and 'bubbles up' unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.", + "Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and'bubbles up'unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.", "This method requires multiple iterations through the array and for average and worst cases has quadratic time complexity. While simple, it is usually impractical in most situations.", "Instructions: Write a function bubbleSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.", "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting algorithm in action!" @@ -316,19 +316,19 @@ "tests": [ { "text": "bubbleSort is a function.", - "testString": "assert(typeof bubbleSort == 'function', 'bubbleSort is a function.');" + "testString": "assert(typeof bubbleSort =='function','bubbleSort is a function.');" }, { "text": "bubbleSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'bubbleSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'bubbleSort returns a sorted array (least to greatest).');" }, { "text": "bubbleSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'bubbleSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'bubbleSort returns an array that is unchanged except for order.');" }, { "text": "bubbleSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'bubbleSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'bubbleSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -341,10 +341,10 @@ "name": "index", "contents": [ "function bubbleSort(array) {", - " // change code below this line", + "// change code below this line", "", - " // change code above this line", - " return array;", + "// change code above this line", + "return array;", "}", "", "// test array:", @@ -353,8 +353,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - " return check(0);", + "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + "return check(0);", "};" ] } @@ -371,19 +371,19 @@ "tests": [ { "text": "selectionSort is a function.", - "testString": "assert(typeof selectionSort == 'function', 'selectionSort is a function.');" + "testString": "assert(typeof selectionSort =='function','selectionSort is a function.');" }, { "text": "selectionSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'selectionSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'selectionSort returns a sorted array (least to greatest).');" }, { "text": "selectionSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'selectionSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'selectionSort returns an array that is unchanged except for order.');" }, { "text": "selectionSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'selectionSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'selectionSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -396,10 +396,10 @@ "name": "index", "contents": [ "function selectionSort(array) {", - " // change code below this line", + "// change code below this line", "", - " // change code above this line", - " return array;", + "// change code above this line", + "return array;", "}", "", "// test array:", @@ -408,8 +408,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - " return check(0);", + "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + "return check(0);", "};" ] } @@ -426,19 +426,19 @@ "tests": [ { "text": "insertionSort is a function.", - "testString": "assert(typeof insertionSort == 'function', 'insertionSort is a function.');" + "testString": "assert(typeof insertionSort =='function','insertionSort is a function.');" }, { "text": "insertionSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'insertionSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'insertionSort returns a sorted array (least to greatest).');" }, { "text": "insertionSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'insertionSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'insertionSort returns an array that is unchanged except for order.');" }, { "text": "insertionSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'insertionSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'insertionSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -451,10 +451,10 @@ "name": "index", "contents": [ "function insertionSort(array) {", - " // change code below this line", + "// change code below this line", "", - " // change code above this line", - " return array;", + "// change code above this line", + "return array;", "}", "", "// test array:", @@ -463,8 +463,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - " return check(0);", + "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + "return check(0);", "};" ] } @@ -482,19 +482,19 @@ "tests": [ { "text": "quickSort is a function.", - "testString": "assert(typeof quickSort == 'function', 'quickSort is a function.');" + "testString": "assert(typeof quickSort =='function','quickSort is a function.');" }, { "text": "quickSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'quickSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'quickSort returns a sorted array (least to greatest).');" }, { "text": "quickSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'quickSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'quickSort returns an array that is unchanged except for order.');" }, { "text": "quickSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'quickSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'quickSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -507,10 +507,10 @@ "name": "index", "contents": [ "function quickSort(array) {", - " // change code below this line", + "// change code below this line", "", - " // change code above this line", - " return array;", + "// change code above this line", + "return array;", "}", "", "// test array:", @@ -519,8 +519,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - " return check(0);", + "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + "return check(0);", "};" ] } @@ -541,19 +541,19 @@ "tests": [ { "text": "mergeSort is a function.", - "testString": "assert(typeof mergeSort == 'function', 'mergeSort is a function.');" + "testString": "assert(typeof mergeSort =='function','mergeSort is a function.');" }, { "text": "mergeSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'mergeSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'mergeSort returns a sorted array (least to greatest).');" }, { "text": "mergeSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'mergeSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'mergeSort returns an array that is unchanged except for order.');" }, { "text": "mergeSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'mergeSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'mergeSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -566,10 +566,10 @@ "name": "index", "contents": [ "function mergeSort(array) {", - " // change code below this line", + "// change code below this line", "", - " // change code above this line", - " return array;", + "// change code above this line", + "return array;", "}", "", "// test array:", @@ -578,8 +578,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - " return check(0);", + "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + "return check(0);", "};" ] } From 08448f4d7763e6e18788f3924ca8a8bfd46d0c74 Mon Sep 17 00:00:00 2001 From: MoverLee Date: Tue, 24 Jul 2018 10:48:41 +0800 Subject: [PATCH 09/24] =?UTF-8?q?=E4=B8=8E=E5=85=B6=E4=BB=96=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E7=9B=B8=E5=90=8C=E4=BF=A1=E6=81=AF=E4=BF=9D?= =?UTF-8?q?=E6=8C=81=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 08-coding-interview-prep/algorithms.json | 74 ++++++++++++------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index dd2c0f9..edee9f4 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -18,60 +18,60 @@ ], "tests": [ { - "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该是 < code>[3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该是 < code>[3, 4, 5]。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4]) 应该返回 < code>[3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4]) 应该返回 < code>[3, 4, 5]。');" }, { "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" }, { - "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该是 [3, 4, 5]。');" + "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 应该返回 [3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3, 3], [5, 2, 1, 4]) 应该返回 [3, 4, 5]。');" }, { - "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" + "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素", + "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该是 [3, 4, 5]。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 应该返回 [3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4, 5]) 应该返回 [3, 4, 5]。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4, 5]) 的返回值应该只包含三个元素。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 返回的数组应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4, 5]) 返回的数组应该只包含三个元素。');" }, { - "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5],'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该是 [1, 4, 5]。');" + "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 应该返回 [1, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5],'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 应该返回 [1, 4, 5]。');" }, { - "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3,'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 的返回值应该只包含三个元素。');" + "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 返回的数组应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3,'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 返回的数组应该只包含三个元素。');" }, { - "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5],'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该是 [1, 4, 5]。');" + "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 应该返回 [1, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5],'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 应该返回 [1, 4, 5]。');" }, { - "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3,'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 的返回值应该只包含三个元素。');" + "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 返回的数组应该只包含三个元素。", + "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3,'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 返回的数组应该只包含三个元素。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该是 [2, 3, 4, 6, 7]。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 应该返回 [2, 3, 4, 6, 7]。", + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 应该返回 [2, 3, 4, 6, 7]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 的返回值应该只包含五个元素。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素;", + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该是 [1, 2, 4, 5, 6, 7, 8, 9]。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 应该返回 [1, 2, 4, 5, 6, 7, 8, 9]。", + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 应该返回 [1, 2, 4, 5, 6, 7, 8, 9]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 的返回值应该只包含八个元素。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 返回的数组应该只包含八个元素。", + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 返回的数组应该只包含八个元素。');" } ], "MDNlinks": [ @@ -109,28 +109,28 @@ ], "tests": [ { - "text": "函数 updateInventory 的返回值应该是一个数组。", - "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]),'函数 updateInventory 的返回值应该是一个数组。');" + "text": "函数 updateInventory 应该返回一个数组。", + "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]),'函数 updateInventory 应该返回一个数组。');" }, { "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。", - "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。');" + "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回一个长度为 6 的数组。 。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 的返回值应该是 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" }, { - "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 的返回值应该是 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" + "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" }, { - "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]],'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 的返回值应该是 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" + "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]],'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" } ], "MDNlinks": [ From 96a9f21f4093604827e1ce1c49400fb9d1b926f8 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Tue, 24 Jul 2018 23:19:15 +0800 Subject: [PATCH 10/24] No Repeats Please --- 08-coding-interview-prep/algorithms.json | 42 ++++++++++++------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index edee9f4..abdca68 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -183,44 +183,44 @@ ], "tests": [ { - "text": "permAlone(\"aab\") should return a number.", - "testString": "assert.isNumber(permAlone('aab'),'permAlone(\"aab\") should return a number.');" + "text": "permAlone(\"aab\") 应该返回一个数字。", + "testString": "assert.isNumber(permAlone('aab'),'permAlone(\"aab\") 应该返回一个数字。');" }, { - "text": "permAlone(\"aab\") should return 2.", - "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\") should return 2.');" + "text": "permAlone(\"aab\") 应该返回 2 。", + "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\") 应该返回 2 。');" }, { - "text": "permAlone(\"aaa\") should return 0.", - "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\") should return 0.');" + "text": "permAlone(\"aaa\") 应该返回 0 。", + "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\") 应该返回 0 。');" }, { - "text": "permAlone(\"aabb\") should return 8.", - "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\") should return 8.');" + "text": "permAlone(\"aabb\") 应该返回 8 。", + "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\") 应该返回 8 。');" }, { - "text": "permAlone(\"abcdefa\") should return 3600.", - "testString": "assert.strictEqual(permAlone('abcdefa'), 3600,'permAlone(\"abcdefa\") should return 3600.');" + "text": "permAlone(\"abcdefa\") 应该返回 3600 。", + "testString": "assert.strictEqual(permAlone('abcdefa'), 3600,'permAlone(\"abcdefa\") 应该返回 3600 。');" }, { - "text": "permAlone(\"abfdefa\") should return 2640.", - "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\") should return 2640.');" + "text": "permAlone(\"abfdefa\") 应该返回 2640 。", + "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\") 应该返回 2640 。');" }, { - "text": "permAlone(\"zzzzzzzz\") should return 0.", - "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\") should return 0.');" + "text": "permAlone(\"zzzzzzzz\") 应该返回 0 。", + "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\") 应该返回 0 。');" }, { - "text": "permAlone(\"a\") should return 1.", - "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\") should return 1.');" + "text": "permAlone(\"a\") 应该返回 1 。", + "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\") 应该返回 1 。');" }, { - "text": "permAlone(\"aaab\") should return 0.", - "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\") should return 0.');" + "text": "permAlone(\"aaab\") 应该返回 0 。", + "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\") 应该返回 0 。');" }, { - "text": "permAlone(\"aaabb\") should return 12.", - "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\") should return 12.');" + "text": "permAlone(\"aaabb\") 应该返回 12 。", + "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\") 应该返回 12 。');" } ], "MDNlinks": [ @@ -255,7 +255,7 @@ "
Index01234
Value79111315
", "Below we'll take their corresponding indices and add them.", "7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6", - "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", ], "solutions": [ "function pairwise(arr, arg) {\n var sum = 0;\n arr.forEach(function(e, i, a) {\n if (e != null) { \n var diff = arg-e;\n a[i] = null;\n var dix = a.indexOf(diff);\n if (dix !== -1) {\n sum += dix;\n sum += i;\n a[dix] = null;\n } \n }\n });\n return sum;\n}\n\npairwise([1,4,2,3,0,5], 7);\n" From b1fa3379e14b9ec52342472270b681761ca49637 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Thu, 26 Jul 2018 22:52:03 +0800 Subject: [PATCH 11/24] Modified by reviwer --- .DS_Store | Bin 8196 -> 0 bytes 08-coding-interview-prep/algorithms.json | 72 +++++++++++------------ 2 files changed, 36 insertions(+), 36 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 266076d47015893c518dda68e8a41e9ef3ccc346..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeHM-A)rh6g~r}Ta3Vk3f}FFi5D#?KWd3FsTD4`FfrEXg&Npx7Z|eLt=%mR)SBM- z4)_SZiErS8_yGEynW3}`#7IIgGLy`l`8(g4`OeI4&z6WprCnbjnj<0yndQt)6sIH} z=X@fKnVu;~0ezx?5^7RR5u{C)0n318z%pPNunhc*4B(y3$vNY_uSacZ8L$joNe1}& z;3Bgu8`;-VJvvaR5CC!l-72AuI6&#RMwX52YpGO$siOyBRD}sKgjUD6%hMstM)tMT z>Lj!}31eB92t_FC&~w#v5@juIX&JB#oMeDw_c&c@8`=W&#JG74HsS=IwgdH+^M^|(`s+fhAH zI8{C1I5{V`F*DokE|yApce&WhyWPhti+Q)SRO8*B~x?=I{vL30bU`CATK3k+@u`#$O&j`nRZJ^D}#OMtm*+T7 z7J&E&I1+o?QcO0ESA%{=t0~STIM^Su_;IlQqy9wgiFt|G_UK;J4LQu*kP`4}uv%C4 zx6L}z!43l4yaM*h9&2Xs*ki+Z)7Z(1!sGp?K-^Ls@EJJWM}MLHpWe`(#TYc0gg3s$Pt5Y};67!lk=lmSqMVoKaJl8V1d@_f1(X`1jaKQ|WYy3Hm z|JTm`{(r%%u<(`v%fMwaASPC;s}-!^=*Mh!F&t~V$eYNVcwS#irGi3*z>% diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index abdca68..305a278 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,10 +8,10 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ - "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。 百科 。", - "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为 ** 对称差 ** 的数组", - "思路:设定两个数组 (例如:A = {1, 2, 3}B = {2, 3, 4},) 作为参数传入,返回对称差数组(A △ B = C = {1, 4}),且数组元素具有唯一性(没有重复项)。", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "知识提要: 对称差 (Symmetric Difference),数学上,两个 集合 的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差分为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。", + "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为 ** 对称差分 ** 的数组", + "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4],) 作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组元素具有唯一性(没有重复项)。", + "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" @@ -31,7 +31,7 @@ }, { "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素", - "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素');" + "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素。');" }, { "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 应该返回 [3, 4, 5]。", @@ -62,7 +62,7 @@ "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 应该返回 [2, 3, 4, 6, 7]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素;", + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素。", "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素。');" }, { @@ -102,7 +102,7 @@ "description": [ "", "比较和更新存储在 二维数组 中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。", ], "solutions": [ "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21,'Bowling Ball'],\n [2,'Dirty Sock'],\n [1,'Hair Pin'],\n [5,'Microphone']\n];\n\nvar newInv = [\n [2,'Hair Pin'],\n [3,'Half-Eaten Apple'],\n [67,'Bowling Ball'],\n [7,'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" @@ -113,24 +113,24 @@ "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]),'函数 updateInventory 应该返回一个数组。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6 。", - "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回一个长度为 6 的数组。 。');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6。", + "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回一个长度为 6 的数组。');" }, { "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]].');" + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。');" }, { "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]].');" + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。');" }, { "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]].');" + "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。');" }, { "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]],'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]].');" + "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]],'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。');" } ], "MDNlinks": [ @@ -144,11 +144,11 @@ "name": "index", "contents": [ "function updateInventory(arr1, arr2) {", - "// All inventory must be accounted for or you're fired!", + "// 所有的存货都必须记帐,否则你将被解雇!", "return arr1;", "}", "", - "// Example inventory lists", + "// 两个货物列表示例", "var curInv = [", "[21, \"Bowling Ball\"],", "[2, \"Dirty Sock\"],", @@ -175,11 +175,11 @@ "title": "No Repeats Please", "description": [ "把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数。连续重复只以单个字符为准。", - "例如:aab 应该返回 2 , 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", - "如果你遇到了困难,请点击 帮助 。请尽量编写属于你自己的代码。尝试一下 ** 结对编程 ** 会有意料不到的惊喜哦。" + "例如:aab 应该返回 2, 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", + "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。" ], "solutions": [ - "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: Global utility array which holds a list of \"currently-in-use\"characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\"from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" + "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: 包含 Global utility array which holds a list of \"currently-in-use\"characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\"from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" ], "tests": [ { @@ -187,40 +187,40 @@ "testString": "assert.isNumber(permAlone('aab'),'permAlone(\"aab\") 应该返回一个数字。');" }, { - "text": "permAlone(\"aab\") 应该返回 2 。", - "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\") 应该返回 2 。');" + "text": "permAlone(\"aab\") 应该返回 2。", + "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\") 应该返回 2。');" }, { - "text": "permAlone(\"aaa\") 应该返回 0 。", - "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\") 应该返回 0 。');" + "text": "permAlone(\"aaa\") 应该返回 0。", + "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\") 应该返回 0。');" }, { - "text": "permAlone(\"aabb\") 应该返回 8 。", - "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\") 应该返回 8 。');" + "text": "permAlone(\"aabb\") 应该返回 8。", + "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\") 应该返回 8。');" }, { - "text": "permAlone(\"abcdefa\") 应该返回 3600 。", - "testString": "assert.strictEqual(permAlone('abcdefa'), 3600,'permAlone(\"abcdefa\") 应该返回 3600 。');" + "text": "permAlone(\"abcdefa\") 应该返回 3600。", + "testString": "assert.strictEqual(permAlone('abcdefa'), 3600,'permAlone(\"abcdefa\") 应该返回 3600。');" }, { - "text": "permAlone(\"abfdefa\") 应该返回 2640 。", - "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\") 应该返回 2640 。');" + "text": "permAlone(\"abfdefa\") 应该返回 2640。", + "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\") 应该返回 2640。');" }, { - "text": "permAlone(\"zzzzzzzz\") 应该返回 0 。", - "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\") 应该返回 0 。');" + "text": "permAlone(\"zzzzzzzz\") 应该返回 0。", + "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\") 应该返回 0。');" }, { - "text": "permAlone(\"a\") 应该返回 1 。", - "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\") 应该返回 1 。');" + "text": "permAlone(\"a\") 应该返回 1。", + "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\") 应该返回 1。');" }, { - "text": "permAlone(\"aaab\") 应该返回 0 。", - "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\") 应该返回 0 。');" + "text": "permAlone(\"aaab\") 应该返回 0。", + "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\") 应该返回 0。');" }, { - "text": "permAlone(\"aaabb\") 应该返回 12 。", - "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\") 应该返回 12 。');" + "text": "permAlone(\"aaabb\") 应该返回 12。", + "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\") 应该返回 12。');" } ], "MDNlinks": [ From 575325b9b4d7801dbb5041efeaa158b8f504ff63 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Sat, 28 Jul 2018 19:21:50 +0800 Subject: [PATCH 12/24] modified the space of chinese && code --- 08-coding-interview-prep/algorithms.json | 118 +++++++++++------------ 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 305a278..3afa5b8 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -18,60 +18,60 @@ ], "tests": [ { - "text": "sym([1, 2, 3], [5, 2, 1, 4]) 应该返回 < code>[3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4]) 应该返回 < code>[3, 4, 5]。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4])应该返回< code>[3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4])应该返回< code>[3, 4, 5]。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4]) 的返回值应该只包含三个元素。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4])的返回值应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4])的返回值应该只包含三个元素。');" }, { - "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 应该返回 [3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3, 3], [5, 2, 1, 4]) 应该返回 [3, 4, 5]。');" + "text": "sym([1, 2, 3, 3], [5, 2, 1, 4])应该返回[3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3, 3], [5, 2, 1, 4])应该返回[3, 4, 5]。');" }, { - "text": "sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素", - "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4]) 返回的数组应该只包含三个元素。');" + "text": "sym([1, 2, 3, 3], [5, 2, 1, 4])返回的数组应该只包含三个元素", + "testString": "assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3,'sym([1, 2, 3, 3], [5, 2, 1, 4])返回的数组应该只包含三个元素。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 应该返回 [3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4, 5]) 应该返回 [3, 4, 5]。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4, 5])应该返回[3, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4, 5])应该返回[3, 4, 5]。');" }, { - "text": "sym([1, 2, 3], [5, 2, 1, 4, 5]) 返回的数组应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4, 5]) 返回的数组应该只包含三个元素。');" + "text": "sym([1, 2, 3], [5, 2, 1, 4, 5])返回的数组应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3,'sym([1, 2, 3], [5, 2, 1, 4, 5])返回的数组应该只包含三个元素。');" }, { - "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 应该返回 [1, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5],'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 应该返回 [1, 4, 5]。');" + "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5])应该返回[1, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5],'sym([1, 2, 5], [2, 3, 5], [3, 4, 5])应该返回[1, 4, 5]。');" }, { - "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 返回的数组应该只包含三个元素。", - "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3,'sym([1, 2, 5], [2, 3, 5], [3, 4, 5]) 返回的数组应该只包含三个元素。');" + "text": "sym([1, 2, 5], [2, 3, 5], [3, 4, 5])返回的数组应该只包含三个元素。", + "testString": "assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3,'sym([1, 2, 5], [2, 3, 5], [3, 4, 5])返回的数组应该只包含三个元素。');" }, { - "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 应该返回 [1, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5],'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 应该返回 [1, 4, 5]。');" + "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该返回[1, 4, 5]。", + "testString": "assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5],'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])应该返回[1, 4, 5]。');" }, { - "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 返回的数组应该只包含三个元素。", - "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3,'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) 返回的数组应该只包含三个元素。');" + "text": "sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])返回的数组应该只包含三个元素。", + "testString": "assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3,'sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])返回的数组应该只包含三个元素。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 应该返回 [2, 3, 4, 6, 7]。", + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])应该返回[2, 3, 4, 6, 7]。", "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 应该返回 [2, 3, 4, 6, 7]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素。", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) 返回的数组应该只包含五个元素。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])返回的数组应该只包含五个元素。", + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])返回的数组应该只包含五个元素。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 应该返回 [1, 2, 4, 5, 6, 7, 8, 9]。", - "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 应该返回 [1, 2, 4, 5, 6, 7, 8, 9]。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])应该返回[1, 2, 4, 5, 6, 7, 8, 9]。", + "testString": "assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9],'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])应该返回[1, 2, 4, 5, 6, 7, 8, 9]。');" }, { - "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 返回的数组应该只包含八个元素。", - "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) 返回的数组应该只包含八个元素。');" + "text": "sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])返回的数组应该只包含八个元素。", + "testString": "assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8,'sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])返回的数组应该只包含八个元素。');" } ], "MDNlinks": [ @@ -109,27 +109,27 @@ ], "tests": [ { - "text": "函数 updateInventory 应该返回一个数组。", - "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]),'函数 updateInventory 应该返回一个数组。');" + "text": "函数updateInventory应该返回一个数组。", + "testString": "assert.isArray(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]),'函数updateInventory应该返回一个数组。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 返回的数组长度应该为 6。", - "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回一个长度为 6 的数组。');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]])返回的数组长度应该为 6。", + "testString": "assert.equal(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]).length, 6,'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]])应该返回一个长度为 6 的数组。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]])应该返回[[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]])应该返回[[88, \"Bowling Ball\"], [2, \"Dirty Sock\"], [3, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [5, \"Microphone\"], [7, \"Toothpaste\"]]。');" }, { - "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", - "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []) 应该返回 [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。');" + "text": "updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [])应该返回[[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。", + "testString": "assert.deepEqual(updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], []), [[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]],'updateInventory([[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]], [])应该返回[[21, \"Bowling Ball\"], [2, \"Dirty Sock\"], [1, \"Hair Pin\"], [5, \"Microphone\"]]。');" }, { - "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", - "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]) 应该返回 [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。');" + "text": "updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]])应该返回[[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。", + "testString": "assert.deepEqual(updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]]), [[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]],'updateInventory([], [[2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [67, \"Bowling Ball\"], [7, \"Toothpaste\"]])应该返回[[67, \"Bowling Ball\"], [2, \"Hair Pin\"], [3, \"Half-Eaten Apple\"], [7, \"Toothpaste\"]]。');" }, { - "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", + "text": "updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]])应该返回[[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。", "testString": "assert.deepEqual(updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]), [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]],'updateInventory([[0, \"Bowling Ball\"], [0, \"Dirty Sock\"], [0, \"Hair Pin\"], [0, \"Microphone\"]], [[1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [1, \"Bowling Ball\"], [1, \"Toothpaste\"]]) 应该返回 [[1, \"Bowling Ball\"], [0, \"Dirty Sock\"], [1, \"Hair Pin\"], [1, \"Half-Eaten Apple\"], [0, \"Microphone\"], [1, \"Toothpaste\"]]。');" } ], @@ -175,52 +175,52 @@ "title": "No Repeats Please", "description": [ "把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数。连续重复只以单个字符为准。", - "例如:aab 应该返回 2, 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", + "例如:aab应该返回 2, 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。" ], "solutions": [ - "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: 包含 Global utility array which holds a list of \"currently-in-use\"characters\n var permArr = [], usedChars = [];\n function permute(input) {\n //convert input into a char array (one element for each character)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n //get and remove character at index \"i\"from char array\n ch = chars.splice(i, 1);\n //add removed character to the end of used characters\n usedChars.push(ch);\n //when there are no more characters left in char array to add, add used chars to list of permutations\n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n //send characters (minus the removed one from above) from char array to be permuted\n permute(chars.join(\"\"));\n //add removed character back into char array in original position\n chars.splice(i, 0, ch);\n //remove the last character used off the end of used characters array\n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" + "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: 全局实用数组,其中一个列表包含 \"currently-in-use\"字符 var permArr = [], usedChars = [];\n function permute(input) {\n // 将输入参数转换成一个字符串数组 (每个字符都有一个元素)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n // 从字符数组中获取并删除索引为\"i\"的字符 \n ch = chars.splice(i, 1);\n // 将删除的字符增加的使用字符的末尾 \n usedChars.push(ch);\n // 当char数组中没有其他字符添加时,添加使用的chars列表来排列排列 \n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n // 从char数组发送字符(减去上面删除的字符)进行置换 \n permute(chars.join(\"\"));\n // 将删除的数组添加回char数组中的原位置 \n chars.splice(i, 0, ch);\n //删除使用过的字符数组末尾使用的最后一个字符 \n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" ], "tests": [ { - "text": "permAlone(\"aab\") 应该返回一个数字。", - "testString": "assert.isNumber(permAlone('aab'),'permAlone(\"aab\") 应该返回一个数字。');" + "text": "permAlone(\"aab\")应该返回一个数字。", + "testString": "assert.isNumber(permAlone('aab'),'permAlone(\"aab\")应该返回一个数字。');" }, { - "text": "permAlone(\"aab\") 应该返回 2。", - "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\") 应该返回 2。');" + "text": "permAlone(\"aab\")应该返回 2。", + "testString": "assert.strictEqual(permAlone('aab'), 2,'permAlone(\"aab\")应该返回 2。');" }, { - "text": "permAlone(\"aaa\") 应该返回 0。", - "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\") 应该返回 0。');" + "text": "permAlone(\"aaa\")应该返回 0。", + "testString": "assert.strictEqual(permAlone('aaa'), 0,'permAlone(\"aaa\")应该返回 0。');" }, { - "text": "permAlone(\"aabb\") 应该返回 8。", - "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\") 应该返回 8。');" + "text": "permAlone(\"aabb\")应该返回 8。", + "testString": "assert.strictEqual(permAlone('aabb'), 8,'permAlone(\"aabb\")应该返回 8。');" }, { - "text": "permAlone(\"abcdefa\") 应该返回 3600。", + "text": "permAlone(\"abcdefa\")应该返回 3600。", "testString": "assert.strictEqual(permAlone('abcdefa'), 3600,'permAlone(\"abcdefa\") 应该返回 3600。');" }, { - "text": "permAlone(\"abfdefa\") 应该返回 2640。", - "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\") 应该返回 2640。');" + "text": "permAlone(\"abfdefa\")应该返回 2640。", + "testString": "assert.strictEqual(permAlone('abfdefa'), 2640,'permAlone(\"abfdefa\")应该返回 2640。');" }, { - "text": "permAlone(\"zzzzzzzz\") 应该返回 0。", - "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\") 应该返回 0。');" + "text": "permAlone(\"zzzzzzzz\")应该返回 0。", + "testString": "assert.strictEqual(permAlone('zzzzzzzz'), 0,'permAlone(\"zzzzzzzz\")应该返回 0。');" }, { - "text": "permAlone(\"a\") 应该返回 1。", - "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\") 应该返回 1。');" + "text": "permAlone(\"a\")应该返回 1。", + "testString": "assert.strictEqual(permAlone('a'), 1,'permAlone(\"a\")应该返回 1。');" }, { - "text": "permAlone(\"aaab\") 应该返回 0。", - "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\") 应该返回 0。');" + "text": "permAlone(\"aaab\")应该返回 0。", + "testString": "assert.strictEqual(permAlone('aaab'), 0,'permAlone(\"aaab\")应该返回 0。');" }, { - "text": "permAlone(\"aaabb\") 应该返回 12。", - "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\") 应该返回 12。');" + "text": "permAlone(\"aaabb\")应该返回 12。", + "testString": "assert.strictEqual(permAlone('aaabb'), 12,'permAlone(\"aaabb\")应该返回 12。');" } ], "MDNlinks": [ From b686c287dda47fc1e5068a69fb95b18e1f9476cc Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Tue, 31 Jul 2018 20:46:42 +0800 Subject: [PATCH 13/24] Modified by reviwer --- 08-coding-interview-prep/algorithms.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 3afa5b8..83510d1 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,10 +8,10 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ - "知识提要: 对称差 (Symmetric Difference),数学上,两个 集合 的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合 A = { 1, 2, 3} 和 B = { 2, 3, 4} 的对称差分为 A △ B = C = {1,4},。 集合论中的这个运算相当于布尔逻辑中的异或运算。", - "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为 ** 对称差分 ** 的数组", + "知识提要: 对称差 (Symmetric Difference),数学上,两个 集合 的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合let A = [ 1, 2, 3]let B = [ 2, 3, 4]的对称差分为A △ B = C = [ 1, 4],。 集合论中的这个运算相当于布尔逻辑中的异或运算。", + "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为对称差分的数组", "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4],) 作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组元素具有唯一性(没有重复项)。", - "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助。你可以找人“结对编程”,但不要因此放弃思考。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" @@ -101,8 +101,8 @@ "title": "Inventory Update", "description": [ "", - "比较和更新存储在 二维数组 中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", - "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。", + "比较和更新存储在 二维数组 中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", + "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", ], "solutions": [ "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21,'Bowling Ball'],\n [2,'Dirty Sock'],\n [1,'Hair Pin'],\n [5,'Microphone']\n];\n\nvar newInv = [\n [2,'Hair Pin'],\n [3,'Half-Eaten Apple'],\n [67,'Bowling Ball'],\n [7,'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" From d888fa98820c9e7725ca60ddf683fcf90686f888 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Tue, 31 Jul 2018 21:11:39 +0800 Subject: [PATCH 14/24] Delete the space --- 08-coding-interview-prep/algorithms.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 83510d1..1db401c 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,9 +8,9 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ - "知识提要: 对称差 (Symmetric Difference),数学上,两个 集合 的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合let A = [ 1, 2, 3]let B = [ 2, 3, 4]的对称差分为A △ B = C = [ 1, 4],。 集合论中的这个运算相当于布尔逻辑中的异或运算。", + "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合let A = [ 1, 2, 3]let B = [ 2, 3, 4]的对称差分为A △ B = C = [ 1, 4],。 集合论中的这个运算相当于布尔逻辑中的异或运算。", "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为对称差分的数组", - "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4],) 作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组元素具有唯一性(没有重复项)。", + "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4],)作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组元素具有唯一性(没有重复项)。", "如果你遇到了困难,请点击 帮助。你可以找人“结对编程”,但不要因此放弃思考。", ], "solutions": [ @@ -19,7 +19,7 @@ "tests": [ { "text": "sym([1, 2, 3], [5, 2, 1, 4])应该返回< code>[3, 4, 5]。", - "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4])应该返回< code>[3, 4, 5]。');" + "testString": "assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5],'sym([1, 2, 3], [5, 2, 1, 4])应该返回[3, 4, 5]。');" }, { "text": "sym([1, 2, 3], [5, 2, 1, 4])的返回值应该只包含三个元素。", @@ -101,7 +101,7 @@ "title": "Inventory Update", "description": [ "", - "比较和更新存储在 二维数组 中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", + "比较和更新存储在二维数组中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", ], "solutions": [ From 83a8be9d32247de900e229dcc9c1ae2fc8fa004a Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Sun, 5 Aug 2018 20:31:01 +0800 Subject: [PATCH 15/24] Modified by reviwer --- 08-coding-interview-prep/algorithms.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 1db401c..4ab2f10 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -100,8 +100,7 @@ "id": "a56138aff60341a09ed6c480", "title": "Inventory Update", "description": [ - "", - "比较和更新存储在二维数组中的库存,并将其与新交付的第二个二维数组进行比较。更新当前的库存项数量(arr1)。如果找不到项目,请将新项目和数量添加到库存数组中。返回的库存数组应该按照项目的字母顺序排列。", + "创建一个二维数组,比较并更新存储在二维数组中的”库存“元素,然后并将其与新产生的第二个二维数组进行对比,更新当前的”库存“项的数量(arr1),如果找不到这个对比对象,那么将新的对象和数量添加到“库存”数组中。注意:返回的“库存”数组应该是按照数组元素的首字母顺序排序", "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", ], "solutions": [ From df7b55ebb02fed3fefad2ec8a042a7ee71d12a7c Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Mon, 6 Aug 2018 14:00:35 +0800 Subject: [PATCH 16/24] Modified by reviwer --- 08-coding-interview-prep/algorithms.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 4ab2f10..7c20671 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -8,10 +8,10 @@ "id": "a3f503de51cf954ede28891d", "title": "Find the Symmetric Difference", "description": [ - "知识提要:对称差 (Symmetric Difference),数学上,两个 集合 的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合let A = [ 1, 2, 3]let B = [ 2, 3, 4]的对称差分为A △ B = C = [ 1, 4],。 集合论中的这个运算相当于布尔逻辑中的异或运算。", + "知识提要:对称差 (Symmetric Difference),数学上,两个集合的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合let A = [ 1, 2, 3]let B = [ 2, 3, 4]的对称差分为A △ B = C = [ 1, 4]。 集合论中的这个运算相当于布尔逻辑中的异或运算。", "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为对称差分的数组", - "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4],)作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组元素具有唯一性(没有重复项)。", - "如果你遇到了困难,请点击 帮助。你可以找人“结对编程”,但不要因此放弃思考。", + "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4])作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组中没有重复项。", + "如果你遇到了困难,请点击帮助。你可以找人“结对编程”,但不要因此放弃思考。", ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" @@ -100,8 +100,8 @@ "id": "a56138aff60341a09ed6c480", "title": "Inventory Update", "description": [ - "创建一个二维数组,比较并更新存储在二维数组中的”库存“元素,然后并将其与新产生的第二个二维数组进行对比,更新当前的”库存“项的数量(arr1),如果找不到这个对比对象,那么将新的对象和数量添加到“库存”数组中。注意:返回的“库存”数组应该是按照数组元素的首字母顺序排序", - "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", + "创建一个二维数组,比较并更新存储在二维数组中的 ”库存“ 元素,然后并将其与新产生的第二个二维数组进行对比,更新当前的 ”库存“ 项的数量(arr1),如果找不到这个对比对象,那么将新的对象和数量添加到 “库存” 数组中。注意:返回的 “库存” 数组应该是按照数组元素的首字母顺序排序", + "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", ], "solutions": [ "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21,'Bowling Ball'],\n [2,'Dirty Sock'],\n [1,'Hair Pin'],\n [5,'Microphone']\n];\n\nvar newInv = [\n [2,'Hair Pin'],\n [3,'Half-Eaten Apple'],\n [67,'Bowling Ball'],\n [7,'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" From d13f95544c973687c03ca70efa1d9cbca4bf6d68 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Mon, 6 Aug 2018 15:08:35 +0800 Subject: [PATCH 17/24] Recover space --- 08-coding-interview-prep/algorithms.json | 128 +++++++++++------------ 1 file changed, 64 insertions(+), 64 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index 7c20671..77ccf94 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -86,7 +86,7 @@ "name": "index", "contents": [ "function sym(args) {", - "return args;", + " return args;", "}", "", "sym([1, 2, 3], [5, 2, 1, 4]);" @@ -144,22 +144,22 @@ "contents": [ "function updateInventory(arr1, arr2) {", "// 所有的存货都必须记帐,否则你将被解雇!", - "return arr1;", + " return arr1;", "}", "", "// 两个货物列表示例", "var curInv = [", - "[21, \"Bowling Ball\"],", - "[2, \"Dirty Sock\"],", - "[1, \"Hair Pin\"],", - "[5, \"Microphone\"]", + " [21, \"Bowling Ball\"],", + " [2, \"Dirty Sock\"],", + " [1, \"Hair Pin\"],", + " [5, \"Microphone\"]", "];", "", "var newInv = [", - "[2, \"Hair Pin\"],", - "[3, \"Half-Eaten Apple\"],", - "[67, \"Bowling Ball\"],", - "[7, \"Toothpaste\"]", + " [2, \"Hair Pin\"],", + " [3, \"Half-Eaten Apple\"],", + " [67, \"Bowling Ball\"],", + " [7, \"Toothpaste\"]", "];", "", "updateInventory(curInv, newInv);" @@ -234,7 +234,7 @@ "name": "index", "contents": [ "function permAlone(str) {", - "return str;", + " return str;", "}", "", "permAlone('aab');" @@ -254,7 +254,7 @@ "
Index01234
Value79111315
", "Below we'll take their corresponding indices and add them.", "7 + 13 = 20 → Indices 0 + 3 = 3
9 + 11 = 20 → Indices 1 + 2 = 3
3 + 3 = 6 → Return 6", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code." ], "solutions": [ "function pairwise(arr, arg) {\n var sum = 0;\n arr.forEach(function(e, i, a) {\n if (e != null) { \n var diff = arg-e;\n a[i] = null;\n var dix = a.indexOf(diff);\n if (dix !== -1) {\n sum += dix;\n sum += i;\n a[dix] = null;\n } \n }\n });\n return sum;\n}\n\npairwise([1,4,2,3,0,5], 7);\n" @@ -262,23 +262,23 @@ "tests": [ { "text": "pairwise([1, 4, 2, 3, 0, 5], 7) should return 11.", - "testString": "assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11,'pairwise([1, 4, 2, 3, 0, 5], 7) should return 11.');" + "testString": "assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11, 'pairwise([1, 4, 2, 3, 0, 5], 7) should return 11.');" }, { "text": "pairwise([1, 3, 2, 4], 4) should return 1.", - "testString": "assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1,'pairwise([1, 3, 2, 4], 4) should return 1.');" + "testString": "assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1, 'pairwise([1, 3, 2, 4], 4) should return 1.');" }, { "text": "pairwise([1, 1, 1], 2) should return 1.", - "testString": "assert.deepEqual(pairwise([1, 1, 1], 2), 1,'pairwise([1, 1, 1], 2) should return 1.');" + "testString": "assert.deepEqual(pairwise([1, 1, 1], 2), 1, 'pairwise([1, 1, 1], 2) should return 1.');" }, { "text": "pairwise([0, 0, 0, 0, 1, 1], 1) should return 10.", - "testString": "assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10,'pairwise([0, 0, 0, 0, 1, 1], 1) should return 10.');" + "testString": "assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10, 'pairwise([0, 0, 0, 0, 1, 1], 1) should return 10.');" }, { "text": "pairwise([], 100) should return 0.", - "testString": "assert.deepEqual(pairwise([], 100), 0,'pairwise([], 100) should return 0.');" + "testString": "assert.deepEqual(pairwise([], 100), 0, 'pairwise([], 100) should return 0.');" } ], "MDNlinks": [ @@ -292,7 +292,7 @@ "name": "index", "contents": [ "function pairwise(arr, arg) {", - "return arg;", + " return arg;", "}", "", "pairwise([1,4,2,3,0,5], 7);" @@ -307,7 +307,7 @@ "title": "Implement Bubble Sort", "description": [ "This is the first of several challenges on sorting algorithms. Given an array of unsorted items, we want to be able to return a sorted array. We will see several different methods to do this and learn some tradeoffs between these different approaches. While most modern languages have built-in sorting methods for operations like this, it is still important to understand some of the common basic approaches and learn how they can be implemented.", - "Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and'bubbles up'unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.", + "Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and 'bubbles up' unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.", "This method requires multiple iterations through the array and for average and worst cases has quadratic time complexity. While simple, it is usually impractical in most situations.", "Instructions: Write a function bubbleSort which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.", "Note:
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging array to see your sorting algorithm in action!" @@ -315,19 +315,19 @@ "tests": [ { "text": "bubbleSort is a function.", - "testString": "assert(typeof bubbleSort =='function','bubbleSort is a function.');" + "testString": "assert(typeof bubbleSort == 'function', 'bubbleSort is a function.');" }, { "text": "bubbleSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'bubbleSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'bubbleSort returns a sorted array (least to greatest).');" }, { "text": "bubbleSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'bubbleSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'bubbleSort returns an array that is unchanged except for order.');" }, { "text": "bubbleSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'bubbleSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'bubbleSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -340,10 +340,10 @@ "name": "index", "contents": [ "function bubbleSort(array) {", - "// change code below this line", + " // change code below this line", "", - "// change code above this line", - "return array;", + " // change code above this line", + " return array;", "}", "", "// test array:", @@ -352,8 +352,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - "return check(0);", + " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + " return check(0);", "};" ] } @@ -370,19 +370,19 @@ "tests": [ { "text": "selectionSort is a function.", - "testString": "assert(typeof selectionSort =='function','selectionSort is a function.');" + "testString": "assert(typeof selectionSort == 'function', 'selectionSort is a function.');" }, { "text": "selectionSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'selectionSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'selectionSort returns a sorted array (least to greatest).');" }, { "text": "selectionSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'selectionSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'selectionSort returns an array that is unchanged except for order.');" }, { "text": "selectionSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'selectionSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'selectionSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -395,10 +395,10 @@ "name": "index", "contents": [ "function selectionSort(array) {", - "// change code below this line", + " // change code below this line", "", - "// change code above this line", - "return array;", + " // change code above this line", + " return array;", "}", "", "// test array:", @@ -407,8 +407,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - "return check(0);", + " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + " return check(0);", "};" ] } @@ -425,19 +425,19 @@ "tests": [ { "text": "insertionSort is a function.", - "testString": "assert(typeof insertionSort =='function','insertionSort is a function.');" + "testString": "assert(typeof insertionSort == 'function', 'insertionSort is a function.');" }, { "text": "insertionSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'insertionSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'insertionSort returns a sorted array (least to greatest).');" }, { "text": "insertionSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'insertionSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'insertionSort returns an array that is unchanged except for order.');" }, { "text": "insertionSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'insertionSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'insertionSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -450,10 +450,10 @@ "name": "index", "contents": [ "function insertionSort(array) {", - "// change code below this line", + " // change code below this line", "", - "// change code above this line", - "return array;", + " // change code above this line", + " return array;", "}", "", "// test array:", @@ -462,8 +462,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - "return check(0);", + " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + " return check(0);", "};" ] } @@ -481,19 +481,19 @@ "tests": [ { "text": "quickSort is a function.", - "testString": "assert(typeof quickSort =='function','quickSort is a function.');" + "testString": "assert(typeof quickSort == 'function', 'quickSort is a function.');" }, { "text": "quickSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'quickSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'quickSort returns a sorted array (least to greatest).');" }, { "text": "quickSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'quickSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'quickSort returns an array that is unchanged except for order.');" }, { "text": "quickSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'quickSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'quickSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -506,10 +506,10 @@ "name": "index", "contents": [ "function quickSort(array) {", - "// change code below this line", + " // change code below this line", "", - "// change code above this line", - "return array;", + " // change code above this line", + " return array;", "}", "", "// test array:", @@ -518,8 +518,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - "return check(0);", + " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + " return check(0);", "};" ] } @@ -540,19 +540,19 @@ "tests": [ { "text": "mergeSort is a function.", - "testString": "assert(typeof mergeSort =='function','mergeSort is a function.');" + "testString": "assert(typeof mergeSort == 'function', 'mergeSort is a function.');" }, { "text": "mergeSort returns a sorted array (least to greatest).", - "testString": "assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])),'mergeSort returns a sorted array (least to greatest).');" + "testString": "assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])), 'mergeSort returns a sorted array (least to greatest).');" }, { "text": "mergeSort returns an array that is unchanged except for order.", - "testString": "assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92],'mergeSort returns an array that is unchanged except for order.');" + "testString": "assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92], 'mergeSort returns an array that is unchanged except for order.');" }, { "text": "mergeSort should not use the built-in .sort() method.", - "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1,'mergeSort should not use the built-in .sort() method.');" + "testString": "assert.strictEqual(code.search(/\\.sort\\(/), -1, 'mergeSort should not use the built-in .sort() method.');" } ], "solutions": [], @@ -565,10 +565,10 @@ "name": "index", "contents": [ "function mergeSort(array) {", - "// change code below this line", + " // change code below this line", "", - "// change code above this line", - "return array;", + " // change code above this line", + " return array;", "}", "", "// test array:", @@ -577,8 +577,8 @@ "head": [], "tail": [ "function isSorted(arr) {", - "var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", - "return check(0);", + " var check = (i) => (i == arr.length - 1) ? true : (arr[i] > arr[i + 1]) ? false : check(i + 1);", + " return check(0);", "};" ] } From e1eab52b015a9ac1de5117eb9cb59412334654f4 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Mon, 6 Aug 2018 23:10:37 +0800 Subject: [PATCH 18/24] Translated ReadMe --- 08-coding-interview-prep/algorithms.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/08-coding-interview-prep/algorithms.md b/08-coding-interview-prep/algorithms.md index 3c7930c..f8b792a 100644 --- a/08-coding-interview-prep/algorithms.md +++ b/08-coding-interview-prep/algorithms.md @@ -1,5 +1,5 @@ -# Introduction to the Coding Interview Algorithms # +# 算法面试简介 # -This introduction is a stub +简介存根 -Help us make it real on [GitHub](https://github.com/freeCodeCamp/learn/tree/master/src/introductions). +帮助我们在[GitHub](https://github.com/freeCodeCamp/learn/tree/master/src/introductions)完成这一项目。 \ No newline at end of file From a49a9dd07b15a09fffba684e8621dde52988abaf Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Wed, 8 Aug 2018 21:28:03 +0800 Subject: [PATCH 19/24] Modified by reviwer --- 08-coding-interview-prep/algorithms.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index c09c273..a55a13c 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -100,7 +100,7 @@ "id": "a56138aff60341a09ed6c480", "title": "Inventory Update", "description": [ - "创建一个二维数组,比较并更新存储在二维数组中的 ”库存“ 元素,然后并将其与新产生的第二个二维数组进行对比,更新当前的 ”库存“ 项的数量(arr1),如果找不到这个对比对象,那么将新的对象和数量添加到 “库存” 数组中。注意:返回的 “库存” 数组应该是按照数组元素的首字母顺序排序", + "创建一个二维数组,比较并更新存储在二维数组中的“库存”元素,然后并将其与新产生的第二个二维数组进行对比,更新当前的 ”库存“ 项的数量(arr1),如果找不到这个对比对象,那么将新的对象和数量添加到“库存”数组中。注意:返回的“库存”数组应该是按照数组元素的首字母顺序排序", "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", ], "solutions": [ @@ -173,9 +173,9 @@ "id": "a7bf700cd123b9a54eef01d5", "title": "No Repeats Please", "description": [ - "把一个字符串中的字符重新排列生成新的字符串,返回新生成的字符串里没有连续重复字符的字符串个数。连续重复只以单个字符为准。", - "例如:aab应该返回 2, 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符( 字符 a 是本例中的重复字符 ):abaaba", - "如果你遇到了困难,请点击 帮助 。你可以找人” 结对编程 “,但不要因此放弃思考。" + "把一个字符串中的所有的字符重新排列,然后生成一个新的字符串,返回的新字符串中没有连续重复的字符。连续重复是以单个字符为判断标准。", + "例如:aab应该返回 2, 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符(字符 a 是本例中的重复字符):abaaba", + "如果你遇到了困难,请点击 帮助 。你可以找人“结对编程“,但不要因此放弃思考。", ], "solutions": [ "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: 全局实用数组,其中一个列表包含 \"currently-in-use\"字符 var permArr = [], usedChars = [];\n function permute(input) {\n // 将输入参数转换成一个字符串数组 (每个字符都有一个元素)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n // 从字符数组中获取并删除索引为\"i\"的字符 \n ch = chars.splice(i, 1);\n // 将删除的字符增加的使用字符的末尾 \n usedChars.push(ch);\n // 当char数组中没有其他字符添加时,添加使用的chars列表来排列排列 \n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n // 从char数组发送字符(减去上面删除的字符)进行置换 \n permute(chars.join(\"\"));\n // 将删除的数组添加回char数组中的原位置 \n chars.splice(i, 0, ch);\n //删除使用过的字符数组末尾使用的最后一个字符 \n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" From 5f20dff1de7cea109071cae506c87d79a777ef83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E8=90=BD=E9=98=B3?= Date: Thu, 9 Aug 2018 17:37:14 +0800 Subject: [PATCH 20/24] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6=20RE?= =?UTF-8?q?ADME.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 367cffa..3d8a0b5 100644 --- a/README.md +++ b/README.md @@ -13,4 +13,4 @@ challenges 指的不仅是课程本身,也是每个开源项目贡献者会遇 5、获得 freeCodeCamp 中文社区的认可 -感兴趣的小伙伴可以传送到 [Wiki](https://github.com/FreeCodeCampChina/challenges/wiki) 解锁翻译指南。 +感兴趣的小伙伴可以传送到 [Wiki](https://freecodecamp.coding.net/p/challenges/wiki) 解锁翻译指南。 From 3db8adf6d0709c4b0e8b269201fe4cb839c7f568 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=83=A1=E8=90=BD=E9=98=B3?= Date: Fri, 10 Aug 2018 07:16:17 +0800 Subject: [PATCH 21/24] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E4=BB=B6=20RE?= =?UTF-8?q?ADME.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3d8a0b5..f917f55 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ challenges 指的不仅是课程本身,也是每个开源项目贡献者会遇 3、快速提高你的 Git 操作熟练度 -4、收获 GitHub 认可的 contributions +4、掌握一门前所未有的新技能:翻译 5、获得 freeCodeCamp 中文社区的认可 From b4a06428ca776d8808bb827033e1157e9afea4e4 Mon Sep 17 00:00:00 2001 From: cancan <790864964@qq.com> Date: Fri, 10 Aug 2018 10:27:19 +0800 Subject: [PATCH 22/24] functional-programming.json complete --- .../functional-programming.json | 779 +++++++++--------- 1 file changed, 390 insertions(+), 389 deletions(-) diff --git a/02-javascript-algorithms-and-data-structures/functional-programming.json b/02-javascript-algorithms-and-data-structures/functional-programming.json index f981533..d6d239b 100644 --- a/02-javascript-algorithms-and-data-structures/functional-programming.json +++ b/02-javascript-algorithms-and-data-structures/functional-programming.json @@ -8,24 +8,24 @@ "id": "587d7b8d367417b2b2512b5b", "title": "Learn About Functional Programming", "description": [ - "Functional programming is a style of programming where solutions are simple, isolated functions, without any side effects outside of the function scope.", + "函数式编程是一种解决方案简单,功能独立,对作用域外没有任何副作用的编程范式。", "INPUT -> PROCESS -> OUTPUT", - "Functional programming is about:", - "1) Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change", - "2) Pure functions - the same input always gives the same output", - "3) Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled", + "函数式编程:", + "1)功能独立——不依赖于程序的状态(比如可能发生变化的全局变量);", + "2)纯函数——同一个输入永远能得到同一个输出;", + "3)有限的副作用——可以严格地限制函数外部对状态的更改导致的状态变化。", "
", - "The members of freeCodeCamp happen to love tea.", - "In the code editor, the prepareTea and getTea functions are already defined for you. Call the getTea function to get 40 cups of tea for the team, and store them in the tea4TeamFCC variable." + "freeCodeCamp 成员在 love tea 的故事。", + "在代码编辑器中,已经为你定义好了prepareTeagetTea函数。调用getTea函数为团队准备 40 杯茶,并将它们存储在tea4TeamFCC变量里。" ], "tests": [ { - "text": "The tea4TeamFCC variable should hold 40 cups of tea for the team.", - "testString": "assert(tea4TeamFCC.length === 40, 'The tea4TeamFCC variable should hold 40 cups of tea for the team.');" + "text": "tea4TeamFCC变量里应有 40 杯为团队准备的茶。", + "testString": "assert(tea4TeamFCC.length === 40, 'tea4TeamFCC变量里应有 40 杯为团队准备的茶。');" }, { - "text": "The tea4TeamFCC variable should hold cups of green tea.", - "testString": "assert(tea4TeamFCC[0] === 'greenTea', 'The tea4TeamFCC variable should hold cups of green tea.');" + "text": "tea4TeamFCC变量里应有 greenTea。", + "testString": "assert(tea4TeamFCC[0] === 'greenTea', 'tea4TeamFCC变量里应有 greenTea。');" } ], "solutions": [], @@ -38,15 +38,15 @@ "name": "index", "contents": [ "/**", - " * A long process to prepare tea.", - " * @return {string} A cup of tea.", + " * 备茶过程", + " * @return {string} 一杯茶", " **/", "const prepareTea = () => 'greenTea';", "", "/**", - " * Get given number of cups of tea.", - " * @param {number} numOfCups Number of required cups of tea.", - " * @return {Array} Given amount of tea cups.", + " * 获得一定数量的茶", + " * @param {number} numOfCups 所需茶的数量", + " * @return {Array} 返回给定量的茶", " **/", "const getTea = (numOfCups) => {", " const teaCups = [];", @@ -59,11 +59,11 @@ " return teaCups;", "};", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "", "const tea4TeamFCC = null; // :(", "", - "// Add your code above this line", + "// 请在本行以上添加你的代码", "", "console.log(tea4TeamFCC);" ], @@ -76,33 +76,33 @@ "id": "587d7b8e367417b2b2512b5c", "title": "Understand Functional Programming Terminology", "description": [ - "The FCC Team had a mood swing and now wants two types of tea: green tea and black tea. General Fact: Client mood swings are pretty common.", - "With that information, we'll need to revisit the getTea function from last challenge to handle various tea requests. We can modify getTea to accept a function as a parameter to be able to change the type of tea it prepares. This makes getTea more flexible, and gives the programmer more control when client requests change.", - "But first, let's cover some functional terminology:", - "Callbacks are the functions that are slipped or passed into another function to decide the invocation of that function. You may have seen them passed to other methods, for example in filter, the callback function tells JavaScript the criteria for how to filter an array.", - "Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class functions. In JavaScript, all functions are first class functions.", - "The functions that take a function as an argument, or return a function as a return value are called higher order functions.", - "When the functions are passed in to another function or returned from another function, then those functions which gets passed in or returned can be called a lambda.", + "FCC 团队需求有变更,现在想要两种茶:绿茶(green tea)和红茶(black tea)。事实证明,用户需求变更是很常见的。", + "基于以上信息,我们需要重构上一节挑战中的getTea函数来处理多种茶的请求。我们可以修改getTea接受一个函数作为参数,使它能够修改茶的类型。这让getTea更灵活,也使需求变更时为程序员提供更多控制权。", + "首先,我们将介绍一些术语:", + "Callbacks是被传递到另一个函数中调用的函数。你应该已经在其他函数中看过这个写法,例如在filter中,回调函数告诉 JavaScript 以什么规则过滤数组。", + "函数就像其他正常值一样,可以赋值给变量、传递给另一个函数,或从其它函数返回,这种函数叫做first class函数。在 JavaScript 中,所有函数都是first class函数。", + "将函数为参数或返回值的函数叫做higher order函数。", + "当函数传递给另一个函数或从另一个函数返回时,那些传入或返回的函数可以叫做lambda。", "
", - "Prepare 27 cups of green tea and 13 cups of black tea and store them in tea4GreenTeamFCC and tea4BlackTeamFCC variables, respectively. Note that the getTea function has been modified so it now takes a function as the first argument.", - "Note: The data (the number of cups of tea) is supplied as the last argument. We'll discuss this more in later lessons." + "准备 27 杯绿茶和 13 杯红茶,分别存入tea4GreenTeamFCCtea4BlackTeamFCC变量。请注意,getTea函数已经变了,现在它接收一个函数作为第一个参数。", + "注意:数据(茶的数量)作为最后一个参数。我们将在后面的课程中对此进行更多讨论。" ], "tests": [ { - "text": "The tea4GreenTeamFCC variable should hold 27 cups of green tea for the team.", - "testString": "assert(tea4GreenTeamFCC.length === 27, 'The tea4GreenTeamFCC variable should hold 27 cups of green tea for the team.');" + "text": "tea4GreenTeamFCC变量应存有为团队准备的 27 杯茶。", + "testString": "assert(tea4GreenTeamFCC.length === 27, 'tea4GreenTeamFCC变量应存有为团队准备的 27 杯茶。');" }, { - "text": "The tea4GreenTeamFCC variable should hold cups of green tea.", - "testString": "assert(tea4GreenTeamFCC[0] === 'greenTea', 'The tea4GreenTeamFCC variable should hold cups of green tea.');" + "text": "tea4GreenTeamFCC变量应存有绿茶。", + "testString": "assert(tea4GreenTeamFCC[0] === 'greenTea', 'tea4GreenTeamFCC变量应存有绿茶。');" }, { - "text": "The tea4BlackTeamFCC variable should hold 13 cups of black tea.", - "testString": "assert(tea4BlackTeamFCC.length === 13, 'The tea4BlackTeamFCC variable should hold 13 cups of black tea.');" + "text": "tea4BlackTeamFCC变量应存有 13 杯红茶。", + "testString": "assert(tea4BlackTeamFCC.length === 13, 'tea4BlackTeamFCC变量应存有 13 杯红茶。');" }, { - "text": "The tea4BlackTeamFCC variable should hold cups of black tea.", - "testString": "assert(tea4BlackTeamFCC[0] === 'blackTea', 'The tea4BlackTeamFCC variable should hold cups of black tea.');" + "text": "tea4BlackTeamFCC变量应存有红茶。", + "testString": "assert(tea4BlackTeamFCC[0] === 'blackTea', 'tea4BlackTeamFCC变量应存有红茶。');" } ], "solutions": [], @@ -115,22 +115,22 @@ "name": "index", "contents": [ "/**", - " * A long process to prepare green tea.", - " * @return {string} A cup of green tea.", + " * 绿茶准备过程", + " * @return {string} 一杯绿茶", " **/", "const prepareGreenTea = () => 'greenTea';", "", "/**", - " * A long process to prepare black tea.", - " * @return {string} A cup of black tea.", + " * 备红茶准备过程", + " * @return {string} 一杯红茶", " **/", "const prepareBlackTea = () => 'blackTea';", "", "/**", - " * Get given number of cups of tea.", - " * @param {function():string} prepareTea The type of tea preparing function.", - " * @param {number} numOfCups Number of required cups of tea.", - " * @return {Array} Given amount of tea cups.", + " * 获得一定数量的茶", + " * @param {function():string} prepareTea 茶的类型", + " * @param {number} numOfCups 需要茶的数量", + " * @return {Array} 给定的茶", " **/", "const getTea = (prepareTea, numOfCups) => {", " const teaCups = [];", @@ -143,12 +143,12 @@ " return teaCups;", "};", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "", "const tea4GreenTeamFCC = null; // :(", "const tea4BlackTeamFCC = null; // :(", "", - "// Add your code above this line", + "// 请在本行以上添加你的代码", "", "console.log(", " tea4GreenTeamFCC,", @@ -164,17 +164,17 @@ "id": "587d7b8e367417b2b2512b5d", "title": "Understand the Hazards of Using Imperative Code", "description": [ - "Functional programming is a good habit. It keeps your code easy to manage, and saves you from sneaky bugs. But before we get there, let's look at an imperative approach to programming to highlight where you may have issues.", - "In English (and many other languages), the imperative tense is used to give commands. Similarly, an imperative style in programming is one that gives the computer a set of statements to perform a task.", - "Often the statements change the state of the program, like updating global variables. A classic example is writing a for loop that gives exact directions to iterate over the indices of an array.", - "In contrast, functional programming is a form of declarative programming. You tell the computer what you want done by calling a method or function.", - "JavaScript offers many predefined methods that handle common tasks so you don't need to write out how the computer should perform them. For example, instead of using the for loop mentioned above, you could call the map method which handles the details of iterating over an array. This helps to avoid semantic errors, like the \"Off By One Errors\" that were covered in the Debugging section.", - "Consider the scenario: you are browsing the web in your browser, and want to track the tabs you have opened. Let's try to model this using some simple object-oriented code.", - "A Window object is made up of tabs, and you usually have more than one Window open. The titles of each open site in each Window object is held in an array. After working in the browser (opening new tabs, merging windows, and closing tabs), you want to print the tabs that are still open. Closed tabs are removed from the array and new tabs (for simplicity) get added to the end of it.", - "The code editor shows an implementation of this functionality with functions for tabOpen(), tabClose(), and join(). The array tabs is part of the Window object that stores the name of the open pages.", - "

Instructions

", - "Run the code in the editor. It's using a method that has side effects in the program, causing incorrect output. The final list of open tabs should be ['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab'] but the output will be slightly different.", - "Work through the code and see if you can figure out the problem, then advance to the next challenge to learn more." + "函数式编程是一种好习惯,它能让代码管理更简单,不受隐藏 bug 影响。在我们开始函数式编程之前,为了更好的突显可能遇到的问题,我们先看看命令式编程。", + "类似在英语(和许多其他语言)中,命令式时态用于给出命令,编程中的命令式是给计算机一组语句来执行任务。", + "这些语句通常会改变程序的状态,例如更新全局变量,典型的例子就是写一个for循环,它给出了迭代数组索引的精确方向。", + "相反,函数式编程是声明式编程的一种形式,通过调用方法或函数来告诉计算机要做什么。", + "JavaScript 提供了许多处理常见任务的方法,所以你无需写出计算机应如何执行它们。例如,你可以用map函数替代上面提到的for循环来处理数组迭代。这有助于避免语义错误,如调试章节介绍的\"Off By One Errors\"。", + "考虑这样的场景:你正在浏览器中浏览网页,并想操作你打开的标签。下面我们来试试用面向对象的思路来描述这种情景。", + "窗口对象由选项卡组成,通常会打开多个窗口。窗口对象中每个打开网站的标题都保存在一个数组中。在对浏览器进行了如打开新标签、合并窗口、关闭标签之类的操作后,你需要输出所有打开的标签。关掉的标签将从数组中删除,新打开的标签(为简单起见)则添加到数组的末尾。", + "代码编辑器中显示了此功能的实现,其中包含tabOpen()tabClose(),和 join()函数。tabs数组是窗口对象的一部分用于储存打开页面的名称。", + "

说明

", + "在编辑器中运行代码。它使用了有副作用的方法,导致输出错误。打开标签的最终列表应该是['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']但输出会略有不同。", + "仔细阅读代码,看看你是否能找出问题所在,然后进入下一个挑战,了解更多信息。" ], "tests": [ { @@ -191,41 +191,41 @@ "ext": "js", "name": "index", "contents": [ - "// tabs is an array of titles of each site open within the window", + "// tabs 是窗口中打开网站的标题数组", "var Window = function(tabs) {", - " this.tabs = tabs; // we keep a record of the array inside the object", + " this.tabs = tabs; // 我们将数组记录在对象内部", "};", "", - "// When you join two windows into one window", + "// 当两个窗口合并成一个窗口时", "Window.prototype.join = function (otherWindow) {", " this.tabs = this.tabs.concat(otherWindow.tabs);", " return this;", "};", "", - "// When you open a new tab at the end", + "// 在末尾打开一个新标签", "Window.prototype.tabOpen = function (tab) {", - " this.tabs.push('new tab'); // let's open a new tab for now", + " this.tabs.push('new tab'); // 打开一个新的标签", " return this;", "};", "", - "// When you close a tab", + "// 关闭一个标签", "Window.prototype.tabClose = function (index) {", - " var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab", - " var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab", + " var tabsBeforeIndex = this.tabs.splice(0, index); // 获取前面的标签", + " var tabsAfterIndex = this.tabs.splice(index); // 获取后面的标签", "", - " this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together ", + " this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // 拼接到一起", " return this;", " };", "", - "// Let's create three browser windows", - "var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites", - "var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites", - "var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites", + "// 创建三个浏览器窗口", + "var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // 邮箱、文档及其他与工作相关的网站", + "var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // 社交网站", + "var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // 娱乐网站", "", - "// Now perform the tab opening, closing, and other operations", + "// 执行标签打开,关闭和其他操作", "var finalTabs = socialWindow", - " .tabOpen() // Open a new tab for cat memes", - " .join(videoWindow.tabClose(2)) // Close third tab in video window, and join", + " .tabOpen() // 新开一个 cat memes 的标签", + " .join(videoWindow.tabClose(2)) // 在娱乐网站关闭第三个标签,加入数组", " .join(workWindow.tabClose(1).tabOpen());", "", "alert(finalTabs.tabs);" @@ -239,23 +239,23 @@ "id": "587d7b8e367417b2b2512b5e", "title": "Avoid Mutations and Side Effects Using Functional Programming", "description": [ - "If you haven't already figured it out, the issue in the previous challenge was with the splice call in the tabClose() function. Unfortunately, splice changes the original array it is called on, so the second call to it used a modified array, and gave unexpected results.", - "This is a small example of a much larger pattern - you call a function on a variable, array, or an object, and the function changes the variable or something in the object.", - "One of the core principle of functional programming is to not change things. Changes lead to bugs. It's easier to prevent bugs knowing that your functions don't change anything, including the function arguments or any global variable.", - "The previous example didn't have any complicated operations but the splice method changed the original array, and resulted in a bug.", - "Recall that in functional programming, changing or altering things is called mutation, and the outcome is called a side effect. A function, ideally, should be a pure function, meaning that it does not cause any side effects.", - "Let's try to master this discipline and not alter any variable or object in our code.", + "如果你还没想通,上一个挑战的问题出在tabClose()函数里的splice。不幸的是,splice修改了调用它的原始数组,所以第二次调用它时是基于修改后的数组,才给出了意料之外的结果。", + "这是一个小例子,还有更广义的定义——你在变量,数组或对象上调用一个函数,这个会函数改变对象中的变量或其他东西。", + "函数式编程的核心原则之一是不改变任何东西。变化会导致错误。如果一个函数不改变传入的参数、全局变量等数据,那么它造成问题的可能性就会小很多。", + "前面的例子没有任何复杂的操作,但是splice方法改变了原始数组,导致 bug 产生。", + "回想一下,在函数式编程中,改变或变更叫做mutation,这种改变的结果叫做“副作用”(side effect)。理想情况下,函数应该是不会产生任何副作用的pure function。", + "让我们尝试掌握这个原则:不要改变代码中的任何变量或对象。", "
", - "Fill in the code for the function incrementer so it returns the value of the global variable fixedValue increased by one." + "填写incrementer函数的代码,使其返回全局变量fixedValue的值增加 1。" ], "tests": [ { - "text": "Your function incrementer should not change the value of fixedValue.", - "testString": "assert(fixedValue === 4, 'Your function incrementer should not change the value of fixedValue.');" + "text": "incrementer函数不能改变fixedValue的值。", + "testString": "assert(fixedValue === 4, '你的incrementer函数不能改变fixedValue的值。');" }, { - "text": "Your incrementer function should return a value that is one larger than the fixedValue value.", - "testString": "assert(newValue === 5, 'Your incrementer function should return a value that is one larger than the fixedValue value.');" + "text": "incrementer函数应返回比fixedValue变量更大的值。", + "testString": "assert(newValue === 5, 'incrementer函数应返回比fixedValue变量更大的值。');" } ], "solutions": [], @@ -267,18 +267,18 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var fixedValue = 4;", "", "function incrementer () {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "", - "var newValue = incrementer(); // Should equal 5", - "console.log(fixedValue); // Should print 4" + "var newValue = incrementer(); // 应等于 5", + "console.log(fixedValue); // 应打印 4" ], "head": [], "tail": [] @@ -289,28 +289,28 @@ "id": "587d7b8e367417b2b2512b5f", "title": "Pass Arguments to Avoid External Dependence in a Function", "description": [ - "The last challenge was a step closer to functional programming principles, but there is still something missing.", - "We didn't alter the global variable value, but the function incrementer would not work without the global variable fixedValue being there.", - "Another principle of functional programming is to always declare your dependencies explicitly. This means if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument.", - "There are several good consequences from this principle. The function is easier to test, you know exactly what input it takes, and it won't depend on anything else in your program.", - "This can give you more confidence when you alter, remove, or add new code. You would know what you can or cannot change and you can see where the potential traps are.", - "Finally, the function would always produce the same output for the same set of inputs, no matter what part of the code executes it.", + "最后一个挑战是更接近函数式编程原则的挑战,但是仍然缺少一些东西。", + "虽然我们没有改变全局变量值,但在没有全局变量fixedValue情况下,incrementer函数将不起作用。", + "函数式编程的另一个原则是:总是显式声明依赖关系。如果函数依赖于一个变量或对象,那么将该变量或对象作为参数直接传递到函数中。", + "这样做会有很多好处,其中一点是让函数更容易测试,因为你确切地知道参数是什么,并且这个参数也不依赖于程序中的任何其他内容。", + "其次,这样做可以让你更加自信地更改,删除或添加新代码。因为你很清楚哪些是可以改的,哪些是不可以改的,这样你就知道哪里可能会有潜在的陷阱。", + "最后,无论代码的哪一部分执行它,函数总是会为同一组输入生成相同的输出。", "
", - "Let's update the incrementer function to clearly declare its dependencies.", - "Write the incrementer function so it takes an argument, and then increases the value by one." + "更新incrementer函数,明确声明其依赖项。", + "编写incrementer函数,获取它的参数,然后将值增加 1。" ], "tests": [ { - "text": "Your function incrementer should not change the value of fixedValue.", - "testString": "assert(fixedValue === 4, 'Your function incrementer should not change the value of fixedValue.');" + "text": "incrementer函数不能修改fixedValue的值。", + "testString": "assert(fixedValue === 4, 'incrementer函数不能修改fixedValue的值。');" }, { - "text": "Your incrementer function should take a parameter.", - "testString": "assert(code.match(/function\\s+?incrementer\\s*?\\(.+?\\)/g), 'Your incrementer function should take a parameter.');" + "text": "incrementer函数应该接收一个参数。", + "testString": "assert(code.match(/function\\s+?incrementer\\s*?\\(.+?\\)/g), 'incrementer函数应该接收一个参数。');" }, { - "text": "Your incrementer function should return a value that is one larger than the fixedValue value.", - "testString": "assert(newValue === 5, 'Your incrementer function should return a value that is one larger than the fixedValue value.');" + "text": "incrementer函数应返回比fixedValue更大的值。", + "testString": "assert(newValue === 5, 'incrementer函数应返回比fixedValue更大的值。');" } ], "solutions": [], @@ -322,18 +322,18 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var fixedValue = 4;", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "function incrementer () {", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "", - "var newValue = incrementer(fixedValue); // Should equal 5", - "console.log(fixedValue); // Should print 4" + "var newValue = incrementer(fixedValue); // 应等于 5", + "console.log(fixedValue); // 应打印 4" ], "head": [], "tail": [] @@ -344,29 +344,29 @@ "id": "587d7b8f367417b2b2512b60", "title": "Refactor Global Variables Out of Functions", "description": [ - "So far, we have seen two distinct principles for functional programming:", - "1) Don't alter a variable or object - create new variables and objects and return them if need be from a function.", - "2) Declare function arguments - any computation inside a function depends only on the arguments, and not on any global object or variable.", - "Adding one to a number is not very exciting, but we can apply these principles when working with arrays or more complex objects.", + "目前为止,我们已经看到了函数式编程的两个不同原则:", + "1) 不要更改变量或对象——创建新变量和对象,并在需要时从函数返回它们。", + "2) 声明函数参数——函数内的任何计算仅取决于参数,而不取决于任何全局对象或变量。", + "给数字加 1 不够刺激,我们可以在处理数组或更复杂的对象时应用这些原则。", "
", - "Refactor (rewrite) the code so the global array bookList is not changed inside either function. The add function should add the given bookName to the end of an array. The remove function should remove the given bookName from an array. Both functions should return an array, and any new parameters should be added before the bookName one." + "重构代码,使全局数组bookList在任何函数内部都不会被改变。add函数可以将指定的bookName增加到数组末尾。remove函数可以从数组中移除指定bookName。两个函数都返回数组,并且任何参数都应该添加到bookName前面。" ], "tests": [ { - "text": "bookList should not change and still equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", - "testString": "assert(JSON.stringify(bookList) === JSON.stringify([\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]), 'bookList should not change and still equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" + "text": "bookList不应该改变,应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", + "testString": "assert(JSON.stringify(bookList) === JSON.stringify([\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]), 'bookList不应该改变,应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" }, { - "text": "newBookList should equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", - "testString": "assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newBookList should equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" + "text": "newBookList应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", + "testString": "assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newBookList应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" }, { - "text": "newerBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", - "testString": "assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae']), 'newerBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" + "text": "newerBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", + "testString": "assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae']), 'newerBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" }, { - "text": "newestBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", - "testString": "assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newestBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" + "text": "newestBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", + "testString": "assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newestBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" } ], "solutions": [], @@ -382,30 +382,30 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var bookList = [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"];", "", - "/* This function should add a book to the list and return the list */", - "// New parameters should come before the bookName one", + "/* 这个函数应该增加一本书到列表中,并返回这个列表 */", + "// 新参数应在 bookName 之前", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "function add (bookName) {", " ", " return bookList.push(bookName);", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "", - "/* This function should remove a book from the list and return the list */", - "// New parameters should come before the bookName one", + "/* 这个函数应该移除一本书到列表中,并返回这个列表 */", + "// 新参数应在 bookName 之前", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "function remove (bookName) {", " if (bookList.indexOf(bookName) >= 0) {", " ", " return bookList.splice(0, 1, bookName);", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", " }", "}", "", @@ -424,30 +424,30 @@ "id": "587d7b8f367417b2b2512b61", "title": "Use the map Method to Extract Data from an Array", "description": [ - "So far we have learned to use pure functions to avoid side effects in a program. Also, we have seen the value in having a function only depend on its input arguments.", - "This is only the beginning. As its name suggests, functional programming is centered around a theory of functions.", - "It would make sense to be able to pass them as arguments to other functions, and return a function from another function. Functions are considered First Class Objects in JavaScript, which means they can be used like any other object. They can be saved in variables, stored in an object, or passed as function arguments.", - "Let's start with some simple array functions, which are methods on the array object prototype. In this exercise we are looking at Array.prototype.map(), or more simply map.", - "Remember that the map method is a way to iterate over each item in an array. It creates a new array (without changing the original one) after applying a callback function to every element.", + "目前为止,我们已经学会了使用纯函数来避免程序中的副作用。此外,我们已经看到函数的值仅取决于其输入参数。", + "这仅仅是个开始。顾名思义,函数式编程以函数理论为中心。", + "能够将它们作为参数传递给其他函数,和从另一个函数返回一个函数是有意义的。函数在 JavaScript 中被视为First Class Objects,它们可以像任何其他对象一样使用。它们可以保存在变量中,存储在对象中,也可以作为函数参数传递。", + "让我们从一些简单的数组函数开始,这些函数是数组对象原型上的方法。在本练习中,我们来了解下数组的map方法(即Array.prototype.map())。", + "请记住,map方法是迭代数组中每一项的方式之一。在对每个元素应用回调函数后,它会创建一个新数组(不改变原来的数组)。", "
", - "The watchList array holds objects with information on several movies. Use map to pull the title and rating from watchList and save the new array in the rating variable. The code in the editor currently uses a for loop to do this, replace the loop functionality with your map expression." + "watchList数组保存了包含一些电影信息的对象。使用mapwatchList中提取标题(title)和评分(rating),并将新数组保存在rating变量里。目前编辑器中的代码是使用for循环实现,使用map表达式替换循环功能。" ], "tests": [ { - "text": "The watchList variable should not change.", - "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList variable should not change.');" + "text": "watchList不能被改变", + "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList不能被改变');" }, { - "text": "Your code should not use a for loop.", - "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), 'Your code should not use a for loop.');" + "text": "你的代码不能使用for循环。", + "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), '你的代码不能使用for循环。');" }, { - "text": "Your code should use the map method.", - "testString": "assert(code.match(/\\.map/g), 'Your code should use the map method.');" + "text": "你的代码应使用map方法。", + "testString": "assert(code.match(/\\.map/g), '你的代码应使用map方法。');" }, { - "text": "rating should equal [{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].", - "testString": "assert(JSON.stringify(rating) === JSON.stringify([{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]), 'rating should equal [{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].');" + "text": "rating应等于[{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].", + "testString": "assert(JSON.stringify(rating) === JSON.stringify([{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]), 'rating应等于[{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].');" } ], "solutions": [], @@ -459,7 +459,7 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var watchList = [", " { ", " \"Title\": \"Inception\",", @@ -573,14 +573,14 @@ " }", "];", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "", "var rating = [];", "for(var i=0; i < watchList.length; i++){", " rating.push({title: watchList[i][\"Title\"], rating: watchList[i][\"imdbRating\"]});", "}", "", - "// Add your code above this line", + "// 请在本行以上添加你的代码", "", "console.log(rating); " ], @@ -593,21 +593,21 @@ "id": "587d7b8f367417b2b2512b62", "title": "Implement map on a Prototype", "description": [ - "As you have seen from applying Array.prototype.map(), or simply map() earlier, the map method returns an array of the same length as the one it was called on. It also doesn't alter the original array, as long as its callback function doesn't.", - "In other words, map is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.", - "It would teach us a lot about map to try to implement a version of it that behaves exactly like the Array.prototype.map() with a for loop or Array.prototype.forEach().", - "Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.", + "我们之前用map方法(即Array.prototype.map())返回一个与调用它的数组长度相同的数组。只要它的回调函数不改变原始数组,它就不会改变原始数组。", + "换句话说,map是一个纯函数,它的输出仅取决于输入的数组和作为参数传入的回调函数。", + "为了加深对map方法的理解,现在我们来用forArray.prototype.forEach()自己实现一下这个方法。", + "注意:纯函数可以改变其作用域内定义的局部变量,但我们最好不要这样做。", "
", - "Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map(). You may use a for loop or the forEach method." + "写一个和Array.prototype.map()一样的Array.prototype.myMap()。你可以用for循环或者forEach方法。" ], "tests": [ { - "text": "new_s should equal [46, 130, 196, 10].", - "testString": "assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]), 'new_s should equal [46, 130, 196, 10].');" + "text": "new_s应等于[46, 130, 196, 10]。", + "testString": "assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]), 'new_s应等于[46, 130, 196, 10]。');" }, { - "text": "Your code should not use the map method.", - "testString": "assert(!code.match(/\\.map/g), 'Your code should not use the map method.');" + "text": "不能使用map方法。", + "testString": "assert(!code.match(/\\.map/g), '不能使用map方法。');" } ], "solutions": [], @@ -619,14 +619,14 @@ "ext": "js", "name": "index", "contents": [ - "// the global Array", + "// 全局变量", "var s = [23, 65, 98, 5];", "", "Array.prototype.myMap = function(callback){", " var newArray = [];", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", " return newArray;", "", "};", @@ -644,27 +644,27 @@ "id": "587d7b8f367417b2b2512b63", "title": "Use the filter Method to Extract Data from an Array", "description": [ - "Another useful array function is Array.prototype.filter(), or simply filter(). The filter method returns a new array which is at most as long as the original array, but usually has fewer items.", - "Filter doesn't alter the original array, just like map. It takes a callback function that applies the logic inside the callback on each element of the array. If an element returns true based on the criteria in the callback function, then it is included in the new array.", + "另一个有用的数组方法是filter()(即Array.prototype.filter())。filter方法会返回一个长度不大于原始数组的新数组。", + "和map一样,Filter不会改变原始数组,它接收一个回调函数,将回调内的逻辑应用于数组的每个元素。新数组包含根据回调函数内条件返回 true 的元素。", "
", - "The variable watchList holds an array of objects with information on several movies. Use a combination of filter and map to return a new array of objects with only title and rating keys, but where imdbRating is greater than or equal to 8.0. Note that the rating values are saved as strings in the object and you may want to convert them into numbers to perform mathematical operations on them." + "watchList是包含一些电影信息的对象。结合filtermap返回一个只包含titlerating属性的新数组,并且imdbRating值大于或等于 8.0。请注意,评级值在对象中保存为字符串,你可能需要将它转换成数字来执行运算。" ], "tests": [ { - "text": "The watchList variable should not change.", - "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList variable should not change.');" + "text": "watchList应保持不变。", + "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList应保持不变。');" }, { - "text": "Your code should use the filter method.", - "testString": "assert(code.match(/\\.filter/g), 'Your code should use the filter method.');" + "text": "应使用filter方法。", + "testString": "assert(code.match(/\\.filter/g), '应使用filter方法。');" }, { - "text": "Your code should not use a for loop.", - "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), 'Your code should not use a for loop.');" + "text": "不能使用for循环。", + "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), '不能使用for循环。');" }, { - "text": "filteredList should equal [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}].", - "testString": "assert.deepEqual(filteredList, [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}], 'filteredList should equal [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}].');" + "text": "filteredList应等于[{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]。", + "testString": "assert.deepEqual(filteredList, [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}], 'filteredList应等于[{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]。');" } ], "solutions": [], @@ -676,7 +676,7 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var watchList = [", " { ", " \"Title\": \"Inception\",", @@ -790,11 +790,11 @@ " }", "];", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "", "var filteredList;", "", - "// Add your code above this line", + "// 请在本行以上添加你的代码", "", "console.log(filteredList); " ], @@ -807,19 +807,20 @@ "id": "587d7b8f367417b2b2512b64", "title": "Implement the filter Method on a Prototype", "description": [ - "It would teach us a lot about the filter method if we try to implement a version of it that behaves exactly like Array.prototype.filter(). It can use either a for loop or Array.prototype.forEach().", - "Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.", + "为了加深对filter的理解,现在我们来自己实现一下Array.prototype.filter()方法。", + "可以用for循环或Array.prototype.forEach()。", + "请注意:纯函数可以改变其作用域内定义的局部变量,但我们最好不要这样做。", "
", - "Write your own Array.prototype.myFilter(), which should behave exactly like Array.prototype.filter(). You may use a for loop or the Array.prototype.forEach() method." + "编写一个和Array.prototype.filter()功能一样的Array.prototype.myFilter()方法。你可以用for循环或Array.prototype.forEach()方法。" ], "tests": [ { - "text": "new_s should equal [23, 65, 5].", - "testString": "assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]), 'new_s should equal [23, 65, 5].');" + "text": "new_s应等于[23, 65, 5]。", + "testString": "assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]), 'new_s应等于[23, 65, 5]。');" }, { - "text": "Your code should not use the filter method.", - "testString": "assert(!code.match(/\\.filter/g), 'Your code should not use the filter method.');" + "text": "不能使用filter方法。", + "testString": "assert(!code.match(/\\.filter/g), '不能使用filter方法。');" } ], "solutions": [], @@ -831,14 +832,14 @@ "ext": "js", "name": "index", "contents": [ - "// the global Array", + "// 全局变量", "var s = [23, 65, 98, 5];", "", "Array.prototype.myFilter = function(callback){", " var newArray = [];", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", " return newArray;", "", "};", @@ -856,32 +857,32 @@ "id": "587d7b90367417b2b2512b65", "title": "Return Part of an Array Using the slice Method", "description": [ - "The slice method returns a copy of certain elements of an array. It can take two arguments, the first gives the index of where to begin the slice, the second is the index for where to end the slice (and it's non-inclusive). If the arguments are not provided, the default is to start at the beginning of the array through the end, which is an easy way to make a copy of the entire array. The slice method does not mutate the original array, but returns a new one.", - "Here's an example:", - "
var arr = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\"];
var newArray = arr.slice(1, 3);
// Sets newArray to [\"Dog\", \"Tiger\"]
", + "slice方法可以从已有数组中返回指定元素。它接受两个参数,第一个规定从何处开始选取,第二个规定从何处结束选取(不包括该元素)。如果没有传参,则默认为从数组的开头开始到结尾结束,这是复制整个数组的简单方式。slice返回一个新数组,不会修改原始数组。", + "举个例子:", + "
var arr = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\"];
var newArray = arr.slice(1, 3);
// 将新数组设置为 [\"Dog\", \"Tiger\"]
", "
", - "Use the slice method in the sliceArray function to return part of the anim array given the provided beginSlice and endSlice indices. The function should return an array." + "在sliceArray函数中使用slice方法,给出beginSliceendSlice索引,返回anim数组的一部分,这个函数应返回一个数组。" ], "tests": [ { - "text": "Your code should use the slice method.", - "testString": "assert(code.match(/\\.slice/g), 'Your code should use the slice method.');" + "text": "你的代码中应使用slice方法。", + "testString": "assert(code.match(/\\.slice/g), '你的代码中应使用slice方法。');" }, { - "text": "The inputAnim variable should not change.", - "testString": "assert(JSON.stringify(inputAnim) === JSON.stringify([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"]), 'The inputAnim variable should not change.');" + "text": "不能改变inputAnim变量。", + "testString": "assert(JSON.stringify(inputAnim) === JSON.stringify([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"]), '不能改变inputAnim变量。');" }, { - "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3) should return [\"Dog\", \"Tiger\"].", - "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)) === JSON.stringify([\"Dog\", \"Tiger\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3) should return [\"Dog\", \"Tiger\"].');" + "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)应返回[\"Dog\", \"Tiger\"]。", + "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)) === JSON.stringify([\"Dog\", \"Tiger\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)应返回[\"Dog\", \"Tiger\"]。');" }, { - "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1) should return [\"Cat\"].", - "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)) === JSON.stringify([\"Cat\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1) should return [\"Cat\"].');" + "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)应返回[\"Cat\"]。", + "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)) === JSON.stringify([\"Cat\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)应返回[\"Cat\"]。');" }, { - "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4) should return [\"Dog\", \"Tiger\", \"Zebra\"].", - "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)) === JSON.stringify([\"Dog\", \"Tiger\", \"Zebra\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4) should return [\"Dog\", \"Tiger\", \"Zebra\"].');" + "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)应返回[\"Dog\", \"Tiger\", \"Zebra\"]。", + "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)) === JSON.stringify([\"Dog\", \"Tiger\", \"Zebra\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)应返回[\"Dog\", \"Tiger\", \"Zebra\"]。');" } ], "solutions": [], @@ -894,10 +895,10 @@ "name": "index", "contents": [ "function sliceArray(anim, beginSlice, endSlice) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "var inputAnim = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"];", "sliceArray(inputAnim, 1, 3);" @@ -911,29 +912,29 @@ "id": "9d7123c8c441eeafaeb5bdef", "title": "Remove Elements from an Array Using slice Instead of splice", "description": [ - "A common pattern while working with arrays is when you want to remove items and keep the rest of the array. JavaScript offers the splice method for this, which takes arguments for the index of where to start removing items, then the number of items to remove. If the second argument is not provided, the default is to remove items through the end. However, the splice method mutates the original array it is called on. Here's an example:", - "
var cities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];
cities.splice(3, 1); // Returns \"London\" and deletes it from the cities array
// cities is now [\"Chicago\", \"Delhi\", \"Islamabad\", \"Berlin\"]
", - "As we saw in the last challenge, the slice method does not mutate the original array, but returns a new one which can be saved into a variable. Recall that the slice method takes two arguments for the indices to begin and end the slice (the end is non-inclusive), and returns those items in a new array. Using the slice method instead of splice helps to avoid any array-mutating side effects.", + "使用数组时经常遇到要删除一些元素并保留数组剩余部分的情况。为此,JavaScript 提供了splice方法,它接收从哪里开始删除项目的索引,和要删除的项目数两个参数。如果没有提供第二个参数,默认情况下是移除到结尾的元素。但splice方法会改变调用它的原始数组。举个例子:", + "
var cities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];
cities.splice(3, 1); // 返回 \"London\" 并将它从 cities 数组删除
// cities 现在是 [\"Chicago\", \"Delhi\", \"Islamabad\", \"Berlin\"]
", + "正如我们在上一次挑战中看到的那样,slice方法不会改变原始数组,而是返回一个可以保存到变量中的新数组。回想一下,slice方法接收两个参数,从开始索引开始选取到结束(不包括该元素),并在新数组中返回这些元素。使用slice方法替代splice有助于避免数组变化产生的副作用。", "
", - "Rewrite the function nonMutatingSplice by using slice instead of splice. It should limit the provided cities array to a length of 3, and return a new array with only the first three items.", - "Do not mutate the original array provided to the function." + "用slice代替splice重写nonMutatingSplice函数。将cities数组长度限制为3,并返回一个仅包含前 3 项的新数组。", + "不要改变提供给函数的原始数组。" ], "tests": [ { - "text": "Your code should use the slice method.", - "testString": "assert(code.match(/\\.slice/g), 'Your code should use the slice method.');" + "text": "你应该使用slice方法。", + "testString": "assert(code.match(/\\.slice/g), '你应该使用slice方法。');" }, { - "text": "Your code should not use the splice method.", - "testString": "assert(!code.match(/\\.splice/g), 'Your code should not use the splice method.');" + "text": "你不应该使用splice方法。", + "testString": "assert(!code.match(/\\.splice/g), '你不应该使用splice方法。;" }, { - "text": "The inputCities array should not change.", - "testString": "assert(JSON.stringify(inputCities) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]), 'The inputCities array should not change.');" + "text": "不能改变inputCities数组。", + "testString": "assert(JSON.stringify(inputCities) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]), '不能改变inputCities数组。');" }, { - "text": "nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]) should return [\"Chicago\", \"Delhi\", \"Islamabad\"].", - "testString": "assert(JSON.stringify(nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\"]), 'nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]) should return [\"Chicago\", \"Delhi\", \"Islamabad\"].');" + "text": "nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])应返回[\"Chicago\", \"Delhi\", \"Islamabad\"]。", + "testString": "assert(JSON.stringify(nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\"]), 'nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])应返回[\"Chicago\", \"Delhi\", \"Islamabad\"]。');" } ], "solutions": [], @@ -946,10 +947,10 @@ "name": "index", "contents": [ "function nonMutatingSplice(cities) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " return cities.splice(3);", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "var inputCities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];", "nonMutatingSplice(inputCities);" @@ -963,27 +964,27 @@ "id": "587d7da9367417b2b2512b66", "title": "Combine Two Arrays Using the concat Method", "description": [ - "Concatenation means to join items end to end. JavaScript offers the concat method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to concat, which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:", - "
[1, 2, 3].concat([4, 5, 6]);
// Returns a new array [1, 2, 3, 4, 5, 6]
", + "Concatenation意思是连接元素到尾部。同理,JavaScript 为字符串和数组提供了concat方法。对数组来说,在一个数组上调用concat方法,然后提供另一个数组作为参数添加到第一个数组末尾,返回一个新数组,不会改变任何一个原始数组。举个例子:", + "
[1, 2, 3].concat([4, 5, 6]);
// 返回新数组 [1, 2, 3, 4, 5, 6]
", "
", - "Use the concat method in the nonMutatingConcat function to concatenate attach to the end of original. The function should return the concatenated array." + "在nonMutatingConcat函数里使用concat,将attach拼接到original尾部,返回拼接后的数组。" ], "tests": [ { - "text": "Your code should use the concat method.", - "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat method.');" + "text": "你应该使用concat方法。", + "testString": "assert(code.match(/\\.concat/g), '你应该使用concat方法。');" }, { - "text": "The first array should not change.", - "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The first array should not change.');" + "text": "不能改变first数组。", + "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), '不能改变first数组。');" }, { - "text": "The second array should not change.", - "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The second array should not change.');" + "text": "不能改变second数组。", + "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), '不能改变second数组。');" }, { - "text": "nonMutatingConcat([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].", - "testString": "assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingConcat([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].');" + "text": "nonMutatingConcat([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。", + "testString": "assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingConcat([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。');" } ], "solutions": [], @@ -996,10 +997,10 @@ "name": "index", "contents": [ "function nonMutatingConcat(original, attach) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "var first = [1, 2, 3];", "var second = [4, 5];", @@ -1014,33 +1015,33 @@ "id": "587d7da9367417b2b2512b67", "title": "Add Elements to the End of an Array Using concat Instead of push", "description": [ - "Functional programming is all about creating and using non-mutating functions.", - "The last challenge introduced the concat method as a way to combine arrays into a new one without mutating the original arrays. Compare concat to the push method. Push adds an item to the end of the same array it is called on, which mutates that array. Here's an example:", - "
var arr = [1, 2, 3];
arr.push([4, 5, 6]);
// arr is changed to [1, 2, 3, [4, 5, 6]]
// Not the functional programming way
", - "Concat offers a way to add new items to the end of an array without any mutating side effects.", + "函数式编程就是创建和使用 non-mutating 函数。", + "最后一个挑战介绍了concat方法,这是一种在不改变原始数组的前提下,将数组组合成新数组的方法。将concat方法与push方法做比较,Push将元素添加到调用它的数组的末尾,这样会改变该数组。举个例子:", + "
var arr = [1, 2, 3];
arr.push([4, 5, 6]);
// arr 变成了 [1, 2, 3, [4, 5, 6]]
// 不是函数式编程
", + "Concat方法可以将新项目添加到数组末尾,而不产生任何变更副作用。", "
", - "Change the nonMutatingPush function so it uses concat to add newItem to the end of original instead of push. The function should return an array." + "修改nonMutatingPush函数,用concat替代pushnewItem添加到original末尾,该函数应返回一个数组。" ], "tests": [ { - "text": "Your code should use the concat method.", - "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat method.');" + "text": "应该使用concat方法。", + "testString": "assert(code.match(/\\.concat/g), '应该使用concat方法。');" }, { - "text": "Your code should not use the push method.", - "testString": "assert(!code.match(/\\.push/g), 'Your code should not use the push method.');" + "text": "不能使用push方法。", + "testString": "assert(!code.match(/\\.push/g), '不能使用push方法。');" }, { - "text": "The first array should not change.", - "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The first array should not change.');" + "text": "不能改变first数组。", + "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), '不能改变first数组。');" }, { - "text": "The second array should not change.", - "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The second array should not change.');" + "text": "不能改变second数组。", + "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), '不能改变second数组。');" }, { - "text": "nonMutatingPush([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].", - "testString": "assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingPush([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].');" + "text": "nonMutatingPush([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。", + "testString": "assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingPush([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。');" } ], "solutions": [], @@ -1053,10 +1054,10 @@ "name": "index", "contents": [ "function nonMutatingPush(original, newItem) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " return original.push(newItem);", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "var first = [1, 2, 3];", "var second = [4, 5];", @@ -1071,29 +1072,29 @@ "id": "587d7da9367417b2b2512b68", "title": "Use the reduce Method to Analyze Data", "description": [ - "Array.prototype.reduce(), or simply reduce(), is the most general of all array operations in JavaScript. You can solve almost any array processing problem using the reduce method.", - "This is not the case with the filter and map methods since they do not allow interaction between two different elements of the array. For example, if you want to compare elements of the array, or add them together, filter or map could not process that.", - "The reduce method allows for more general forms of array processing, and it's possible to show that both filter and map can be derived as a special application of reduce.", - "However, before we get there, let's practice using reduce first.", + "reduce()(即Array.prototype.reduce()),是 JavaScript 所有数组操作中最通用的方法。几乎可以用reduce方法解决所有数组处理问题。", + "filtermap方法不支持对数组中两个不同元素的交互。举个例子,如果你想把数组中的元素拿来比较或者相加,用filtermap是做不到的。", + "reduce方法允许更通用的数组处理方式,而且filtermap方法都可以当作是reduce的特殊实现。", + "然而,在我们介绍它们的特殊实现之前,我们先来练习使用reduce。", "
", - "The variable watchList holds an array of objects with information on several movies. Use reduce to find the average IMDB rating of the movies directed by Christopher Nolan. Recall from prior challenges how to filter data and map over it to pull what you need. You may need to create other variables, but save the final average into the variable averageRating. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations." + "watchList变量中包含一组存有多部电影信息对象。使用reduce查找由 Christopher Nolan 导演的电影directed by Christopher Nolan的 IMDB 评级。回想一下之前的挑战,如何filter数据,以及使用map来获取你想要的数据。你可能需要创建一些变量,但是请将最后的平均值保存到averageRating变量中。请注意,评级在对象中是字符串,需要将其转换为数字再用于数学运算。" ], "tests": [ { - "text": "The watchList variable should not change.", - "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList variable should not change.');" + "text": "watchList应保持不变。", + "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList应保持不变。');" }, { - "text": "Your code should use the reduce method.", - "testString": "assert(code.match(/\\.reduce/g), 'Your code should use the reduce method.');" + "text": "应该使用reduce方法。", + "testString": "assert(code.match(/\\.reduce/g), '应该使用reduce方法。');" }, { - "text": "The averageRating should equal 8.675.", - "testString": "assert(averageRating == 8.675, 'The averageRating should equal 8.675.');" + "text": "The averageRating应等于 8.675。", + "testString": "assert(averageRating == 8.675, 'The averageRating应等于 8.675。');" }, { - "text": "Your code should not use a for loop.", - "testString": "assert(!code.match(/for\\s*?\\(.*\\)/g), 'Your code should not use a for loop.');" + "text": "不能使用for循环。", + "testString": "assert(!code.match(/for\\s*?\\(.*\\)/g), '不能使用for循环。');" } ], "solutions": [], @@ -1109,7 +1110,7 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var watchList = [", " { ", " \"Title\": \"Inception\",", @@ -1223,11 +1224,11 @@ " }", "];", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "", "var averageRating;", "", - "// Add your code above this line", + "// 请在本行以上添加你的代码", "", "console.log(averageRating); " ], @@ -1240,29 +1241,29 @@ "id": "587d7da9367417b2b2512b69", "title": "Sort an Array Alphabetically using the sort Method", "description": [ - "The sort method sorts the elements of an array according to the callback function.", - "For example:", - "
function ascendingOrder(arr) {
  return arr.sort(function(a, b) {
    return a - b;
  });
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a < b;
  });
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b']
", - "Note: It's encouraged to provide a callback function to specify how to sort the array items. JavaScript's default sorting method is by string Unicode point value, which may return unexpected results.", + "sort方法可以根据回调函数对数组元素进行排序。", + "举个例子:", + "
function ascendingOrder(arr) {
  return arr.sort(function(a, b) {
    return a - b;
  });
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a < b;
  });
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// 返回 ['z', 's', 'l', 'h', 'b']
", + "注意:提倡使用回调函数来指定如何对数组项进行排序。JavaScript 的默认排序算法是按照 Unicode 字符编码排序的,所以可能会返回意料之外的结果。", "
", - "Use the sort method in the alphabeticalOrder function to sort the elements of arr in alphabetical order." + "在alphabeticalOrder函数中使用sort方法对arr中的元素按照字母顺序排列。" ], "tests": [ { - "text": "Your code should use the sort method.", - "testString": "assert(code.match(/\\.sort/g), 'Your code should use the sort method.');" + "text": "应该使用sort方法。", + "testString": "assert(code.match(/\\.sort/g), '应该使用sort方法。');" }, { - "text": "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]) should return [\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"].", - "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])) === JSON.stringify([\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]), 'alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]) should return [\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"].');" + "text": "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])应返回[\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]。", + "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])) === JSON.stringify([\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]), 'alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])应返回[\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]。');" }, { - "text": "alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"]) should return [\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"].", - "testString": "assert(JSON.stringify(alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])) === JSON.stringify([\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]), 'alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"]) should return [\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"].');" + "text": "alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])应返回[\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]。", + "testString": "assert(JSON.stringify(alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])) === JSON.stringify([\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]), 'alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])应返回[\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]。');" }, { - "text": "alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"]) should return [\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"].", - "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])) === JSON.stringify([\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]), 'alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"]) should return [\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"].');" + "text": "alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])应返回[\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]。", + "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])) === JSON.stringify([\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]), 'alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])应返回[\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]。');" } ], "solutions": [], @@ -1275,10 +1276,10 @@ "name": "index", "contents": [ "function alphabeticalOrder(arr) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]);" ], @@ -1291,26 +1292,26 @@ "id": "587d7da9367417b2b2512b6a", "title": "Return a Sorted Array Without Changing the Original Array", "description": [ - "A side effect of the sort method is that it changes the order of the elements in the original array. In other words, it mutates the array in place. One way to avoid this is to first concatenate an empty array to the one being sorted (remember that concat returns a new array), then run the sort method.", + "sort方法会产生改变原始数组中元素顺序的副作用。换句话说,它会改变数组的位置。避免这种情况的一种方法是先将空数组连接到正在排序的数组上(记住concat返回一个新数组),再用sort方法。", "
", - "Use the sort method in the nonMutatingSort function to sort the elements of an array in ascending order. The function should return a new array, and not mutate the globalArray variable." + "在nonMutatingSort函数中使用sort方法对数组中的元素按升序进行排列。函数不能改变globalArray变量,应返回一个新数组。" ], "tests": [ { - "text": "Your code should use the sort method.", - "testString": "assert(code.match(/\\.sort/g), 'Your code should use the sort method.');" + "text": "应该使用sort方法。", + "testString": "assert(code.match(/\\.sort/g), '应该使用sort方法。');" }, { - "text": "Your code should use the concat method.", - "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat method.');" + "text": "应该使用concat方法。", + "testString": "assert(code.match(/\\.concat/g), '应该使用concat方法。');" }, { - "text": "The globalArray variable should not change.", - "testString": "assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]), 'The globalArray variable should not change.');" + "text": "globalArray variable 应保持不变。", + "testString": "assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]), 'globalArray variable 应保持不变。');" }, { - "text": "nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].", - "testString": "assert(JSON.stringify(nonMutatingSort(globalArray)) === JSON.stringify([2, 3, 5, 6, 9]), 'nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].');" + "text": "nonMutatingSort(globalArray)应返回[2, 3, 5, 6, 9]。", + "testString": "assert(JSON.stringify(nonMutatingSort(globalArray)) === JSON.stringify([2, 3, 5, 6, 9]), 'nonMutatingSort(globalArray)应返回[2, 3, 5, 6, 9]。');" } ], "solutions": [], @@ -1324,10 +1325,10 @@ "contents": [ "var globalArray = [5, 6, 3, 2, 9];", "function nonMutatingSort(arr) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "nonMutatingSort(globalArray);" ], @@ -1340,29 +1341,29 @@ "id": "587d7daa367417b2b2512b6b", "title": "Split a String into an Array Using the split Method", "description": [ - "The split method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.", - "Here are two examples that split one string by spaces, then another by digits using a regular expression:", + "split方法用于把字符串分割成字符串数组。接收一个分隔符参数,分隔符可以是用于分解字符串或正则表达式的字符。举个例子,如果分隔符是空格,你会得到一个单词数组;如果分隔符是空字符串,你会得到一个由字符串中每个字符组成的数组。", + "下面是两个用空格分隔一个字符串的例子,另一个是用数字的正则表达式分隔:", "
var str = \"Hello World\";
var bySpace = str.split(\" \");
// Sets bySpace to [\"Hello\", \"World\"]

var otherString = \"How9are7you2today\";
var byDigits = str.split(/\\d/);
// Sets byDigits to [\"How\", \"are\", \"you\", \"today\"]
", - "Since strings are immutable, the split method makes it easier to work with them.", + "因为字符串是固定的,split方法可以更简单的操作它们。", "
", - "Use the split method inside the splitify function to split str into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation." + "在splitify函数中用split方法将str分割成单词数组,这个方法应该返回一个数组。单词不一定都是用空格分隔,所以数组中不应包含标点符号。" ], "tests": [ { - "text": "Your code should use the split method.", - "testString": "assert(code.match(/\\.split/g), 'Your code should use the split method.');" + "text": "应该使用split方法。", + "testString": "assert(code.match(/\\.split/g), '应该使用split方法。');" }, { - "text": "splitify(\"Hello World,I-am code\") should return [\"Hello\", \"World\", \"I\", \"am\", \"code\"].", - "testString": "assert(JSON.stringify(splitify(\"Hello World,I-am code\")) === JSON.stringify([\"Hello\", \"World\", \"I\", \"am\", \"code\"]), 'splitify(\"Hello World,I-am code\") should return [\"Hello\", \"World\", \"I\", \"am\", \"code\"].');" + "text": "splitify(\"Hello World,I-am code\")应返回[\"Hello\", \"World\", \"I\", \"am\", \"code\"]。", + "testString": "assert(JSON.stringify(splitify(\"Hello World,I-am code\")) === JSON.stringify([\"Hello\", \"World\", \"I\", \"am\", \"code\"]), 'splitify(\"Hello World,I-am code\")应返回[\"Hello\", \"World\", \"I\", \"am\", \"code\"]。');" }, { - "text": "splitify(\"Earth-is-our home\") should return [\"Earth\", \"is\", \"our\", \"home\"].", - "testString": "assert(JSON.stringify(splitify(\"Earth-is-our home\")) === JSON.stringify([\"Earth\", \"is\", \"our\", \"home\"]), 'splitify(\"Earth-is-our home\") should return [\"Earth\", \"is\", \"our\", \"home\"].');" + "text": "splitify(\"Earth-is-our home\")应返回[\"Earth\", \"is\", \"our\", \"home\"]。", + "testString": "assert(JSON.stringify(splitify(\"Earth-is-our home\")) === JSON.stringify([\"Earth\", \"is\", \"our\", \"home\"]), 'splitify(\"Earth-is-our home\")应返回[\"Earth\", \"is\", \"our\", \"home\"]。');" }, { - "text": "splitify(\"This.is.a-sentence\") should return [\"This\", \"is\", \"a\", \"sentence\"].", - "testString": "assert(JSON.stringify(splitify(\"This.is.a-sentence\")) === JSON.stringify([\"This\", \"is\", \"a\", \"sentence\"]), 'splitify(\"This.is.a-sentence\") should return [\"This\", \"is\", \"a\", \"sentence\"].');" + "text": "splitify(\"This.is.a-sentence\")应返回[\"This\", \"is\", \"a\", \"sentence\"]。", + "testString": "assert(JSON.stringify(splitify(\"This.is.a-sentence\")) === JSON.stringify([\"This\", \"is\", \"a\", \"sentence\"]), 'splitify(\"This.is.a-sentence\")应返回[\"This\", \"is\", \"a\", \"sentence\"]。');" } ], "solutions": [], @@ -1377,10 +1378,10 @@ "name": "index", "contents": [ "function splitify(str) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "splitify(\"Hello World,I-am code\");" ], @@ -1393,36 +1394,36 @@ "id": "587d7daa367417b2b2512b6c", "title": "Combine an Array into a String Using the join Method", "description": [ - "The join method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string.", - "Here's an example:", + "join方法用来把数组中的所有元素放入一个字符串,通过指定的分隔符参数进行分隔。", + "举个例子:", "
var arr = [\"Hello\", \"World\"];
var str = arr.join(\" \");
// Sets str to \"Hello World\"
", "
", - "Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str. The function should return a string. For example, \"I-like-Star-Wars\" would be converted to \"I like Star Wars\". For this challenge, do not use the replace method." + "在函数sentensify内用join方法(及其他方法)用字符串str中的单词造句,这个函数应返回一个字符串。举个例子,\"I-like-Star-Wars\" 会被转换成 \"I like Star Wars\"。在此挑战中请勿使用replace方法。" ], "tests": [ { - "text": "Your code should use the join method.", - "testString": "assert(code.match(/\\.join/g), 'Your code should use the join method.');" + "text": "应该使用join方法。", + "testString": "assert(code.match(/\\.join/g), '应该使用join方法。');" }, { - "text": "Your code should not use the replace method.", - "testString": "assert(!code.match(/\\.replace/g), 'Your code should not use the replace method.');" + "text": "不能使用replace方法。", + "testString": "assert(!code.match(/\\.replace/g), '不能使用replace方法。');" }, { - "text": "sentensify(\"May-the-force-be-with-you\") should return a string.", - "testString": "assert(typeof sentensify(\"May-the-force-be-with-you\") === \"string\", 'sentensify(\"May-the-force-be-with-you\") should return a string.');" + "text": "sentensify(\"May-the-force-be-with-you\")应返回一个字符串", + "testString": "assert(typeof sentensify(\"May-the-force-be-with-you\") === \"string\", 'sentensify(\"May-the-force-be-with-you\")应返回一个字符串');" }, { - "text": "sentensify(\"May-the-force-be-with-you\") should return \"May the force be with you\".", - "testString": "assert(sentensify(\"May-the-force-be-with-you\") === \"May the force be with you\", 'sentensify(\"May-the-force-be-with-you\") should return \"May the force be with you\".');" + "text": "sentensify(\"May-the-force-be-with-you\")应返回\"May the force be with you\"。", + "testString": "assert(sentensify(\"May-the-force-be-with-you\") === \"May the force be with you\", 'sentensify(\"May-the-force-be-with-you\")应返回\"May the force be with you\"。');" }, { - "text": "sentensify(\"The.force.is.strong.with.this.one\") should return \"The force is strong with this one\".", - "testString": "assert(sentensify(\"The.force.is.strong.with.this.one\") === \"The force is strong with this one\", 'sentensify(\"The.force.is.strong.with.this.one\") should return \"The force is strong with this one\".');" + "text": "sentensify(\"The.force.is.strong.with.this.one\")应返回\"The force is strong with this one\"。", + "testString": "assert(sentensify(\"The.force.is.strong.with.this.one\") === \"The force is strong with this one\", 'sentensify(\"The.force.is.strong.with.this.one\")应返回\"The force is strong with this one\"。');" }, { - "text": "sentensify(\"There,has,been,an,awakening\") should return \"There has been an awakening\".", - "testString": "assert(sentensify(\"There,has,been,an,awakening\") === \"There has been an awakening\", 'sentensify(\"There,has,been,an,awakening\") should return \"There has been an awakening\".');" + "text": "sentensify(\"There,has,been,an,awakening\")应返回\"There has been an awakening\"。", + "testString": "assert(sentensify(\"There,has,been,an,awakening\") === \"There has been an awakening\", 'sentensify(\"There,has,been,an,awakening\")应返回\"There has been an awakening\"。');" } ], "solutions": [], @@ -1437,10 +1438,10 @@ "name": "index", "contents": [ "function sentensify(str) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "sentensify(\"May-the-force-be-with-you\");" ], @@ -1453,40 +1454,40 @@ "id": "587d7dab367417b2b2512b6d", "title": "Apply Functional Programming to Convert Strings to URL Slugs", "description": [ - "The last several challenges covered a number of useful array and string methods that follow functional programming principles. We've also learned about reduce, which is a powerful method used to reduce problems to simpler forms. From computing averages to sorting, any array operation can be achieved by applying it. Recall that map and filter are special cases of reduce.", - "Let's combine what we've learned to solve a practical problem.", - "Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled \"Stop Using Reduce\", it's likely the URL would have some form of the title string in it (\".../stop-using-reduce\"). You may have already noticed this on the freeCodeCamp site.", + "最后几个挑战中涵盖了许多符合函数式编程原则并在处理数组和字符串中非常有用的方法。我们还学习了强大的、可以将问题简化为更简单形式的reduce方法,从计算平均值到排序,任何数组操作都可以用它来实现。回想一下,mapfilter方法都是reduce的特殊实现。", + "让我们把学到的知识结合起来解决一个实际问题。", + "许多内容管理站点(CMS)为了让添加书签更简单,会将帖子的标题添加到 URL 上。举个例子,如果你写了一篇标题为 \"Stop Using Reduce\" 的帖子,URL很可能会包含某种形式的标题字符串 (如:\".../stop-using-reduce\"),你可能已经在 freeCodeCamp 网站上注意到了这一点。", "
", - "Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace. Here are the requirements:", - "The input is a string with spaces and title-cased words", - "The output is a string with the spaces between words replaced by a hyphen (-)", - "The output should be all lower-cased letters", - "The output should not have any spaces" + "填写urlSlug函数,使其转换字符串title带有连字符号的 URL 版本。您可以使用本节中介绍的任何方法,但不要用replace方法。以下是本次挑战的要求:", + "输入包含空格和标题大小写单词的字符串", + "输出字符串,单词之间的空格用连字符(-)替换", + "输出应该是小写字母", + "输出不应有任何空格" ], "tests": [ { - "text": "The globalTitle variable should not change.", - "testString": "assert(globalTitle === \"Winter Is Coming\", 'The globalTitle variable should not change.');" + "text": "globalTitle变量应保持不变。", + "testString": "assert(globalTitle === \"Winter Is Coming\", 'globalTitle变量应保持不变。');" }, { - "text": "Your code should not use the replace method for this challenge.", - "testString": "assert(!code.match(/\\.replace/g), 'Your code should not use the replace method for this challenge.');" + "text": "在此挑战中不能使用replace方法。", + "testString": "assert(!code.match(/\\.replace/g), '在此挑战中不能使用replace方法。');" }, { - "text": "urlSlug(\"Winter Is Coming\") should return \"winter-is-coming\".", - "testString": "assert(urlSlug(\"Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\"Winter Is Coming\") should return \"winter-is-coming\".');" + "text": "urlSlug(\"Winter Is Coming\")应返回\"winter-is-coming\"。", + "testString": "assert(urlSlug(\"Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\"Winter Is Coming\")应返回\"winter-is-coming\"。');" }, { - "text": "urlSlug(\" Winter Is  Coming\") should return \"winter-is-coming\".", - "testString": "assert(urlSlug(\" Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\" Winter Is  Coming\") should return \"winter-is-coming\".');" + "text": "urlSlug(\" Winter Is  Coming\")应返回\"winter-is-coming\"。", + "testString": "assert(urlSlug(\" Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\" Winter Is  Coming\")应返回\"winter-is-coming\"。');" }, { - "text": "urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") should return \"a-mind-needs-books-like-a-sword-needs-a-whetstone\".", - "testString": "assert(urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") === \"a-mind-needs-books-like-a-sword-needs-a-whetstone\", 'urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") should return \"a-mind-needs-books-like-a-sword-needs-a-whetstone\".');" + "text": "urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")应返回\"a-mind-needs-books-like-a-sword-needs-a-whetstone\"。", + "testString": "assert(urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") === \"a-mind-needs-books-like-a-sword-needs-a-whetstone\", 'urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")应返回\"a-mind-needs-books-like-a-sword-needs-a-whetstone\"。');" }, { - "text": "urlSlug(\"Hold The Door\") should return \"hold-the-door\".", - "testString": "assert(urlSlug(\"Hold The Door\") === \"hold-the-door\", 'urlSlug(\"Hold The Door\") should return \"hold-the-door\".');" + "text": "urlSlug(\"Hold The Door\")应返回\"hold-the-door\"。", + "testString": "assert(urlSlug(\"Hold The Door\") === \"hold-the-door\", 'urlSlug(\"Hold The Door\")应返回\"hold-the-door\"。');" } ], "solutions": [], @@ -1501,17 +1502,17 @@ "ext": "js", "name": "index", "contents": [ - "// the global variable", + "// 全局变量", "var globalTitle = \"Winter Is Coming\";", "", - "// Add your code below this line", + "// 请在本行以下添加你的代码", "function urlSlug(title) {", " ", " ", "}", - "// Add your code above this line", + "// 请在本行以上添加你的代码", "", - "var winterComing = urlSlug(globalTitle); // Should be \"winter-is-coming\"" + "var winterComing = urlSlug(globalTitle); // 应为 \"winter-is-coming\"" ], "head": [], "tail": [] @@ -1522,28 +1523,28 @@ "id": "587d7dab367417b2b2512b6e", "title": "Use the every Method to Check that Every Element in an Array Meets a Criteria", "description": [ - "The every method works with arrays to check if every element passes a particular test. It returns a Boolean value - true if all values meet the criteria, false if not.", - "For example, the following code would check if every element in the numbers array is less than 10:", - "
var numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) {
  return currentValue < 10;
});
// Returns false
", + "every方法用于检测数组所有元素是否都符合指定条件。如果所有元素满足条件,返回布尔值true,反之返回false。", + "举个例子,下面的代码检测数组numbers的所有元素是否都小于 10:", + "
var numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) {
  return currentValue < 10;
});
// 返回 false
", "
", - "Use the every method inside the checkPositive function to check if every element in arr is positive. The function should return a Boolean value." + "在checkPositive函数中使用every方法检查arr中是否所有元素都是正数,函数应返回一个布尔值。" ], "tests": [ { - "text": "Your code should use the every method.", - "testString": "assert(code.match(/\\.every/g), 'Your code should use the every method.');" + "text": "应该使用every方法。", + "testString": "assert(code.match(/\\.every/g), '应该使用every方法。');" }, { - "text": "checkPositive([1, 2, 3, -4, 5]) should return false.", - "testString": "assert(!checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5]) should return false.');" + "text": "checkPositive([1, 2, 3, -4, 5])应返回false。", + "testString": "assert(!checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])应返回false。');" }, { - "text": "checkPositive([1, 2, 3, 4, 5]) should return true.", - "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5]) should return true.');" + "text": "checkPositive([1, 2, 3, 4, 5])应返回true。", + "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])应返回true。');" }, { - "text": "checkPositive([1, -2, 3, -4, 5]) should return false.", - "testString": "assert(!checkPositive([1, -2, 3, -4, 5]), 'checkPositive([1, -2, 3, -4, 5]) should return false.');" + "text": "checkPositive([1, -2, 3, -4, 5])应返回false。", + "testString": "assert(!checkPositive([1, -2, 3, -4, 5]), 'checkPositive([1, -2, 3, -4, 5])应返回false。');" } ], "solutions": [], @@ -1556,10 +1557,10 @@ "name": "index", "contents": [ "function checkPositive(arr) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "checkPositive([1, 2, 3, -4, 5]);" ], @@ -1572,28 +1573,28 @@ "id": "587d7dab367417b2b2512b6f", "title": "Use the some Method to Check that Any Elements in an Array Meet a Criteria", "description": [ - "The some method works with arrays to check if any element passes a particular test. It returns a Boolean value - true if any of the values meet the criteria, false if not.", - "For example, the following code would check if any element in the numbers array is less than 10:", - "
var numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) {
  return currentValue < 10;
});
// Returns true
", + "some方法用于检测数组中任何元素是否满足指定条件。如果有一个元素满足条件,返回布尔值true,反之返回false。", + "举个例子,下面的代码检测数组numbers中是否有元素小于10:", + "
var numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) {
  return currentValue < 10;
});
// 返回 true
", "
", - "Use the some method inside the checkPositive function to check if any element in arr is positive. The function should return a Boolean value." + "在checkPositive函数值中使用some检查arr中是否有元素为正数,函数应返回一个布尔值。" ], "tests": [ { - "text": "Your code should use the some method.", - "testString": "assert(code.match(/\\.some/g), 'Your code should use the some method.');" + "text": "应该使用some method.", + "testString": "assert(code.match(/\\.some/g), '应该使用some method.');" }, { - "text": "checkPositive([1, 2, 3, -4, 5]) should return true.", - "testString": "assert(checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5]) should return true.');" + "text": "checkPositive([1, 2, 3, -4, 5])应返回true。", + "testString": "assert(checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])应返回true。');" }, { - "text": "checkPositive([1, 2, 3, 4, 5]) should return true.", - "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5]) should return true.');" + "text": "checkPositive([1, 2, 3, 4, 5])应返回true。", + "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])应返回true。');" }, { - "text": "checkPositive([-1, -2, -3, -4, -5]) should return false.", - "testString": "assert(!checkPositive([-1, -2, -3, -4, -5]), 'checkPositive([-1, -2, -3, -4, -5]) should return false.');" + "text": "checkPositive([-1, -2, -3, -4, -5])应返回false。", + "testString": "assert(!checkPositive([-1, -2, -3, -4, -5]), 'checkPositive([-1, -2, -3, -4, -5])应返回false。');" } ], "solutions": [], @@ -1606,10 +1607,10 @@ "name": "index", "contents": [ "function checkPositive(arr) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "checkPositive([1, 2, 3, -4, 5]);" ], @@ -1622,34 +1623,34 @@ "id": "587d7dab367417b2b2512b70", "title": "Introduction to Currying and Partial Application", "description": [ - "The arity of a function is the number of arguments it requires. Currying a function means to convert a function of N arity into N functions of arity 1.", - "In other words, it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on.", - "Here's an example:", - "
//Un-curried function
function unCurried(x, y) {
  return x + y;
}

//Curried function
function curried(x) {
  return function(y) {
    return x + y;
  }
}
curried(1)(2) // Returns 3
", - "This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the curried function in the example above:", - "
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
", - "Similarly, partial application can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments.", - "Here's an example:", - "
//Impartial function
function impartial(x, y, z) {
  return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
", + "arity是函数所需的形参的数量。函数Currying意思是把接受多个arity的函数变换成接受一个单一arity的函数。", + "换句话说,重构函数让它接收一个参数,然后返回另一个接收下一个参数的函数,依此类推。", + "举个例子:", + "
//Un-curried function
function unCurried(x, y) {
  return x + y;
}

// 柯里化函数
function curried(x) {
  return function(y) {
    return x + y;
  }
}
curried(1)(2) // 返回 3
", + "如果不能一次为函数提供所有参数,那么在柯里化很有用。可以将每个函数的调用保存到一个变量中,该变量将保存返回的函数引用,该引用在下一个参数可用时接受该参数。下面是一个使用curried函数的例子:", + "
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // 打印 3
", + "类似地,partial application可以描述为一次对一个函数应用几个参数,然后返回另一个应用更多参数的函数。", + "举个例子:", + "
//Impartial function
function impartial(x, y, z) {
  return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // 返回 13
", "
", - "Fill in the body of the add function so it uses currying to add parameters x, y, and z." + "填写add函数主体部分,通过柯里化方式来添加参数xyz." ], "tests": [ { - "text": "add(10)(20)(30) should return 60.", - "testString": "assert(add(10)(20)(30) === 60, 'add(10)(20)(30) should return 60.');" + "text": "add(10)(20)(30)应返回60。", + "testString": "assert(add(10)(20)(30) === 60, 'add(10)(20)(30)应返回60。');" }, { - "text": "add(1)(2)(3) should return 6.", - "testString": "assert(add(1)(2)(3) === 6, 'add(1)(2)(3) should return 6.');" + "text": "add(1)(2)(3)应返回6。", + "testString": "assert(add(1)(2)(3) === 6, 'add(1)(2)(3)应返回6。');" }, { - "text": "add(11)(22)(33) should return 66.", - "testString": "assert(add(11)(22)(33) === 66, 'add(11)(22)(33) should return 66.');" + "text": "add(11)(22)(33)应返回66。", + "testString": "assert(add(11)(22)(33) === 66, 'add(11)(22)(33)应返回66。');" }, { - "text": "Your code should include a final statement that returns x + y + z.", - "testString": "assert(code.match(/[xyz]\\s*?\\+\\s*?[xyz]\\s*?\\+\\s*?[xyz]/g), 'Your code should include a final statement that returns x + y + z.');" + "text": "应包含一个最终返回x + y + z的代码。", + "testString": "assert(code.match(/[xyz]\\s*?\\+\\s*?[xyz]\\s*?\\+\\s*?[xyz]/g), '应包含一个最终返回x + y + z的代码。');" } ], "solutions": [], @@ -1662,10 +1663,10 @@ "name": "index", "contents": [ "function add(x) {", - " // Add your code below this line", + " // 请在本行以下添加你的代码", " ", " ", - " // Add your code above this line", + " // 请在本行以上添加你的代码", "}", "add(10)(20)(30);" ], From fdbc363c39647809d6eecc8f5505e5b09510a1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E9=9B=A8=E7=BA=AF?= <790864964@qq.com> Date: Fri, 10 Aug 2018 10:32:51 +0800 Subject: [PATCH 23/24] Revert "functional-programming.json complete" --- .../functional-programming.json | 779 +++++++++--------- 1 file changed, 389 insertions(+), 390 deletions(-) diff --git a/02-javascript-algorithms-and-data-structures/functional-programming.json b/02-javascript-algorithms-and-data-structures/functional-programming.json index d6d239b..f981533 100644 --- a/02-javascript-algorithms-and-data-structures/functional-programming.json +++ b/02-javascript-algorithms-and-data-structures/functional-programming.json @@ -8,24 +8,24 @@ "id": "587d7b8d367417b2b2512b5b", "title": "Learn About Functional Programming", "description": [ - "函数式编程是一种解决方案简单,功能独立,对作用域外没有任何副作用的编程范式。", + "Functional programming is a style of programming where solutions are simple, isolated functions, without any side effects outside of the function scope.", "INPUT -> PROCESS -> OUTPUT", - "函数式编程:", - "1)功能独立——不依赖于程序的状态(比如可能发生变化的全局变量);", - "2)纯函数——同一个输入永远能得到同一个输出;", - "3)有限的副作用——可以严格地限制函数外部对状态的更改导致的状态变化。", + "Functional programming is about:", + "1) Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change", + "2) Pure functions - the same input always gives the same output", + "3) Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled", "
", - "freeCodeCamp 成员在 love tea 的故事。", - "在代码编辑器中,已经为你定义好了prepareTeagetTea函数。调用getTea函数为团队准备 40 杯茶,并将它们存储在tea4TeamFCC变量里。" + "The members of freeCodeCamp happen to love tea.", + "In the code editor, the prepareTea and getTea functions are already defined for you. Call the getTea function to get 40 cups of tea for the team, and store them in the tea4TeamFCC variable." ], "tests": [ { - "text": "tea4TeamFCC变量里应有 40 杯为团队准备的茶。", - "testString": "assert(tea4TeamFCC.length === 40, 'tea4TeamFCC变量里应有 40 杯为团队准备的茶。');" + "text": "The tea4TeamFCC variable should hold 40 cups of tea for the team.", + "testString": "assert(tea4TeamFCC.length === 40, 'The tea4TeamFCC variable should hold 40 cups of tea for the team.');" }, { - "text": "tea4TeamFCC变量里应有 greenTea。", - "testString": "assert(tea4TeamFCC[0] === 'greenTea', 'tea4TeamFCC变量里应有 greenTea。');" + "text": "The tea4TeamFCC variable should hold cups of green tea.", + "testString": "assert(tea4TeamFCC[0] === 'greenTea', 'The tea4TeamFCC variable should hold cups of green tea.');" } ], "solutions": [], @@ -38,15 +38,15 @@ "name": "index", "contents": [ "/**", - " * 备茶过程", - " * @return {string} 一杯茶", + " * A long process to prepare tea.", + " * @return {string} A cup of tea.", " **/", "const prepareTea = () => 'greenTea';", "", "/**", - " * 获得一定数量的茶", - " * @param {number} numOfCups 所需茶的数量", - " * @return {Array} 返回给定量的茶", + " * Get given number of cups of tea.", + " * @param {number} numOfCups Number of required cups of tea.", + " * @return {Array} Given amount of tea cups.", " **/", "const getTea = (numOfCups) => {", " const teaCups = [];", @@ -59,11 +59,11 @@ " return teaCups;", "};", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "", "const tea4TeamFCC = null; // :(", "", - "// 请在本行以上添加你的代码", + "// Add your code above this line", "", "console.log(tea4TeamFCC);" ], @@ -76,33 +76,33 @@ "id": "587d7b8e367417b2b2512b5c", "title": "Understand Functional Programming Terminology", "description": [ - "FCC 团队需求有变更,现在想要两种茶:绿茶(green tea)和红茶(black tea)。事实证明,用户需求变更是很常见的。", - "基于以上信息,我们需要重构上一节挑战中的getTea函数来处理多种茶的请求。我们可以修改getTea接受一个函数作为参数,使它能够修改茶的类型。这让getTea更灵活,也使需求变更时为程序员提供更多控制权。", - "首先,我们将介绍一些术语:", - "Callbacks是被传递到另一个函数中调用的函数。你应该已经在其他函数中看过这个写法,例如在filter中,回调函数告诉 JavaScript 以什么规则过滤数组。", - "函数就像其他正常值一样,可以赋值给变量、传递给另一个函数,或从其它函数返回,这种函数叫做first class函数。在 JavaScript 中,所有函数都是first class函数。", - "将函数为参数或返回值的函数叫做higher order函数。", - "当函数传递给另一个函数或从另一个函数返回时,那些传入或返回的函数可以叫做lambda。", + "The FCC Team had a mood swing and now wants two types of tea: green tea and black tea. General Fact: Client mood swings are pretty common.", + "With that information, we'll need to revisit the getTea function from last challenge to handle various tea requests. We can modify getTea to accept a function as a parameter to be able to change the type of tea it prepares. This makes getTea more flexible, and gives the programmer more control when client requests change.", + "But first, let's cover some functional terminology:", + "Callbacks are the functions that are slipped or passed into another function to decide the invocation of that function. You may have seen them passed to other methods, for example in filter, the callback function tells JavaScript the criteria for how to filter an array.", + "Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first class functions. In JavaScript, all functions are first class functions.", + "The functions that take a function as an argument, or return a function as a return value are called higher order functions.", + "When the functions are passed in to another function or returned from another function, then those functions which gets passed in or returned can be called a lambda.", "
", - "准备 27 杯绿茶和 13 杯红茶,分别存入tea4GreenTeamFCCtea4BlackTeamFCC变量。请注意,getTea函数已经变了,现在它接收一个函数作为第一个参数。", - "注意:数据(茶的数量)作为最后一个参数。我们将在后面的课程中对此进行更多讨论。" + "Prepare 27 cups of green tea and 13 cups of black tea and store them in tea4GreenTeamFCC and tea4BlackTeamFCC variables, respectively. Note that the getTea function has been modified so it now takes a function as the first argument.", + "Note: The data (the number of cups of tea) is supplied as the last argument. We'll discuss this more in later lessons." ], "tests": [ { - "text": "tea4GreenTeamFCC变量应存有为团队准备的 27 杯茶。", - "testString": "assert(tea4GreenTeamFCC.length === 27, 'tea4GreenTeamFCC变量应存有为团队准备的 27 杯茶。');" + "text": "The tea4GreenTeamFCC variable should hold 27 cups of green tea for the team.", + "testString": "assert(tea4GreenTeamFCC.length === 27, 'The tea4GreenTeamFCC variable should hold 27 cups of green tea for the team.');" }, { - "text": "tea4GreenTeamFCC变量应存有绿茶。", - "testString": "assert(tea4GreenTeamFCC[0] === 'greenTea', 'tea4GreenTeamFCC变量应存有绿茶。');" + "text": "The tea4GreenTeamFCC variable should hold cups of green tea.", + "testString": "assert(tea4GreenTeamFCC[0] === 'greenTea', 'The tea4GreenTeamFCC variable should hold cups of green tea.');" }, { - "text": "tea4BlackTeamFCC变量应存有 13 杯红茶。", - "testString": "assert(tea4BlackTeamFCC.length === 13, 'tea4BlackTeamFCC变量应存有 13 杯红茶。');" + "text": "The tea4BlackTeamFCC variable should hold 13 cups of black tea.", + "testString": "assert(tea4BlackTeamFCC.length === 13, 'The tea4BlackTeamFCC variable should hold 13 cups of black tea.');" }, { - "text": "tea4BlackTeamFCC变量应存有红茶。", - "testString": "assert(tea4BlackTeamFCC[0] === 'blackTea', 'tea4BlackTeamFCC变量应存有红茶。');" + "text": "The tea4BlackTeamFCC variable should hold cups of black tea.", + "testString": "assert(tea4BlackTeamFCC[0] === 'blackTea', 'The tea4BlackTeamFCC variable should hold cups of black tea.');" } ], "solutions": [], @@ -115,22 +115,22 @@ "name": "index", "contents": [ "/**", - " * 绿茶准备过程", - " * @return {string} 一杯绿茶", + " * A long process to prepare green tea.", + " * @return {string} A cup of green tea.", " **/", "const prepareGreenTea = () => 'greenTea';", "", "/**", - " * 备红茶准备过程", - " * @return {string} 一杯红茶", + " * A long process to prepare black tea.", + " * @return {string} A cup of black tea.", " **/", "const prepareBlackTea = () => 'blackTea';", "", "/**", - " * 获得一定数量的茶", - " * @param {function():string} prepareTea 茶的类型", - " * @param {number} numOfCups 需要茶的数量", - " * @return {Array} 给定的茶", + " * Get given number of cups of tea.", + " * @param {function():string} prepareTea The type of tea preparing function.", + " * @param {number} numOfCups Number of required cups of tea.", + " * @return {Array} Given amount of tea cups.", " **/", "const getTea = (prepareTea, numOfCups) => {", " const teaCups = [];", @@ -143,12 +143,12 @@ " return teaCups;", "};", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "", "const tea4GreenTeamFCC = null; // :(", "const tea4BlackTeamFCC = null; // :(", "", - "// 请在本行以上添加你的代码", + "// Add your code above this line", "", "console.log(", " tea4GreenTeamFCC,", @@ -164,17 +164,17 @@ "id": "587d7b8e367417b2b2512b5d", "title": "Understand the Hazards of Using Imperative Code", "description": [ - "函数式编程是一种好习惯,它能让代码管理更简单,不受隐藏 bug 影响。在我们开始函数式编程之前,为了更好的突显可能遇到的问题,我们先看看命令式编程。", - "类似在英语(和许多其他语言)中,命令式时态用于给出命令,编程中的命令式是给计算机一组语句来执行任务。", - "这些语句通常会改变程序的状态,例如更新全局变量,典型的例子就是写一个for循环,它给出了迭代数组索引的精确方向。", - "相反,函数式编程是声明式编程的一种形式,通过调用方法或函数来告诉计算机要做什么。", - "JavaScript 提供了许多处理常见任务的方法,所以你无需写出计算机应如何执行它们。例如,你可以用map函数替代上面提到的for循环来处理数组迭代。这有助于避免语义错误,如调试章节介绍的\"Off By One Errors\"。", - "考虑这样的场景:你正在浏览器中浏览网页,并想操作你打开的标签。下面我们来试试用面向对象的思路来描述这种情景。", - "窗口对象由选项卡组成,通常会打开多个窗口。窗口对象中每个打开网站的标题都保存在一个数组中。在对浏览器进行了如打开新标签、合并窗口、关闭标签之类的操作后,你需要输出所有打开的标签。关掉的标签将从数组中删除,新打开的标签(为简单起见)则添加到数组的末尾。", - "代码编辑器中显示了此功能的实现,其中包含tabOpen()tabClose(),和 join()函数。tabs数组是窗口对象的一部分用于储存打开页面的名称。", - "

说明

", - "在编辑器中运行代码。它使用了有副作用的方法,导致输出错误。打开标签的最终列表应该是['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab']但输出会略有不同。", - "仔细阅读代码,看看你是否能找出问题所在,然后进入下一个挑战,了解更多信息。" + "Functional programming is a good habit. It keeps your code easy to manage, and saves you from sneaky bugs. But before we get there, let's look at an imperative approach to programming to highlight where you may have issues.", + "In English (and many other languages), the imperative tense is used to give commands. Similarly, an imperative style in programming is one that gives the computer a set of statements to perform a task.", + "Often the statements change the state of the program, like updating global variables. A classic example is writing a for loop that gives exact directions to iterate over the indices of an array.", + "In contrast, functional programming is a form of declarative programming. You tell the computer what you want done by calling a method or function.", + "JavaScript offers many predefined methods that handle common tasks so you don't need to write out how the computer should perform them. For example, instead of using the for loop mentioned above, you could call the map method which handles the details of iterating over an array. This helps to avoid semantic errors, like the \"Off By One Errors\" that were covered in the Debugging section.", + "Consider the scenario: you are browsing the web in your browser, and want to track the tabs you have opened. Let's try to model this using some simple object-oriented code.", + "A Window object is made up of tabs, and you usually have more than one Window open. The titles of each open site in each Window object is held in an array. After working in the browser (opening new tabs, merging windows, and closing tabs), you want to print the tabs that are still open. Closed tabs are removed from the array and new tabs (for simplicity) get added to the end of it.", + "The code editor shows an implementation of this functionality with functions for tabOpen(), tabClose(), and join(). The array tabs is part of the Window object that stores the name of the open pages.", + "

Instructions

", + "Run the code in the editor. It's using a method that has side effects in the program, causing incorrect output. The final list of open tabs should be ['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium', 'new tab', 'Netflix', 'YouTube', 'Vine', 'GMail', 'Work mail', 'Docs', 'freeCodeCamp', 'new tab'] but the output will be slightly different.", + "Work through the code and see if you can figure out the problem, then advance to the next challenge to learn more." ], "tests": [ { @@ -191,41 +191,41 @@ "ext": "js", "name": "index", "contents": [ - "// tabs 是窗口中打开网站的标题数组", + "// tabs is an array of titles of each site open within the window", "var Window = function(tabs) {", - " this.tabs = tabs; // 我们将数组记录在对象内部", + " this.tabs = tabs; // we keep a record of the array inside the object", "};", "", - "// 当两个窗口合并成一个窗口时", + "// When you join two windows into one window", "Window.prototype.join = function (otherWindow) {", " this.tabs = this.tabs.concat(otherWindow.tabs);", " return this;", "};", "", - "// 在末尾打开一个新标签", + "// When you open a new tab at the end", "Window.prototype.tabOpen = function (tab) {", - " this.tabs.push('new tab'); // 打开一个新的标签", + " this.tabs.push('new tab'); // let's open a new tab for now", " return this;", "};", "", - "// 关闭一个标签", + "// When you close a tab", "Window.prototype.tabClose = function (index) {", - " var tabsBeforeIndex = this.tabs.splice(0, index); // 获取前面的标签", - " var tabsAfterIndex = this.tabs.splice(index); // 获取后面的标签", + " var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab", + " var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab", "", - " this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // 拼接到一起", + " this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together ", " return this;", " };", "", - "// 创建三个浏览器窗口", - "var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // 邮箱、文档及其他与工作相关的网站", - "var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // 社交网站", - "var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // 娱乐网站", + "// Let's create three browser windows", + "var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); // Your mailbox, drive, and other work sites", + "var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); // Social sites", + "var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Entertainment sites", "", - "// 执行标签打开,关闭和其他操作", + "// Now perform the tab opening, closing, and other operations", "var finalTabs = socialWindow", - " .tabOpen() // 新开一个 cat memes 的标签", - " .join(videoWindow.tabClose(2)) // 在娱乐网站关闭第三个标签,加入数组", + " .tabOpen() // Open a new tab for cat memes", + " .join(videoWindow.tabClose(2)) // Close third tab in video window, and join", " .join(workWindow.tabClose(1).tabOpen());", "", "alert(finalTabs.tabs);" @@ -239,23 +239,23 @@ "id": "587d7b8e367417b2b2512b5e", "title": "Avoid Mutations and Side Effects Using Functional Programming", "description": [ - "如果你还没想通,上一个挑战的问题出在tabClose()函数里的splice。不幸的是,splice修改了调用它的原始数组,所以第二次调用它时是基于修改后的数组,才给出了意料之外的结果。", - "这是一个小例子,还有更广义的定义——你在变量,数组或对象上调用一个函数,这个会函数改变对象中的变量或其他东西。", - "函数式编程的核心原则之一是不改变任何东西。变化会导致错误。如果一个函数不改变传入的参数、全局变量等数据,那么它造成问题的可能性就会小很多。", - "前面的例子没有任何复杂的操作,但是splice方法改变了原始数组,导致 bug 产生。", - "回想一下,在函数式编程中,改变或变更叫做mutation,这种改变的结果叫做“副作用”(side effect)。理想情况下,函数应该是不会产生任何副作用的pure function。", - "让我们尝试掌握这个原则:不要改变代码中的任何变量或对象。", + "If you haven't already figured it out, the issue in the previous challenge was with the splice call in the tabClose() function. Unfortunately, splice changes the original array it is called on, so the second call to it used a modified array, and gave unexpected results.", + "This is a small example of a much larger pattern - you call a function on a variable, array, or an object, and the function changes the variable or something in the object.", + "One of the core principle of functional programming is to not change things. Changes lead to bugs. It's easier to prevent bugs knowing that your functions don't change anything, including the function arguments or any global variable.", + "The previous example didn't have any complicated operations but the splice method changed the original array, and resulted in a bug.", + "Recall that in functional programming, changing or altering things is called mutation, and the outcome is called a side effect. A function, ideally, should be a pure function, meaning that it does not cause any side effects.", + "Let's try to master this discipline and not alter any variable or object in our code.", "
", - "填写incrementer函数的代码,使其返回全局变量fixedValue的值增加 1。" + "Fill in the code for the function incrementer so it returns the value of the global variable fixedValue increased by one." ], "tests": [ { - "text": "incrementer函数不能改变fixedValue的值。", - "testString": "assert(fixedValue === 4, '你的incrementer函数不能改变fixedValue的值。');" + "text": "Your function incrementer should not change the value of fixedValue.", + "testString": "assert(fixedValue === 4, 'Your function incrementer should not change the value of fixedValue.');" }, { - "text": "incrementer函数应返回比fixedValue变量更大的值。", - "testString": "assert(newValue === 5, 'incrementer函数应返回比fixedValue变量更大的值。');" + "text": "Your incrementer function should return a value that is one larger than the fixedValue value.", + "testString": "assert(newValue === 5, 'Your incrementer function should return a value that is one larger than the fixedValue value.');" } ], "solutions": [], @@ -267,18 +267,18 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var fixedValue = 4;", "", "function incrementer () {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "", - "var newValue = incrementer(); // 应等于 5", - "console.log(fixedValue); // 应打印 4" + "var newValue = incrementer(); // Should equal 5", + "console.log(fixedValue); // Should print 4" ], "head": [], "tail": [] @@ -289,28 +289,28 @@ "id": "587d7b8e367417b2b2512b5f", "title": "Pass Arguments to Avoid External Dependence in a Function", "description": [ - "最后一个挑战是更接近函数式编程原则的挑战,但是仍然缺少一些东西。", - "虽然我们没有改变全局变量值,但在没有全局变量fixedValue情况下,incrementer函数将不起作用。", - "函数式编程的另一个原则是:总是显式声明依赖关系。如果函数依赖于一个变量或对象,那么将该变量或对象作为参数直接传递到函数中。", - "这样做会有很多好处,其中一点是让函数更容易测试,因为你确切地知道参数是什么,并且这个参数也不依赖于程序中的任何其他内容。", - "其次,这样做可以让你更加自信地更改,删除或添加新代码。因为你很清楚哪些是可以改的,哪些是不可以改的,这样你就知道哪里可能会有潜在的陷阱。", - "最后,无论代码的哪一部分执行它,函数总是会为同一组输入生成相同的输出。", + "The last challenge was a step closer to functional programming principles, but there is still something missing.", + "We didn't alter the global variable value, but the function incrementer would not work without the global variable fixedValue being there.", + "Another principle of functional programming is to always declare your dependencies explicitly. This means if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument.", + "There are several good consequences from this principle. The function is easier to test, you know exactly what input it takes, and it won't depend on anything else in your program.", + "This can give you more confidence when you alter, remove, or add new code. You would know what you can or cannot change and you can see where the potential traps are.", + "Finally, the function would always produce the same output for the same set of inputs, no matter what part of the code executes it.", "
", - "更新incrementer函数,明确声明其依赖项。", - "编写incrementer函数,获取它的参数,然后将值增加 1。" + "Let's update the incrementer function to clearly declare its dependencies.", + "Write the incrementer function so it takes an argument, and then increases the value by one." ], "tests": [ { - "text": "incrementer函数不能修改fixedValue的值。", - "testString": "assert(fixedValue === 4, 'incrementer函数不能修改fixedValue的值。');" + "text": "Your function incrementer should not change the value of fixedValue.", + "testString": "assert(fixedValue === 4, 'Your function incrementer should not change the value of fixedValue.');" }, { - "text": "incrementer函数应该接收一个参数。", - "testString": "assert(code.match(/function\\s+?incrementer\\s*?\\(.+?\\)/g), 'incrementer函数应该接收一个参数。');" + "text": "Your incrementer function should take a parameter.", + "testString": "assert(code.match(/function\\s+?incrementer\\s*?\\(.+?\\)/g), 'Your incrementer function should take a parameter.');" }, { - "text": "incrementer函数应返回比fixedValue更大的值。", - "testString": "assert(newValue === 5, 'incrementer函数应返回比fixedValue更大的值。');" + "text": "Your incrementer function should return a value that is one larger than the fixedValue value.", + "testString": "assert(newValue === 5, 'Your incrementer function should return a value that is one larger than the fixedValue value.');" } ], "solutions": [], @@ -322,18 +322,18 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var fixedValue = 4;", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "function incrementer () {", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "", - "var newValue = incrementer(fixedValue); // 应等于 5", - "console.log(fixedValue); // 应打印 4" + "var newValue = incrementer(fixedValue); // Should equal 5", + "console.log(fixedValue); // Should print 4" ], "head": [], "tail": [] @@ -344,29 +344,29 @@ "id": "587d7b8f367417b2b2512b60", "title": "Refactor Global Variables Out of Functions", "description": [ - "目前为止,我们已经看到了函数式编程的两个不同原则:", - "1) 不要更改变量或对象——创建新变量和对象,并在需要时从函数返回它们。", - "2) 声明函数参数——函数内的任何计算仅取决于参数,而不取决于任何全局对象或变量。", - "给数字加 1 不够刺激,我们可以在处理数组或更复杂的对象时应用这些原则。", + "So far, we have seen two distinct principles for functional programming:", + "1) Don't alter a variable or object - create new variables and objects and return them if need be from a function.", + "2) Declare function arguments - any computation inside a function depends only on the arguments, and not on any global object or variable.", + "Adding one to a number is not very exciting, but we can apply these principles when working with arrays or more complex objects.", "
", - "重构代码,使全局数组bookList在任何函数内部都不会被改变。add函数可以将指定的bookName增加到数组末尾。remove函数可以从数组中移除指定bookName。两个函数都返回数组,并且任何参数都应该添加到bookName前面。" + "Refactor (rewrite) the code so the global array bookList is not changed inside either function. The add function should add the given bookName to the end of an array. The remove function should remove the given bookName from an array. Both functions should return an array, and any new parameters should be added before the bookName one." ], "tests": [ { - "text": "bookList不应该改变,应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", - "testString": "assert(JSON.stringify(bookList) === JSON.stringify([\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]), 'bookList不应该改变,应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" + "text": "bookList should not change and still equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", + "testString": "assert(JSON.stringify(bookList) === JSON.stringify([\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"]), 'bookList should not change and still equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" }, { - "text": "newBookList应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", - "testString": "assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newBookList应等于[\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" + "text": "newBookList should equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", + "testString": "assert(JSON.stringify(newBookList) === JSON.stringify(['The Hound of the Baskervilles', 'On The Electrodynamics of Moving Bodies', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newBookList should equal [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" }, { - "text": "newerBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", - "testString": "assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae']), 'newerBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" + "text": "newerBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].", + "testString": "assert(JSON.stringify(newerBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae']), 'newerBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"].');" }, { - "text": "newestBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", - "testString": "assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newestBookList应等于[\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" + "text": "newestBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].", + "testString": "assert(JSON.stringify(newestBookList) === JSON.stringify(['The Hound of the Baskervilles', 'Philosophiæ Naturalis Principia Mathematica', 'Disquisitiones Arithmeticae', 'A Brief History of Time']), 'newestBookList should equal [\"The Hound of the Baskervilles\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\", \"A Brief History of Time\"].');" } ], "solutions": [], @@ -382,30 +382,30 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var bookList = [\"The Hound of the Baskervilles\", \"On The Electrodynamics of Moving Bodies\", \"Philosophiæ Naturalis Principia Mathematica\", \"Disquisitiones Arithmeticae\"];", "", - "/* 这个函数应该增加一本书到列表中,并返回这个列表 */", - "// 新参数应在 bookName 之前", + "/* This function should add a book to the list and return the list */", + "// New parameters should come before the bookName one", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "function add (bookName) {", " ", " return bookList.push(bookName);", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "", - "/* 这个函数应该移除一本书到列表中,并返回这个列表 */", - "// 新参数应在 bookName 之前", + "/* This function should remove a book from the list and return the list */", + "// New parameters should come before the bookName one", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "function remove (bookName) {", " if (bookList.indexOf(bookName) >= 0) {", " ", " return bookList.splice(0, 1, bookName);", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", " }", "}", "", @@ -424,30 +424,30 @@ "id": "587d7b8f367417b2b2512b61", "title": "Use the map Method to Extract Data from an Array", "description": [ - "目前为止,我们已经学会了使用纯函数来避免程序中的副作用。此外,我们已经看到函数的值仅取决于其输入参数。", - "这仅仅是个开始。顾名思义,函数式编程以函数理论为中心。", - "能够将它们作为参数传递给其他函数,和从另一个函数返回一个函数是有意义的。函数在 JavaScript 中被视为First Class Objects,它们可以像任何其他对象一样使用。它们可以保存在变量中,存储在对象中,也可以作为函数参数传递。", - "让我们从一些简单的数组函数开始,这些函数是数组对象原型上的方法。在本练习中,我们来了解下数组的map方法(即Array.prototype.map())。", - "请记住,map方法是迭代数组中每一项的方式之一。在对每个元素应用回调函数后,它会创建一个新数组(不改变原来的数组)。", + "So far we have learned to use pure functions to avoid side effects in a program. Also, we have seen the value in having a function only depend on its input arguments.", + "This is only the beginning. As its name suggests, functional programming is centered around a theory of functions.", + "It would make sense to be able to pass them as arguments to other functions, and return a function from another function. Functions are considered First Class Objects in JavaScript, which means they can be used like any other object. They can be saved in variables, stored in an object, or passed as function arguments.", + "Let's start with some simple array functions, which are methods on the array object prototype. In this exercise we are looking at Array.prototype.map(), or more simply map.", + "Remember that the map method is a way to iterate over each item in an array. It creates a new array (without changing the original one) after applying a callback function to every element.", "
", - "watchList数组保存了包含一些电影信息的对象。使用mapwatchList中提取标题(title)和评分(rating),并将新数组保存在rating变量里。目前编辑器中的代码是使用for循环实现,使用map表达式替换循环功能。" + "The watchList array holds objects with information on several movies. Use map to pull the title and rating from watchList and save the new array in the rating variable. The code in the editor currently uses a for loop to do this, replace the loop functionality with your map expression." ], "tests": [ { - "text": "watchList不能被改变", - "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList不能被改变');" + "text": "The watchList variable should not change.", + "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList variable should not change.');" }, { - "text": "你的代码不能使用for循环。", - "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), '你的代码不能使用for循环。');" + "text": "Your code should not use a for loop.", + "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), 'Your code should not use a for loop.');" }, { - "text": "你的代码应使用map方法。", - "testString": "assert(code.match(/\\.map/g), '你的代码应使用map方法。');" + "text": "Your code should use the map method.", + "testString": "assert(code.match(/\\.map/g), 'Your code should use the map method.');" }, { - "text": "rating应等于[{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].", - "testString": "assert(JSON.stringify(rating) === JSON.stringify([{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]), 'rating应等于[{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].');" + "text": "rating should equal [{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].", + "testString": "assert(JSON.stringify(rating) === JSON.stringify([{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}]), 'rating should equal [{\"title\":\"Inception\",\"rating\":\"8.8\"},{\"title\":\"Interstellar\",\"rating\":\"8.6\"},{\"title\":\"The Dark Knight\",\"rating\":\"9.0\"},{\"title\":\"Batman Begins\",\"rating\":\"8.3\"},{\"title\":\"Avatar\",\"rating\":\"7.9\"}].');" } ], "solutions": [], @@ -459,7 +459,7 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var watchList = [", " { ", " \"Title\": \"Inception\",", @@ -573,14 +573,14 @@ " }", "];", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "", "var rating = [];", "for(var i=0; i < watchList.length; i++){", " rating.push({title: watchList[i][\"Title\"], rating: watchList[i][\"imdbRating\"]});", "}", "", - "// 请在本行以上添加你的代码", + "// Add your code above this line", "", "console.log(rating); " ], @@ -593,21 +593,21 @@ "id": "587d7b8f367417b2b2512b62", "title": "Implement map on a Prototype", "description": [ - "我们之前用map方法(即Array.prototype.map())返回一个与调用它的数组长度相同的数组。只要它的回调函数不改变原始数组,它就不会改变原始数组。", - "换句话说,map是一个纯函数,它的输出仅取决于输入的数组和作为参数传入的回调函数。", - "为了加深对map方法的理解,现在我们来用forArray.prototype.forEach()自己实现一下这个方法。", - "注意:纯函数可以改变其作用域内定义的局部变量,但我们最好不要这样做。", + "As you have seen from applying Array.prototype.map(), or simply map() earlier, the map method returns an array of the same length as the one it was called on. It also doesn't alter the original array, as long as its callback function doesn't.", + "In other words, map is a pure function, and its output depends solely on its inputs. Plus, it takes another function as its argument.", + "It would teach us a lot about map to try to implement a version of it that behaves exactly like the Array.prototype.map() with a for loop or Array.prototype.forEach().", + "Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.", "
", - "写一个和Array.prototype.map()一样的Array.prototype.myMap()。你可以用for循环或者forEach方法。" + "Write your own Array.prototype.myMap(), which should behave exactly like Array.prototype.map(). You may use a for loop or the forEach method." ], "tests": [ { - "text": "new_s应等于[46, 130, 196, 10]。", - "testString": "assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]), 'new_s应等于[46, 130, 196, 10]。');" + "text": "new_s should equal [46, 130, 196, 10].", + "testString": "assert(JSON.stringify(new_s) === JSON.stringify([46, 130, 196, 10]), 'new_s should equal [46, 130, 196, 10].');" }, { - "text": "不能使用map方法。", - "testString": "assert(!code.match(/\\.map/g), '不能使用map方法。');" + "text": "Your code should not use the map method.", + "testString": "assert(!code.match(/\\.map/g), 'Your code should not use the map method.');" } ], "solutions": [], @@ -619,14 +619,14 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global Array", "var s = [23, 65, 98, 5];", "", "Array.prototype.myMap = function(callback){", " var newArray = [];", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", " return newArray;", "", "};", @@ -644,27 +644,27 @@ "id": "587d7b8f367417b2b2512b63", "title": "Use the filter Method to Extract Data from an Array", "description": [ - "另一个有用的数组方法是filter()(即Array.prototype.filter())。filter方法会返回一个长度不大于原始数组的新数组。", - "和map一样,Filter不会改变原始数组,它接收一个回调函数,将回调内的逻辑应用于数组的每个元素。新数组包含根据回调函数内条件返回 true 的元素。", + "Another useful array function is Array.prototype.filter(), or simply filter(). The filter method returns a new array which is at most as long as the original array, but usually has fewer items.", + "Filter doesn't alter the original array, just like map. It takes a callback function that applies the logic inside the callback on each element of the array. If an element returns true based on the criteria in the callback function, then it is included in the new array.", "
", - "watchList是包含一些电影信息的对象。结合filtermap返回一个只包含titlerating属性的新数组,并且imdbRating值大于或等于 8.0。请注意,评级值在对象中保存为字符串,你可能需要将它转换成数字来执行运算。" + "The variable watchList holds an array of objects with information on several movies. Use a combination of filter and map to return a new array of objects with only title and rating keys, but where imdbRating is greater than or equal to 8.0. Note that the rating values are saved as strings in the object and you may want to convert them into numbers to perform mathematical operations on them." ], "tests": [ { - "text": "watchList应保持不变。", - "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList应保持不变。');" + "text": "The watchList variable should not change.", + "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList variable should not change.');" }, { - "text": "应使用filter方法。", - "testString": "assert(code.match(/\\.filter/g), '应使用filter方法。');" + "text": "Your code should use the filter method.", + "testString": "assert(code.match(/\\.filter/g), 'Your code should use the filter method.');" }, { - "text": "不能使用for循环。", - "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), '不能使用for循环。');" + "text": "Your code should not use a for loop.", + "testString": "assert(!code.match(/for\\s*?\\(.+?\\)/g), 'Your code should not use a for loop.');" }, { - "text": "filteredList应等于[{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]。", - "testString": "assert.deepEqual(filteredList, [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}], 'filteredList应等于[{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}]。');" + "text": "filteredList should equal [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}].", + "testString": "assert.deepEqual(filteredList, [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}], 'filteredList should equal [{\"title\": \"Inception\",\"rating\": \"8.8\"},{\"title\": \"Interstellar\",\"rating\": \"8.6\"},{\"title\": \"The Dark Knight\",\"rating\": \"9.0\"},{\"title\": \"Batman Begins\",\"rating\": \"8.3\"}].');" } ], "solutions": [], @@ -676,7 +676,7 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var watchList = [", " { ", " \"Title\": \"Inception\",", @@ -790,11 +790,11 @@ " }", "];", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "", "var filteredList;", "", - "// 请在本行以上添加你的代码", + "// Add your code above this line", "", "console.log(filteredList); " ], @@ -807,20 +807,19 @@ "id": "587d7b8f367417b2b2512b64", "title": "Implement the filter Method on a Prototype", "description": [ - "为了加深对filter的理解,现在我们来自己实现一下Array.prototype.filter()方法。", - "可以用for循环或Array.prototype.forEach()。", - "请注意:纯函数可以改变其作用域内定义的局部变量,但我们最好不要这样做。", + "It would teach us a lot about the filter method if we try to implement a version of it that behaves exactly like Array.prototype.filter(). It can use either a for loop or Array.prototype.forEach().", + "Note: A pure function is allowed to alter local variables defined within its scope, although, it's preferable to avoid that as well.", "
", - "编写一个和Array.prototype.filter()功能一样的Array.prototype.myFilter()方法。你可以用for循环或Array.prototype.forEach()方法。" + "Write your own Array.prototype.myFilter(), which should behave exactly like Array.prototype.filter(). You may use a for loop or the Array.prototype.forEach() method." ], "tests": [ { - "text": "new_s应等于[23, 65, 5]。", - "testString": "assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]), 'new_s应等于[23, 65, 5]。');" + "text": "new_s should equal [23, 65, 5].", + "testString": "assert(JSON.stringify(new_s) === JSON.stringify([23, 65, 5]), 'new_s should equal [23, 65, 5].');" }, { - "text": "不能使用filter方法。", - "testString": "assert(!code.match(/\\.filter/g), '不能使用filter方法。');" + "text": "Your code should not use the filter method.", + "testString": "assert(!code.match(/\\.filter/g), 'Your code should not use the filter method.');" } ], "solutions": [], @@ -832,14 +831,14 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global Array", "var s = [23, 65, 98, 5];", "", "Array.prototype.myFilter = function(callback){", " var newArray = [];", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", " return newArray;", "", "};", @@ -857,32 +856,32 @@ "id": "587d7b90367417b2b2512b65", "title": "Return Part of an Array Using the slice Method", "description": [ - "slice方法可以从已有数组中返回指定元素。它接受两个参数,第一个规定从何处开始选取,第二个规定从何处结束选取(不包括该元素)。如果没有传参,则默认为从数组的开头开始到结尾结束,这是复制整个数组的简单方式。slice返回一个新数组,不会修改原始数组。", - "举个例子:", - "
var arr = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\"];
var newArray = arr.slice(1, 3);
// 将新数组设置为 [\"Dog\", \"Tiger\"]
", + "The slice method returns a copy of certain elements of an array. It can take two arguments, the first gives the index of where to begin the slice, the second is the index for where to end the slice (and it's non-inclusive). If the arguments are not provided, the default is to start at the beginning of the array through the end, which is an easy way to make a copy of the entire array. The slice method does not mutate the original array, but returns a new one.", + "Here's an example:", + "
var arr = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\"];
var newArray = arr.slice(1, 3);
// Sets newArray to [\"Dog\", \"Tiger\"]
", "
", - "在sliceArray函数中使用slice方法,给出beginSliceendSlice索引,返回anim数组的一部分,这个函数应返回一个数组。" + "Use the slice method in the sliceArray function to return part of the anim array given the provided beginSlice and endSlice indices. The function should return an array." ], "tests": [ { - "text": "你的代码中应使用slice方法。", - "testString": "assert(code.match(/\\.slice/g), '你的代码中应使用slice方法。');" + "text": "Your code should use the slice method.", + "testString": "assert(code.match(/\\.slice/g), 'Your code should use the slice method.');" }, { - "text": "不能改变inputAnim变量。", - "testString": "assert(JSON.stringify(inputAnim) === JSON.stringify([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"]), '不能改变inputAnim变量。');" + "text": "The inputAnim variable should not change.", + "testString": "assert(JSON.stringify(inputAnim) === JSON.stringify([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"]), 'The inputAnim variable should not change.');" }, { - "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)应返回[\"Dog\", \"Tiger\"]。", - "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)) === JSON.stringify([\"Dog\", \"Tiger\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)应返回[\"Dog\", \"Tiger\"]。');" + "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3) should return [\"Dog\", \"Tiger\"].", + "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3)) === JSON.stringify([\"Dog\", \"Tiger\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 3) should return [\"Dog\", \"Tiger\"].');" }, { - "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)应返回[\"Cat\"]。", - "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)) === JSON.stringify([\"Cat\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)应返回[\"Cat\"]。');" + "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1) should return [\"Cat\"].", + "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1)) === JSON.stringify([\"Cat\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 0, 1) should return [\"Cat\"].');" }, { - "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)应返回[\"Dog\", \"Tiger\", \"Zebra\"]。", - "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)) === JSON.stringify([\"Dog\", \"Tiger\", \"Zebra\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)应返回[\"Dog\", \"Tiger\", \"Zebra\"]。');" + "text": "sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4) should return [\"Dog\", \"Tiger\", \"Zebra\"].", + "testString": "assert(JSON.stringify(sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4)) === JSON.stringify([\"Dog\", \"Tiger\", \"Zebra\"]), 'sliceArray([\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"], 1, 4) should return [\"Dog\", \"Tiger\", \"Zebra\"].');" } ], "solutions": [], @@ -895,10 +894,10 @@ "name": "index", "contents": [ "function sliceArray(anim, beginSlice, endSlice) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "var inputAnim = [\"Cat\", \"Dog\", \"Tiger\", \"Zebra\", \"Ant\"];", "sliceArray(inputAnim, 1, 3);" @@ -912,29 +911,29 @@ "id": "9d7123c8c441eeafaeb5bdef", "title": "Remove Elements from an Array Using slice Instead of splice", "description": [ - "使用数组时经常遇到要删除一些元素并保留数组剩余部分的情况。为此,JavaScript 提供了splice方法,它接收从哪里开始删除项目的索引,和要删除的项目数两个参数。如果没有提供第二个参数,默认情况下是移除到结尾的元素。但splice方法会改变调用它的原始数组。举个例子:", - "
var cities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];
cities.splice(3, 1); // 返回 \"London\" 并将它从 cities 数组删除
// cities 现在是 [\"Chicago\", \"Delhi\", \"Islamabad\", \"Berlin\"]
", - "正如我们在上一次挑战中看到的那样,slice方法不会改变原始数组,而是返回一个可以保存到变量中的新数组。回想一下,slice方法接收两个参数,从开始索引开始选取到结束(不包括该元素),并在新数组中返回这些元素。使用slice方法替代splice有助于避免数组变化产生的副作用。", + "A common pattern while working with arrays is when you want to remove items and keep the rest of the array. JavaScript offers the splice method for this, which takes arguments for the index of where to start removing items, then the number of items to remove. If the second argument is not provided, the default is to remove items through the end. However, the splice method mutates the original array it is called on. Here's an example:", + "
var cities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];
cities.splice(3, 1); // Returns \"London\" and deletes it from the cities array
// cities is now [\"Chicago\", \"Delhi\", \"Islamabad\", \"Berlin\"]
", + "As we saw in the last challenge, the slice method does not mutate the original array, but returns a new one which can be saved into a variable. Recall that the slice method takes two arguments for the indices to begin and end the slice (the end is non-inclusive), and returns those items in a new array. Using the slice method instead of splice helps to avoid any array-mutating side effects.", "
", - "用slice代替splice重写nonMutatingSplice函数。将cities数组长度限制为3,并返回一个仅包含前 3 项的新数组。", - "不要改变提供给函数的原始数组。" + "Rewrite the function nonMutatingSplice by using slice instead of splice. It should limit the provided cities array to a length of 3, and return a new array with only the first three items.", + "Do not mutate the original array provided to the function." ], "tests": [ { - "text": "你应该使用slice方法。", - "testString": "assert(code.match(/\\.slice/g), '你应该使用slice方法。');" + "text": "Your code should use the slice method.", + "testString": "assert(code.match(/\\.slice/g), 'Your code should use the slice method.');" }, { - "text": "你不应该使用splice方法。", - "testString": "assert(!code.match(/\\.splice/g), '你不应该使用splice方法。;" + "text": "Your code should not use the splice method.", + "testString": "assert(!code.match(/\\.splice/g), 'Your code should not use the splice method.');" }, { - "text": "不能改变inputCities数组。", - "testString": "assert(JSON.stringify(inputCities) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]), '不能改变inputCities数组。');" + "text": "The inputCities array should not change.", + "testString": "assert(JSON.stringify(inputCities) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]), 'The inputCities array should not change.');" }, { - "text": "nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])应返回[\"Chicago\", \"Delhi\", \"Islamabad\"]。", - "testString": "assert(JSON.stringify(nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\"]), 'nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])应返回[\"Chicago\", \"Delhi\", \"Islamabad\"]。');" + "text": "nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]) should return [\"Chicago\", \"Delhi\", \"Islamabad\"].", + "testString": "assert(JSON.stringify(nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"])) === JSON.stringify([\"Chicago\", \"Delhi\", \"Islamabad\"]), 'nonMutatingSplice([\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"]) should return [\"Chicago\", \"Delhi\", \"Islamabad\"].');" } ], "solutions": [], @@ -947,10 +946,10 @@ "name": "index", "contents": [ "function nonMutatingSplice(cities) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " return cities.splice(3);", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "var inputCities = [\"Chicago\", \"Delhi\", \"Islamabad\", \"London\", \"Berlin\"];", "nonMutatingSplice(inputCities);" @@ -964,27 +963,27 @@ "id": "587d7da9367417b2b2512b66", "title": "Combine Two Arrays Using the concat Method", "description": [ - "Concatenation意思是连接元素到尾部。同理,JavaScript 为字符串和数组提供了concat方法。对数组来说,在一个数组上调用concat方法,然后提供另一个数组作为参数添加到第一个数组末尾,返回一个新数组,不会改变任何一个原始数组。举个例子:", - "
[1, 2, 3].concat([4, 5, 6]);
// 返回新数组 [1, 2, 3, 4, 5, 6]
", + "Concatenation means to join items end to end. JavaScript offers the concat method for both strings and arrays that work in the same way. For arrays, the method is called on one, then another array is provided as the argument to concat, which is added to the end of the first array. It returns a new array and does not mutate either of the original arrays. Here's an example:", + "
[1, 2, 3].concat([4, 5, 6]);
// Returns a new array [1, 2, 3, 4, 5, 6]
", "
", - "在nonMutatingConcat函数里使用concat,将attach拼接到original尾部,返回拼接后的数组。" + "Use the concat method in the nonMutatingConcat function to concatenate attach to the end of original. The function should return the concatenated array." ], "tests": [ { - "text": "你应该使用concat方法。", - "testString": "assert(code.match(/\\.concat/g), '你应该使用concat方法。');" + "text": "Your code should use the concat method.", + "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat method.');" }, { - "text": "不能改变first数组。", - "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), '不能改变first数组。');" + "text": "The first array should not change.", + "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The first array should not change.');" }, { - "text": "不能改变second数组。", - "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), '不能改变second数组。');" + "text": "The second array should not change.", + "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The second array should not change.');" }, { - "text": "nonMutatingConcat([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。", - "testString": "assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingConcat([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。');" + "text": "nonMutatingConcat([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].", + "testString": "assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingConcat([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].');" } ], "solutions": [], @@ -997,10 +996,10 @@ "name": "index", "contents": [ "function nonMutatingConcat(original, attach) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "var first = [1, 2, 3];", "var second = [4, 5];", @@ -1015,33 +1014,33 @@ "id": "587d7da9367417b2b2512b67", "title": "Add Elements to the End of an Array Using concat Instead of push", "description": [ - "函数式编程就是创建和使用 non-mutating 函数。", - "最后一个挑战介绍了concat方法,这是一种在不改变原始数组的前提下,将数组组合成新数组的方法。将concat方法与push方法做比较,Push将元素添加到调用它的数组的末尾,这样会改变该数组。举个例子:", - "
var arr = [1, 2, 3];
arr.push([4, 5, 6]);
// arr 变成了 [1, 2, 3, [4, 5, 6]]
// 不是函数式编程
", - "Concat方法可以将新项目添加到数组末尾,而不产生任何变更副作用。", + "Functional programming is all about creating and using non-mutating functions.", + "The last challenge introduced the concat method as a way to combine arrays into a new one without mutating the original arrays. Compare concat to the push method. Push adds an item to the end of the same array it is called on, which mutates that array. Here's an example:", + "
var arr = [1, 2, 3];
arr.push([4, 5, 6]);
// arr is changed to [1, 2, 3, [4, 5, 6]]
// Not the functional programming way
", + "Concat offers a way to add new items to the end of an array without any mutating side effects.", "
", - "修改nonMutatingPush函数,用concat替代pushnewItem添加到original末尾,该函数应返回一个数组。" + "Change the nonMutatingPush function so it uses concat to add newItem to the end of original instead of push. The function should return an array." ], "tests": [ { - "text": "应该使用concat方法。", - "testString": "assert(code.match(/\\.concat/g), '应该使用concat方法。');" + "text": "Your code should use the concat method.", + "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat method.');" }, { - "text": "不能使用push方法。", - "testString": "assert(!code.match(/\\.push/g), '不能使用push方法。');" + "text": "Your code should not use the push method.", + "testString": "assert(!code.match(/\\.push/g), 'Your code should not use the push method.');" }, { - "text": "不能改变first数组。", - "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), '不能改变first数组。');" + "text": "The first array should not change.", + "testString": "assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), 'The first array should not change.');" }, { - "text": "不能改变second数组。", - "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), '不能改变second数组。');" + "text": "The second array should not change.", + "testString": "assert(JSON.stringify(second) === JSON.stringify([4, 5]), 'The second array should not change.');" }, { - "text": "nonMutatingPush([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。", - "testString": "assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingPush([1, 2, 3], [4, 5])应返回[1, 2, 3, 4, 5]。');" + "text": "nonMutatingPush([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].", + "testString": "assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), 'nonMutatingPush([1, 2, 3], [4, 5]) should return [1, 2, 3, 4, 5].');" } ], "solutions": [], @@ -1054,10 +1053,10 @@ "name": "index", "contents": [ "function nonMutatingPush(original, newItem) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " return original.push(newItem);", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "var first = [1, 2, 3];", "var second = [4, 5];", @@ -1072,29 +1071,29 @@ "id": "587d7da9367417b2b2512b68", "title": "Use the reduce Method to Analyze Data", "description": [ - "reduce()(即Array.prototype.reduce()),是 JavaScript 所有数组操作中最通用的方法。几乎可以用reduce方法解决所有数组处理问题。", - "filtermap方法不支持对数组中两个不同元素的交互。举个例子,如果你想把数组中的元素拿来比较或者相加,用filtermap是做不到的。", - "reduce方法允许更通用的数组处理方式,而且filtermap方法都可以当作是reduce的特殊实现。", - "然而,在我们介绍它们的特殊实现之前,我们先来练习使用reduce。", + "Array.prototype.reduce(), or simply reduce(), is the most general of all array operations in JavaScript. You can solve almost any array processing problem using the reduce method.", + "This is not the case with the filter and map methods since they do not allow interaction between two different elements of the array. For example, if you want to compare elements of the array, or add them together, filter or map could not process that.", + "The reduce method allows for more general forms of array processing, and it's possible to show that both filter and map can be derived as a special application of reduce.", + "However, before we get there, let's practice using reduce first.", "
", - "watchList变量中包含一组存有多部电影信息对象。使用reduce查找由 Christopher Nolan 导演的电影directed by Christopher Nolan的 IMDB 评级。回想一下之前的挑战,如何filter数据,以及使用map来获取你想要的数据。你可能需要创建一些变量,但是请将最后的平均值保存到averageRating变量中。请注意,评级在对象中是字符串,需要将其转换为数字再用于数学运算。" + "The variable watchList holds an array of objects with information on several movies. Use reduce to find the average IMDB rating of the movies directed by Christopher Nolan. Recall from prior challenges how to filter data and map over it to pull what you need. You may need to create other variables, but save the final average into the variable averageRating. Note that the rating values are saved as strings in the object and need to be converted into numbers before they are used in any mathematical operations." ], "tests": [ { - "text": "watchList应保持不变。", - "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'watchList应保持不变。');" + "text": "The watchList variable should not change.", + "testString": "assert(watchList[0].Title === \"Inception\" && watchList[4].Director == \"James Cameron\", 'The watchList variable should not change.');" }, { - "text": "应该使用reduce方法。", - "testString": "assert(code.match(/\\.reduce/g), '应该使用reduce方法。');" + "text": "Your code should use the reduce method.", + "testString": "assert(code.match(/\\.reduce/g), 'Your code should use the reduce method.');" }, { - "text": "The averageRating应等于 8.675。", - "testString": "assert(averageRating == 8.675, 'The averageRating应等于 8.675。');" + "text": "The averageRating should equal 8.675.", + "testString": "assert(averageRating == 8.675, 'The averageRating should equal 8.675.');" }, { - "text": "不能使用for循环。", - "testString": "assert(!code.match(/for\\s*?\\(.*\\)/g), '不能使用for循环。');" + "text": "Your code should not use a for loop.", + "testString": "assert(!code.match(/for\\s*?\\(.*\\)/g), 'Your code should not use a for loop.');" } ], "solutions": [], @@ -1110,7 +1109,7 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var watchList = [", " { ", " \"Title\": \"Inception\",", @@ -1224,11 +1223,11 @@ " }", "];", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "", "var averageRating;", "", - "// 请在本行以上添加你的代码", + "// Add your code above this line", "", "console.log(averageRating); " ], @@ -1241,29 +1240,29 @@ "id": "587d7da9367417b2b2512b69", "title": "Sort an Array Alphabetically using the sort Method", "description": [ - "sort方法可以根据回调函数对数组元素进行排序。", - "举个例子:", - "
function ascendingOrder(arr) {
  return arr.sort(function(a, b) {
    return a - b;
  });
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a < b;
  });
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// 返回 ['z', 's', 'l', 'h', 'b']
", - "注意:提倡使用回调函数来指定如何对数组项进行排序。JavaScript 的默认排序算法是按照 Unicode 字符编码排序的,所以可能会返回意料之外的结果。", + "The sort method sorts the elements of an array according to the callback function.", + "For example:", + "
function ascendingOrder(arr) {
  return arr.sort(function(a, b) {
    return a - b;
  });
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]

function reverseAlpha(arr) {
  return arr.sort(function(a, b) {
    return a < b;
  });
}
reverseAlpha(['l', 'h', 'z', 'b', 's']);
// Returns ['z', 's', 'l', 'h', 'b']
", + "Note: It's encouraged to provide a callback function to specify how to sort the array items. JavaScript's default sorting method is by string Unicode point value, which may return unexpected results.", "
", - "在alphabeticalOrder函数中使用sort方法对arr中的元素按照字母顺序排列。" + "Use the sort method in the alphabeticalOrder function to sort the elements of arr in alphabetical order." ], "tests": [ { - "text": "应该使用sort方法。", - "testString": "assert(code.match(/\\.sort/g), '应该使用sort方法。');" + "text": "Your code should use the sort method.", + "testString": "assert(code.match(/\\.sort/g), 'Your code should use the sort method.');" }, { - "text": "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])应返回[\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]。", - "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])) === JSON.stringify([\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]), 'alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])应返回[\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]。');" + "text": "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]) should return [\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"].", + "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"])) === JSON.stringify([\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"]), 'alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]) should return [\"a\", \"a\", \"c\", \"d\", \"g\", \"z\"].');" }, { - "text": "alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])应返回[\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]。", - "testString": "assert(JSON.stringify(alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])) === JSON.stringify([\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]), 'alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])应返回[\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]。');" + "text": "alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"]) should return [\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"].", + "testString": "assert(JSON.stringify(alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"])) === JSON.stringify([\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"]), 'alphabeticalOrder([\"x\", \"h\", \"a\", \"m\", \"n\", \"m\"]) should return [\"a\", \"h\", \"m\", \"m\", \"n\", \"x\"].');" }, { - "text": "alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])应返回[\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]。", - "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])) === JSON.stringify([\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]), 'alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])应返回[\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]。');" + "text": "alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"]) should return [\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"].", + "testString": "assert(JSON.stringify(alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"])) === JSON.stringify([\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"]), 'alphabeticalOrder([\"a\", \"a\", \"a\", \"a\", \"x\", \"t\"]) should return [\"a\", \"a\", \"a\", \"a\", \"t\", \"x\"].');" } ], "solutions": [], @@ -1276,10 +1275,10 @@ "name": "index", "contents": [ "function alphabeticalOrder(arr) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "alphabeticalOrder([\"a\", \"d\", \"c\", \"a\", \"z\", \"g\"]);" ], @@ -1292,26 +1291,26 @@ "id": "587d7da9367417b2b2512b6a", "title": "Return a Sorted Array Without Changing the Original Array", "description": [ - "sort方法会产生改变原始数组中元素顺序的副作用。换句话说,它会改变数组的位置。避免这种情况的一种方法是先将空数组连接到正在排序的数组上(记住concat返回一个新数组),再用sort方法。", + "A side effect of the sort method is that it changes the order of the elements in the original array. In other words, it mutates the array in place. One way to avoid this is to first concatenate an empty array to the one being sorted (remember that concat returns a new array), then run the sort method.", "
", - "在nonMutatingSort函数中使用sort方法对数组中的元素按升序进行排列。函数不能改变globalArray变量,应返回一个新数组。" + "Use the sort method in the nonMutatingSort function to sort the elements of an array in ascending order. The function should return a new array, and not mutate the globalArray variable." ], "tests": [ { - "text": "应该使用sort方法。", - "testString": "assert(code.match(/\\.sort/g), '应该使用sort方法。');" + "text": "Your code should use the sort method.", + "testString": "assert(code.match(/\\.sort/g), 'Your code should use the sort method.');" }, { - "text": "应该使用concat方法。", - "testString": "assert(code.match(/\\.concat/g), '应该使用concat方法。');" + "text": "Your code should use the concat method.", + "testString": "assert(code.match(/\\.concat/g), 'Your code should use the concat method.');" }, { - "text": "globalArray variable 应保持不变。", - "testString": "assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]), 'globalArray variable 应保持不变。');" + "text": "The globalArray variable should not change.", + "testString": "assert(JSON.stringify(globalArray) === JSON.stringify([5, 6, 3, 2, 9]), 'The globalArray variable should not change.');" }, { - "text": "nonMutatingSort(globalArray)应返回[2, 3, 5, 6, 9]。", - "testString": "assert(JSON.stringify(nonMutatingSort(globalArray)) === JSON.stringify([2, 3, 5, 6, 9]), 'nonMutatingSort(globalArray)应返回[2, 3, 5, 6, 9]。');" + "text": "nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].", + "testString": "assert(JSON.stringify(nonMutatingSort(globalArray)) === JSON.stringify([2, 3, 5, 6, 9]), 'nonMutatingSort(globalArray) should return [2, 3, 5, 6, 9].');" } ], "solutions": [], @@ -1325,10 +1324,10 @@ "contents": [ "var globalArray = [5, 6, 3, 2, 9];", "function nonMutatingSort(arr) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "nonMutatingSort(globalArray);" ], @@ -1341,29 +1340,29 @@ "id": "587d7daa367417b2b2512b6b", "title": "Split a String into an Array Using the split Method", "description": [ - "split方法用于把字符串分割成字符串数组。接收一个分隔符参数,分隔符可以是用于分解字符串或正则表达式的字符。举个例子,如果分隔符是空格,你会得到一个单词数组;如果分隔符是空字符串,你会得到一个由字符串中每个字符组成的数组。", - "下面是两个用空格分隔一个字符串的例子,另一个是用数字的正则表达式分隔:", + "The split method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.", + "Here are two examples that split one string by spaces, then another by digits using a regular expression:", "
var str = \"Hello World\";
var bySpace = str.split(\" \");
// Sets bySpace to [\"Hello\", \"World\"]

var otherString = \"How9are7you2today\";
var byDigits = str.split(/\\d/);
// Sets byDigits to [\"How\", \"are\", \"you\", \"today\"]
", - "因为字符串是固定的,split方法可以更简单的操作它们。", + "Since strings are immutable, the split method makes it easier to work with them.", "
", - "在splitify函数中用split方法将str分割成单词数组,这个方法应该返回一个数组。单词不一定都是用空格分隔,所以数组中不应包含标点符号。" + "Use the split method inside the splitify function to split str into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation." ], "tests": [ { - "text": "应该使用split方法。", - "testString": "assert(code.match(/\\.split/g), '应该使用split方法。');" + "text": "Your code should use the split method.", + "testString": "assert(code.match(/\\.split/g), 'Your code should use the split method.');" }, { - "text": "splitify(\"Hello World,I-am code\")应返回[\"Hello\", \"World\", \"I\", \"am\", \"code\"]。", - "testString": "assert(JSON.stringify(splitify(\"Hello World,I-am code\")) === JSON.stringify([\"Hello\", \"World\", \"I\", \"am\", \"code\"]), 'splitify(\"Hello World,I-am code\")应返回[\"Hello\", \"World\", \"I\", \"am\", \"code\"]。');" + "text": "splitify(\"Hello World,I-am code\") should return [\"Hello\", \"World\", \"I\", \"am\", \"code\"].", + "testString": "assert(JSON.stringify(splitify(\"Hello World,I-am code\")) === JSON.stringify([\"Hello\", \"World\", \"I\", \"am\", \"code\"]), 'splitify(\"Hello World,I-am code\") should return [\"Hello\", \"World\", \"I\", \"am\", \"code\"].');" }, { - "text": "splitify(\"Earth-is-our home\")应返回[\"Earth\", \"is\", \"our\", \"home\"]。", - "testString": "assert(JSON.stringify(splitify(\"Earth-is-our home\")) === JSON.stringify([\"Earth\", \"is\", \"our\", \"home\"]), 'splitify(\"Earth-is-our home\")应返回[\"Earth\", \"is\", \"our\", \"home\"]。');" + "text": "splitify(\"Earth-is-our home\") should return [\"Earth\", \"is\", \"our\", \"home\"].", + "testString": "assert(JSON.stringify(splitify(\"Earth-is-our home\")) === JSON.stringify([\"Earth\", \"is\", \"our\", \"home\"]), 'splitify(\"Earth-is-our home\") should return [\"Earth\", \"is\", \"our\", \"home\"].');" }, { - "text": "splitify(\"This.is.a-sentence\")应返回[\"This\", \"is\", \"a\", \"sentence\"]。", - "testString": "assert(JSON.stringify(splitify(\"This.is.a-sentence\")) === JSON.stringify([\"This\", \"is\", \"a\", \"sentence\"]), 'splitify(\"This.is.a-sentence\")应返回[\"This\", \"is\", \"a\", \"sentence\"]。');" + "text": "splitify(\"This.is.a-sentence\") should return [\"This\", \"is\", \"a\", \"sentence\"].", + "testString": "assert(JSON.stringify(splitify(\"This.is.a-sentence\")) === JSON.stringify([\"This\", \"is\", \"a\", \"sentence\"]), 'splitify(\"This.is.a-sentence\") should return [\"This\", \"is\", \"a\", \"sentence\"].');" } ], "solutions": [], @@ -1378,10 +1377,10 @@ "name": "index", "contents": [ "function splitify(str) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "splitify(\"Hello World,I-am code\");" ], @@ -1394,36 +1393,36 @@ "id": "587d7daa367417b2b2512b6c", "title": "Combine an Array into a String Using the join Method", "description": [ - "join方法用来把数组中的所有元素放入一个字符串,通过指定的分隔符参数进行分隔。", - "举个例子:", + "The join method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string.", + "Here's an example:", "
var arr = [\"Hello\", \"World\"];
var str = arr.join(\" \");
// Sets str to \"Hello World\"
", "
", - "在函数sentensify内用join方法(及其他方法)用字符串str中的单词造句,这个函数应返回一个字符串。举个例子,\"I-like-Star-Wars\" 会被转换成 \"I like Star Wars\"。在此挑战中请勿使用replace方法。" + "Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str. The function should return a string. For example, \"I-like-Star-Wars\" would be converted to \"I like Star Wars\". For this challenge, do not use the replace method." ], "tests": [ { - "text": "应该使用join方法。", - "testString": "assert(code.match(/\\.join/g), '应该使用join方法。');" + "text": "Your code should use the join method.", + "testString": "assert(code.match(/\\.join/g), 'Your code should use the join method.');" }, { - "text": "不能使用replace方法。", - "testString": "assert(!code.match(/\\.replace/g), '不能使用replace方法。');" + "text": "Your code should not use the replace method.", + "testString": "assert(!code.match(/\\.replace/g), 'Your code should not use the replace method.');" }, { - "text": "sentensify(\"May-the-force-be-with-you\")应返回一个字符串", - "testString": "assert(typeof sentensify(\"May-the-force-be-with-you\") === \"string\", 'sentensify(\"May-the-force-be-with-you\")应返回一个字符串');" + "text": "sentensify(\"May-the-force-be-with-you\") should return a string.", + "testString": "assert(typeof sentensify(\"May-the-force-be-with-you\") === \"string\", 'sentensify(\"May-the-force-be-with-you\") should return a string.');" }, { - "text": "sentensify(\"May-the-force-be-with-you\")应返回\"May the force be with you\"。", - "testString": "assert(sentensify(\"May-the-force-be-with-you\") === \"May the force be with you\", 'sentensify(\"May-the-force-be-with-you\")应返回\"May the force be with you\"。');" + "text": "sentensify(\"May-the-force-be-with-you\") should return \"May the force be with you\".", + "testString": "assert(sentensify(\"May-the-force-be-with-you\") === \"May the force be with you\", 'sentensify(\"May-the-force-be-with-you\") should return \"May the force be with you\".');" }, { - "text": "sentensify(\"The.force.is.strong.with.this.one\")应返回\"The force is strong with this one\"。", - "testString": "assert(sentensify(\"The.force.is.strong.with.this.one\") === \"The force is strong with this one\", 'sentensify(\"The.force.is.strong.with.this.one\")应返回\"The force is strong with this one\"。');" + "text": "sentensify(\"The.force.is.strong.with.this.one\") should return \"The force is strong with this one\".", + "testString": "assert(sentensify(\"The.force.is.strong.with.this.one\") === \"The force is strong with this one\", 'sentensify(\"The.force.is.strong.with.this.one\") should return \"The force is strong with this one\".');" }, { - "text": "sentensify(\"There,has,been,an,awakening\")应返回\"There has been an awakening\"。", - "testString": "assert(sentensify(\"There,has,been,an,awakening\") === \"There has been an awakening\", 'sentensify(\"There,has,been,an,awakening\")应返回\"There has been an awakening\"。');" + "text": "sentensify(\"There,has,been,an,awakening\") should return \"There has been an awakening\".", + "testString": "assert(sentensify(\"There,has,been,an,awakening\") === \"There has been an awakening\", 'sentensify(\"There,has,been,an,awakening\") should return \"There has been an awakening\".');" } ], "solutions": [], @@ -1438,10 +1437,10 @@ "name": "index", "contents": [ "function sentensify(str) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "sentensify(\"May-the-force-be-with-you\");" ], @@ -1454,40 +1453,40 @@ "id": "587d7dab367417b2b2512b6d", "title": "Apply Functional Programming to Convert Strings to URL Slugs", "description": [ - "最后几个挑战中涵盖了许多符合函数式编程原则并在处理数组和字符串中非常有用的方法。我们还学习了强大的、可以将问题简化为更简单形式的reduce方法,从计算平均值到排序,任何数组操作都可以用它来实现。回想一下,mapfilter方法都是reduce的特殊实现。", - "让我们把学到的知识结合起来解决一个实际问题。", - "许多内容管理站点(CMS)为了让添加书签更简单,会将帖子的标题添加到 URL 上。举个例子,如果你写了一篇标题为 \"Stop Using Reduce\" 的帖子,URL很可能会包含某种形式的标题字符串 (如:\".../stop-using-reduce\"),你可能已经在 freeCodeCamp 网站上注意到了这一点。", + "The last several challenges covered a number of useful array and string methods that follow functional programming principles. We've also learned about reduce, which is a powerful method used to reduce problems to simpler forms. From computing averages to sorting, any array operation can be achieved by applying it. Recall that map and filter are special cases of reduce.", + "Let's combine what we've learned to solve a practical problem.", + "Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled \"Stop Using Reduce\", it's likely the URL would have some form of the title string in it (\".../stop-using-reduce\"). You may have already noticed this on the freeCodeCamp site.", "
", - "填写urlSlug函数,使其转换字符串title带有连字符号的 URL 版本。您可以使用本节中介绍的任何方法,但不要用replace方法。以下是本次挑战的要求:", - "输入包含空格和标题大小写单词的字符串", - "输出字符串,单词之间的空格用连字符(-)替换", - "输出应该是小写字母", - "输出不应有任何空格" + "Fill in the urlSlug function so it converts a string title and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace. Here are the requirements:", + "The input is a string with spaces and title-cased words", + "The output is a string with the spaces between words replaced by a hyphen (-)", + "The output should be all lower-cased letters", + "The output should not have any spaces" ], "tests": [ { - "text": "globalTitle变量应保持不变。", - "testString": "assert(globalTitle === \"Winter Is Coming\", 'globalTitle变量应保持不变。');" + "text": "The globalTitle variable should not change.", + "testString": "assert(globalTitle === \"Winter Is Coming\", 'The globalTitle variable should not change.');" }, { - "text": "在此挑战中不能使用replace方法。", - "testString": "assert(!code.match(/\\.replace/g), '在此挑战中不能使用replace方法。');" + "text": "Your code should not use the replace method for this challenge.", + "testString": "assert(!code.match(/\\.replace/g), 'Your code should not use the replace method for this challenge.');" }, { - "text": "urlSlug(\"Winter Is Coming\")应返回\"winter-is-coming\"。", - "testString": "assert(urlSlug(\"Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\"Winter Is Coming\")应返回\"winter-is-coming\"。');" + "text": "urlSlug(\"Winter Is Coming\") should return \"winter-is-coming\".", + "testString": "assert(urlSlug(\"Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\"Winter Is Coming\") should return \"winter-is-coming\".');" }, { - "text": "urlSlug(\" Winter Is  Coming\")应返回\"winter-is-coming\"。", - "testString": "assert(urlSlug(\" Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\" Winter Is  Coming\")应返回\"winter-is-coming\"。');" + "text": "urlSlug(\" Winter Is  Coming\") should return \"winter-is-coming\".", + "testString": "assert(urlSlug(\" Winter Is Coming\") === \"winter-is-coming\", 'urlSlug(\" Winter Is  Coming\") should return \"winter-is-coming\".');" }, { - "text": "urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")应返回\"a-mind-needs-books-like-a-sword-needs-a-whetstone\"。", - "testString": "assert(urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") === \"a-mind-needs-books-like-a-sword-needs-a-whetstone\", 'urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\")应返回\"a-mind-needs-books-like-a-sword-needs-a-whetstone\"。');" + "text": "urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") should return \"a-mind-needs-books-like-a-sword-needs-a-whetstone\".", + "testString": "assert(urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") === \"a-mind-needs-books-like-a-sword-needs-a-whetstone\", 'urlSlug(\"A Mind Needs Books Like A Sword Needs A Whetstone\") should return \"a-mind-needs-books-like-a-sword-needs-a-whetstone\".');" }, { - "text": "urlSlug(\"Hold The Door\")应返回\"hold-the-door\"。", - "testString": "assert(urlSlug(\"Hold The Door\") === \"hold-the-door\", 'urlSlug(\"Hold The Door\")应返回\"hold-the-door\"。');" + "text": "urlSlug(\"Hold The Door\") should return \"hold-the-door\".", + "testString": "assert(urlSlug(\"Hold The Door\") === \"hold-the-door\", 'urlSlug(\"Hold The Door\") should return \"hold-the-door\".');" } ], "solutions": [], @@ -1502,17 +1501,17 @@ "ext": "js", "name": "index", "contents": [ - "// 全局变量", + "// the global variable", "var globalTitle = \"Winter Is Coming\";", "", - "// 请在本行以下添加你的代码", + "// Add your code below this line", "function urlSlug(title) {", " ", " ", "}", - "// 请在本行以上添加你的代码", + "// Add your code above this line", "", - "var winterComing = urlSlug(globalTitle); // 应为 \"winter-is-coming\"" + "var winterComing = urlSlug(globalTitle); // Should be \"winter-is-coming\"" ], "head": [], "tail": [] @@ -1523,28 +1522,28 @@ "id": "587d7dab367417b2b2512b6e", "title": "Use the every Method to Check that Every Element in an Array Meets a Criteria", "description": [ - "every方法用于检测数组所有元素是否都符合指定条件。如果所有元素满足条件,返回布尔值true,反之返回false。", - "举个例子,下面的代码检测数组numbers的所有元素是否都小于 10:", - "
var numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) {
  return currentValue < 10;
});
// 返回 false
", + "The every method works with arrays to check if every element passes a particular test. It returns a Boolean value - true if all values meet the criteria, false if not.", + "For example, the following code would check if every element in the numbers array is less than 10:", + "
var numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) {
  return currentValue < 10;
});
// Returns false
", "
", - "在checkPositive函数中使用every方法检查arr中是否所有元素都是正数,函数应返回一个布尔值。" + "Use the every method inside the checkPositive function to check if every element in arr is positive. The function should return a Boolean value." ], "tests": [ { - "text": "应该使用every方法。", - "testString": "assert(code.match(/\\.every/g), '应该使用every方法。');" + "text": "Your code should use the every method.", + "testString": "assert(code.match(/\\.every/g), 'Your code should use the every method.');" }, { - "text": "checkPositive([1, 2, 3, -4, 5])应返回false。", - "testString": "assert(!checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])应返回false。');" + "text": "checkPositive([1, 2, 3, -4, 5]) should return false.", + "testString": "assert(!checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5]) should return false.');" }, { - "text": "checkPositive([1, 2, 3, 4, 5])应返回true。", - "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])应返回true。');" + "text": "checkPositive([1, 2, 3, 4, 5]) should return true.", + "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5]) should return true.');" }, { - "text": "checkPositive([1, -2, 3, -4, 5])应返回false。", - "testString": "assert(!checkPositive([1, -2, 3, -4, 5]), 'checkPositive([1, -2, 3, -4, 5])应返回false。');" + "text": "checkPositive([1, -2, 3, -4, 5]) should return false.", + "testString": "assert(!checkPositive([1, -2, 3, -4, 5]), 'checkPositive([1, -2, 3, -4, 5]) should return false.');" } ], "solutions": [], @@ -1557,10 +1556,10 @@ "name": "index", "contents": [ "function checkPositive(arr) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "checkPositive([1, 2, 3, -4, 5]);" ], @@ -1573,28 +1572,28 @@ "id": "587d7dab367417b2b2512b6f", "title": "Use the some Method to Check that Any Elements in an Array Meet a Criteria", "description": [ - "some方法用于检测数组中任何元素是否满足指定条件。如果有一个元素满足条件,返回布尔值true,反之返回false。", - "举个例子,下面的代码检测数组numbers中是否有元素小于10:", - "
var numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) {
  return currentValue < 10;
});
// 返回 true
", + "The some method works with arrays to check if any element passes a particular test. It returns a Boolean value - true if any of the values meet the criteria, false if not.", + "For example, the following code would check if any element in the numbers array is less than 10:", + "
var numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) {
  return currentValue < 10;
});
// Returns true
", "
", - "在checkPositive函数值中使用some检查arr中是否有元素为正数,函数应返回一个布尔值。" + "Use the some method inside the checkPositive function to check if any element in arr is positive. The function should return a Boolean value." ], "tests": [ { - "text": "应该使用some method.", - "testString": "assert(code.match(/\\.some/g), '应该使用some method.');" + "text": "Your code should use the some method.", + "testString": "assert(code.match(/\\.some/g), 'Your code should use the some method.');" }, { - "text": "checkPositive([1, 2, 3, -4, 5])应返回true。", - "testString": "assert(checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5])应返回true。');" + "text": "checkPositive([1, 2, 3, -4, 5]) should return true.", + "testString": "assert(checkPositive([1, 2, 3, -4, 5]), 'checkPositive([1, 2, 3, -4, 5]) should return true.');" }, { - "text": "checkPositive([1, 2, 3, 4, 5])应返回true。", - "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5])应返回true。');" + "text": "checkPositive([1, 2, 3, 4, 5]) should return true.", + "testString": "assert(checkPositive([1, 2, 3, 4, 5]), 'checkPositive([1, 2, 3, 4, 5]) should return true.');" }, { - "text": "checkPositive([-1, -2, -3, -4, -5])应返回false。", - "testString": "assert(!checkPositive([-1, -2, -3, -4, -5]), 'checkPositive([-1, -2, -3, -4, -5])应返回false。');" + "text": "checkPositive([-1, -2, -3, -4, -5]) should return false.", + "testString": "assert(!checkPositive([-1, -2, -3, -4, -5]), 'checkPositive([-1, -2, -3, -4, -5]) should return false.');" } ], "solutions": [], @@ -1607,10 +1606,10 @@ "name": "index", "contents": [ "function checkPositive(arr) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "checkPositive([1, 2, 3, -4, 5]);" ], @@ -1623,34 +1622,34 @@ "id": "587d7dab367417b2b2512b70", "title": "Introduction to Currying and Partial Application", "description": [ - "arity是函数所需的形参的数量。函数Currying意思是把接受多个arity的函数变换成接受一个单一arity的函数。", - "换句话说,重构函数让它接收一个参数,然后返回另一个接收下一个参数的函数,依此类推。", - "举个例子:", - "
//Un-curried function
function unCurried(x, y) {
  return x + y;
}

// 柯里化函数
function curried(x) {
  return function(y) {
    return x + y;
  }
}
curried(1)(2) // 返回 3
", - "如果不能一次为函数提供所有参数,那么在柯里化很有用。可以将每个函数的调用保存到一个变量中,该变量将保存返回的函数引用,该引用在下一个参数可用时接受该参数。下面是一个使用curried函数的例子:", - "
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // 打印 3
", - "类似地,partial application可以描述为一次对一个函数应用几个参数,然后返回另一个应用更多参数的函数。", - "举个例子:", - "
//Impartial function
function impartial(x, y, z) {
  return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // 返回 13
", + "The arity of a function is the number of arguments it requires. Currying a function means to convert a function of N arity into N functions of arity 1.", + "In other words, it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on.", + "Here's an example:", + "
//Un-curried function
function unCurried(x, y) {
  return x + y;
}

//Curried function
function curried(x) {
  return function(y) {
    return x + y;
  }
}
curried(1)(2) // Returns 3
", + "This is useful in your program if you can't supply all the arguments to a function at one time. You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it's available. Here's an example using the curried function in the example above:", + "
// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
", + "Similarly, partial application can be described as applying a few arguments to a function at a time and returning another function that is applied to more arguments.", + "Here's an example:", + "
//Impartial function
function impartial(x, y, z) {
  return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13
", "
", - "填写add函数主体部分,通过柯里化方式来添加参数xyz." + "Fill in the body of the add function so it uses currying to add parameters x, y, and z." ], "tests": [ { - "text": "add(10)(20)(30)应返回60。", - "testString": "assert(add(10)(20)(30) === 60, 'add(10)(20)(30)应返回60。');" + "text": "add(10)(20)(30) should return 60.", + "testString": "assert(add(10)(20)(30) === 60, 'add(10)(20)(30) should return 60.');" }, { - "text": "add(1)(2)(3)应返回6。", - "testString": "assert(add(1)(2)(3) === 6, 'add(1)(2)(3)应返回6。');" + "text": "add(1)(2)(3) should return 6.", + "testString": "assert(add(1)(2)(3) === 6, 'add(1)(2)(3) should return 6.');" }, { - "text": "add(11)(22)(33)应返回66。", - "testString": "assert(add(11)(22)(33) === 66, 'add(11)(22)(33)应返回66。');" + "text": "add(11)(22)(33) should return 66.", + "testString": "assert(add(11)(22)(33) === 66, 'add(11)(22)(33) should return 66.');" }, { - "text": "应包含一个最终返回x + y + z的代码。", - "testString": "assert(code.match(/[xyz]\\s*?\\+\\s*?[xyz]\\s*?\\+\\s*?[xyz]/g), '应包含一个最终返回x + y + z的代码。');" + "text": "Your code should include a final statement that returns x + y + z.", + "testString": "assert(code.match(/[xyz]\\s*?\\+\\s*?[xyz]\\s*?\\+\\s*?[xyz]/g), 'Your code should include a final statement that returns x + y + z.');" } ], "solutions": [], @@ -1663,10 +1662,10 @@ "name": "index", "contents": [ "function add(x) {", - " // 请在本行以下添加你的代码", + " // Add your code below this line", " ", " ", - " // 请在本行以上添加你的代码", + " // Add your code above this line", "}", "add(10)(20)(30);" ], From 7d767f67f11a8080cf62b26cc3f6af0ba1dab482 Mon Sep 17 00:00:00 2001 From: Big Face Master Date: Mon, 20 Aug 2018 09:58:24 +0800 Subject: [PATCH 24/24] algorithms json link --- 08-coding-interview-prep/algorithms.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/08-coding-interview-prep/algorithms.json b/08-coding-interview-prep/algorithms.json index a55a13c..ef97f11 100644 --- a/08-coding-interview-prep/algorithms.json +++ b/08-coding-interview-prep/algorithms.json @@ -11,7 +11,7 @@ "知识提要:对称差 (Symmetric Difference),数学上,两个集合的对称差分是只属于其中一个集合,而不属于另一个集合的元素组成的集合,例如:集合let A = [ 1, 2, 3]let B = [ 2, 3, 4]的对称差分为A △ B = C = [ 1, 4]。 集合论中的这个运算相当于布尔逻辑中的异或运算。", "创建一个函数 sym,输入两个或两个以上的数组作为参数,然后返回值为对称差分的数组", "思路:设定两个数组 (例如:let A = [1, 2, 3]let B = [2, 3, 4])作为参数传入,返回对称差分数组(A △ B = C = [1, 4]),且数组中没有重复项。", - "如果你遇到了困难,请点击帮助。你可以找人“结对编程”,但不要因此放弃思考。", + "如果你遇到了困难,请点击帮助。你可以找人“结对编程”,但不要因此放弃思考。" ], "solutions": [ "function sym() {\n var arrays = [].slice.call(arguments);\n return arrays.reduce(function (symDiff, arr) {\n return symDiff.concat(arr).filter(function (val, idx, theArr) {\n return theArr.indexOf(val) === idx \n && (symDiff.indexOf(val) === -1 || arr.indexOf(val) === -1);\n });\n });\n}\nsym([1, 2, 3], [5, 2, 1, 4]);\n" @@ -101,7 +101,7 @@ "title": "Inventory Update", "description": [ "创建一个二维数组,比较并更新存储在二维数组中的“库存”元素,然后并将其与新产生的第二个二维数组进行对比,更新当前的 ”库存“ 项的数量(arr1),如果找不到这个对比对象,那么将新的对象和数量添加到“库存”数组中。注意:返回的“库存”数组应该是按照数组元素的首字母顺序排序", - "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。", + "如果你遇到了困难,请点击帮助。你可以找人“结对编程“,但不要因此放弃思考。" ], "solutions": [ "function updateInventory(arr1, arr2) {\n arr2.forEach(function(item) {\n createOrUpdate(arr1, item);\n });\n // 所有的存货都必须记帐,否则你将被解雇!\n return arr1;\n}\n\nfunction createOrUpdate(arr1, item) {\n var index = -1;\n while (++index < arr1.length) {\n if (arr1[index][1] === item[1]) {\n arr1[index][0] += item[0];\n return;\n }\n if (arr1[index][1] > item[1]) {\n break;\n }\n }\n arr1.splice(index, 0, item);\n}\n\n// 示例库存列表 \nvar curInv = [\n [21,'Bowling Ball'],\n [2,'Dirty Sock'],\n [1,'Hair Pin'],\n [5,'Microphone']\n];\n\nvar newInv = [\n [2,'Hair Pin'],\n [3,'Half-Eaten Apple'],\n [67,'Bowling Ball'],\n [7,'Toothpaste']\n];\n\nupdateInventory(curInv, newInv);\n" @@ -175,7 +175,7 @@ "description": [ "把一个字符串中的所有的字符重新排列,然后生成一个新的字符串,返回的新字符串中没有连续重复的字符。连续重复是以单个字符为判断标准。", "例如:aab应该返回 2, 因为它总共有 6 种排列方式: aabaababaababaabaa,但是其中只有 2 个没有连续重复的字符(字符 a 是本例中的重复字符):abaaba", - "如果你遇到了困难,请点击 帮助 。你可以找人“结对编程“,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助 。你可以找人“结对编程“,但不要因此放弃思考。" ], "solutions": [ "function permAlone(str) {\n return permutor(str).filter(function(perm) {\n return !perm.match(/(.)\\1/g);\n }).length;\n}\n\nfunction permutor(str) {\n // http://staff.roguecc.edu/JMiller/JavaScript/permute.html\n //permArr: 全局数组:包含排列的列表 \ n //usedChars: 全局实用数组,其中一个列表包含 \"currently-in-use\"字符 var permArr = [], usedChars = [];\n function permute(input) {\n // 将输入参数转换成一个字符串数组 (每个字符都有一个元素)\n var i, ch, chars = input.split(\"\");\n for (i = 0; i < chars.length; i++) {\n // 从字符数组中获取并删除索引为\"i\"的字符 \n ch = chars.splice(i, 1);\n // 将删除的字符增加的使用字符的末尾 \n usedChars.push(ch);\n // 当char数组中没有其他字符添加时,添加使用的chars列表来排列排列 \n if (chars.length === 0) permArr[permArr.length] = usedChars.join(\"\");\n // 从char数组发送字符(减去上面删除的字符)进行置换 \n permute(chars.join(\"\"));\n // 将删除的数组添加回char数组中的原位置 \n chars.splice(i, 0, ch);\n //删除使用过的字符数组末尾使用的最后一个字符 \n usedChars.pop();\n }\n }\n permute(str);\n return permArr;\n}\n\npermAlone('aab');\n" @@ -311,7 +311,7 @@ "这种方式通过多次迭代数组来完成操作,不管是平均还是最坏的情况,都是具有二次时间复杂度。尽管这个方式简单,但是在实际应用中,大多数情况下不切实际的:时间复杂度过高。", "说明:创建一个函数并命名为bubbleSort,输入参数是一个数组,且数组元素全部都是整数类型,然后按照从最小到最大的顺序返回整个数组。", "注意:
我们将在后台调用这些函数,测试数组在编辑器中也是被注释掉了的。尝试记录输入array来确认你的冒泡排序算法是否正确。", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。" ], "tests": [ { @@ -366,8 +366,8 @@ "description": [ "现在我们开始实现选择排序。选择排序是通过选择列表中最小值来与列表中的第一个值进行对比交换,然后从第二位置开始逐一对比,选择剩下的列表中最小值与第二个元素交换位置。然后循环遍历列表并交换元素,直到列表最后一个元素,此时的列表就完成了排序。选择排序在所有的情况下都具有二次时间复杂度。", "说明:创建一个函数并命名为selectionSort,输入参数是一个数组,且数组元素全部都是整数类型,然后按照从最小到最大的顺序返回整个数组。", - "注意:
我们将在后台调用这些函数,测试数组在编辑器中也是被注释掉了的。尝试记录输入array来确认你的冒泡排序算法是否正确。" - "如果你遇到了困难,请点击 帮助。你可以找人 “结对编程”,但不要因此放弃思考。", + "注意:
我们将在后台调用这些函数,测试数组在编辑器中也是被注释掉了的。尝试记录输入array来确认你的冒泡排序算法是否正确。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。" ], "tests": [ { @@ -423,7 +423,7 @@ "现在我们开始研究插入排序。这个方法通过在列表的开头见一个排序的数组来实现整个排序,它以第一个元素开始排序数组,然后检查对比下一个元素,并将其向后交换到排序的书中,直到它处在排序的位置。循环迭代整个列表,将新产生的元素交换到排序部分,直到整个列表处于排序状态。该算法在平均和最坏的情况下具有二次时间复杂度。", "说明:创建一个函数并命名为insertionSort,输入参数是一个数组,且数组元素全部都是整数类型,然后按照从最小到最大的顺序返回整个数组。", "注意:
我们将在后台调用这些函数,测试数组在编辑器中也是被注释掉了的。尝试记录输入array来确认你的冒泡排序算法是否正确。", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。" ], "tests": [ { @@ -480,7 +480,7 @@ "快速排序是一种非常有效的排序方式,平均时间复杂度O(nlog(n)),同时它也是相对比较容易实现的方式。这些特性使得快速排序成为了一种流行而有用的排序方式。", "说明:创建一个函数并命名为quickSort,输入参数是一个数组,且数组元素全部都是整数类型,然后按照从最小到最大的顺序返回整个数组。虽然选择一个枢轴值很重要,但任何一个枢轴都能满足要求,为了以防万一,我嘛一般选择第一个或者最后一个元素来作为数轴值。", "注意:
我们将在后台调用这些函数,测试数组在编辑器中也是被注释掉了的。尝试记录输入array来确认你的冒泡排序算法是否正确。", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。" ], "tests": [ { @@ -541,7 +541,7 @@ "另外,这将是我们在此章节讨论的最后一种排序算法,但是在后续有关树型数据结构的章节中,我们将一起研究堆排序,同样它也是一种高效的排序方式,其实现过程需要用到二进制堆的概念。", "Instructions:创建一个函数并命名为mergeSort,输入参数是一个数组,且数组元素全部都是整数类型,然后按照从最小到最大的顺序返回整个数组>。实现这个排序方式可以采用两个函数来实现:merge负责合并两个排序的数组,merge sort负责生成单个数组的用于归并使用。祝你好用!", "注意:
我们将在后台调用这些函数,测试数组在编辑器中也是被注释掉了的。尝试记录输入array来确认你的冒泡排序算法是否正确。", - "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。", + "如果你遇到了困难,请点击 帮助 。你可以找人 “结对编程”,但不要因此放弃思考。" ], "tests": [ {