Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exersise #7 Basic: Recursion code bug #190

Closed
Gordiievskyi opened this issue Feb 8, 2018 · 0 comments
Closed

exersise #7 Basic: Recursion code bug #190

Gordiievskyi opened this issue Feb 8, 2018 · 0 comments

Comments

@Gordiievskyi
Copy link

If example you suggest is used for producing the expected result then it if I am not mistaken it has a bug.

  1. function reduce(arr, fn, initial) {
  2. return (function reduceOne(index, value) {
  3. if (index > arr.length - 1) return value // end condition
    
    return reduceOne(index + 1, fn(value, arr[index], index, arr)) // calculate & pass values to next step
    })(0, initial) // IIFE. kick off recursion with initial values
    }

Row number 3 has to be updated to : if (index > = arr.length - 1) return value // end condition
module.exports = reduce

Below is output of expected result input and solution:

input: [ 'enim', 'enim', 'cupidatat', 'fugiat', 'sunt', 'ullamco', 'occaecat', 'amet', 'laborum', 'ipsum', 'dolor', 'elit', 'incididunt', 'ea', 'laborum', 'nostrud', 'cupidatat', 'ipsum', 'veniam', 'ipsum', 'quis', 'anim', 'occaecat', 'tempor', 'cupidatat', 'deserunt', 'qui', 'aute', 'laboris', 'magna', 'in' ]
submission: { enim: 2, cupidatat: 3, fugiat: 1, sunt: 1, ullamco: 1, occaecat: 2, amet: 1, laborum: 2, ipsum: 3, dolor: 1, elit: 1, incididunt: 1, ea: 1, nostrud: 1, veniam: 1, quis: 1, anim: 1, tempor: 1, deserunt: 1, qui: 1, aute: 1, laboris: 1, magna: 1 }
solution: { enim: 2, cupidatat: 3, fugiat: 1, sunt: 1, ullamco: 1, occaecat: 2, amet: 1, laborum: 2, ipsum: 3, dolor: 1, elit: 1, incididunt: 1, ea: 1, nostrud: 1, veniam: 1, quis: 1, anim: 1, tempor: 1, deserunt: 1, qui: 1, aute: 1, laboris: 1, magna: 1, in: 1 }

As you can see there is a word : 'in' that is missing in the solution output and present in my version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant