私は再帰関数を使ってJavascriptのreduceを再定義しようとしています。 これは私の試みですが、うまくいきません。もし誰かが少しだけそれを動作させるためにそれを変更することができます、それは私がそれをよりよく理解するので素晴らしいだろう。 (これはfunctional-javascript-workshopの練習です)。なぜreduce working(JS)の再帰的な定義はありませんか?
/usr/local/lib/node_modules/functional-javascript-workshop/exercises/basic_recursion/exercise.js:13
prev[curr] = ++prev[curr] || 1
^
TypeError: Cannot create property 'undefined' on string 'exercitation'
at /usr/local/lib/node_modules/functional-javascript-workshop/exercises/basic_recursion/exercise.js:13:29
at reduce (/home/david/node-school/functional-workshop/solution.js:7:28)
at /usr/local/lib/node_modules/functional-javascript-workshop/exercises/basic_recursion/exercise.js:12:10
at obtainResult (/usr/local/lib/node_modules/functional-javascript-workshop/exercises/runner.js:100:21)
at Exercise.<anonymous> (/usr/local/lib/node_modules/functional-javascript-workshop/exercises/runner.js:66:27)
at next (/usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:188:19)
at /usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:195:7
at Exercise.<anonymous> (/usr/local/lib/node_modules/functional-javascript-workshop/exercises/runner.js:34:5)
at next (/usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:188:19)
at /usr/local/lib/node_modules/functional-javascript-workshop/node_modules/workshopper-exercise/exercise.js:195:7
「if(arr.length)」は機能しますか?私はいつも 'if(arr.length> 0)'を使用します... – Danmoreng
正しいコードを表示していません。エラーは行7の 'solution.js'で呼び出された後、' exercise.js'で発生しています。 –
@Danmoreng、真理値をテストします –