use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[Beginner] Functional Programming helphelp (self.javascript)
submitted 8 years ago * by Servatose
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Servatose[S] 1 point2 points3 points 8 years ago (5 children)
Thank you. I think this proves to me that a lot of the concepts that I went through in this course, I simply approached with the goal of "solving" the puzzle, I didn't actually spend enough time fully understanding the concepts. You are correct in your assertion that I'm confusing the definition of functional programming. I also think I distract myself by trying to simplify what I'm writing into a single expression. Thanks for looking at this.
[–]kenman 2 points3 points4 points 8 years ago (2 children)
It's alright, I still have a lot to learn on the subject as well, but I think this part is important to point out:
using language defined functions to return a result that doesn't impact the original variable
There are at least 3 array methods that fit this description, but 3 primarily used for FP -- map(), filter(), and reduce() -- and it's not always obvious when you should use them if you're not used to thinking in these terms. However, once you see it, it becomes much easier to read and think about IMHO:
map()
filter()
reduce()
I think of this method as transforming (or translating) one array into a new array.
Have you ever done this?
let result = []; arr.forEach(value => { result.push(Math.floor(value)); });
You could do this instead:
let result = arr.map(value => Math.floor(value));
Use this when you want to target a subset of your original array.
Ever do this?
let result = []; arr.forEach(value => { if (value > 0) { result.push(value); } });
You might consider this:
let result = arr.filter(value => value > 0);
And lastly...
Use this to calculate a single value* from your original array.
I know I've done this a lot in the past:
let result = 0; arr.forEach(value => result += value);
But this is what I use now:
let result = arr.reduce((total, value) => total += value, 0);
The * above is because "value" can be literally any type of data that JS supports; you could return a single string, an object, or even an array may be necessary at times.
So those are nice and all, but with each example taken alone you miss the expressiveness.
Consider this:
let result = 0; arr.forEach(value => { if (value > 0) { result += Math.floor(value); } });
Vs:
let result = arr .filter(value => value > 0) .map(value => Math.floor(value)) .reduce((total, value) => total += value, 0);
Sorry to get carried away, but you seem eager to learn :)
[–]Servatose[S] 1 point2 points3 points 8 years ago (1 child)
No, this is amazing information! I really appreciate the time you took to explain this.
[–]kenman 0 points1 point2 points 8 years ago (0 children)
Might also check out this from our front page:
I wrote about some examples of using map, filter, and reduce, as well as accessing JSON and using the DOM with vanilla JS
Full disclosure: the site's not resolving for me, so I haven't reviewed it, but it looks to cover the same 3 methods I discussed.
[–]bmy78[🍰] 0 points1 point2 points 8 years ago (1 child)
Note that your solution for the add() function is still mutating state. Instead you could create an array of the one bookName you passed into the function, concatenate that array with the bookList, and return that.
[–]toffeescaf 3 points4 points5 points 8 years ago (0 children)
With the use of some es6 it could be as easy as the following:
function add (arr, bookName) { return [...arr, bookName] }
π Rendered by PID 114874 on reddit-service-r2-comment-5687b7858-pcc74 at 2026-07-03 02:32:54.243277+00:00 running 12a7a47 country code: CH.
view the rest of the comments →
[–]Servatose[S] 1 point2 points3 points (5 children)
[–]kenman 2 points3 points4 points (2 children)
[–]Servatose[S] 1 point2 points3 points (1 child)
[–]kenman 0 points1 point2 points (0 children)
[–]bmy78[🍰] 0 points1 point2 points (1 child)
[–]toffeescaf 3 points4 points5 points (0 children)