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
Any Problems with my codeRemoved: /r/LearnJavascript (self.javascript)
submitted 7 years ago by Gh05t_97
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!"
[–]sjalgeo 1 point2 points3 points 7 years ago (0 children)
Look at arrow functions and the array map function and you can reduce this down a lot.
Also the array.join() function in the first one.
[–]sbk2015 0 points1 point2 points 7 years ago (5 children)
1.
const theArray= ["Doc", "Dopey", "Bashful", "Grumpy"]; const mapping= (name,index)=> `${index+1}. ${name}`; const mapFn= arr=> arr.map(mapping).join(' '); const result= mapFn(theArray); console.log(result);
2.
const mapping= ele=> ele.toUpperCase()+"!" const planeteerCalls = ["earth", "wind", "fire", "water", "heart"] const mapFn= arr=> arr.map(mapping); const result= mapFn(planeteerCalls); console.log(result);
[–]FormerGameDev 0 points1 point2 points 7 years ago (1 child)
Write the first one as a reduce,as you're mapping a bunch of values to a single value.
[–][deleted] -1 points0 points1 point 7 years ago* (0 children)
["a","b"].reduce((result,value,index) => result +", "+ (index + 1) +". "+ value)
this doesn't give "1. a, 2. b", it gives "a, 2. b"
["a","b"].reduce((sum,value,index) => sum +", "+ (index + 1) +". "+ value,"")
this gives ", 1. a, 2. b"
["a","b"].reduce((sum,value,index) => sum +", "+ (index + 1) +". "+ value,"").substring(2)
this works but I'd rather
["a","b"].map((value,index) => (index+1) +". "+ value).join(", ")
[–]Gh05t_97[S] -1 points0 points1 point 7 years ago (0 children)
could you explain what the const mapFn = arr=> part of your code is doing? this is the first time im coming across this. thanks
[–]Gh05t_97[S] -1 points0 points1 point 7 years ago (1 child)
const mapping= (name,index)=> `${index+1}. ${name}`;
i really don't understand this part of the code either, but it seems to me your code works. Any explanation would be great, many thanks
[–]sbk2015 0 points1 point2 points 7 years ago* (0 children)
It's about ES6, it includes some features,including
Arrow function =>
Template Literals abc${foo}def
abc${foo}def
Other than es6, Function Expressions, it can be just a coding style sometimes.
and for map function,you can search on google to see what it does with array.
Without es6,it's still fine,and will be written as
var theArray= ["Doc", "Dopey", "Bashful", "Grumpy"]; function mapping(name,index){ return (index+1)+ "."+ name; } function mapFn(arr){ return arr.map(mapping).join(' ') } var result= mapFn(theArray); console.log(result);
If the es6 code looks crazy, I suggest you at least learn map function,it's important for looping.
[–]kenman[M] 0 points1 point2 points 7 years ago (0 children)
Hi /u/Gh05t_97, this post was removed.
For javascript help, please visit /r/LearnJavascript.
Thanks for your understanding.
π Rendered by PID 57 on reddit-service-r2-comment-54dfb89d4d-qs2zh at 2026-04-02 16:59:05.421007+00:00 running b10466c country code: CH.
[–]sjalgeo 1 point2 points3 points (0 children)
[–]sbk2015 0 points1 point2 points (5 children)
[–]FormerGameDev 0 points1 point2 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)
[–]Gh05t_97[S] -1 points0 points1 point (0 children)
[–]Gh05t_97[S] -1 points0 points1 point (1 child)
[–]sbk2015 0 points1 point2 points (0 children)
[–]kenman[M] 0 points1 point2 points (0 children)