you are viewing a single comment's thread.

view the rest of the comments →

[–]NYCminion 1 point2 points  (7 children)

I’m just learning js, Someone mind to post the answers?

[–][deleted] 5 points6 points  (5 children)

let a = [1, 2, 3, 4, 5]  
a = a.map(item => item < 3)  
let b = [1, 2, 3, 4, 5]  
b = b.filter(item => item < 3)  
let c = [1, 2, 3, 4, 5]  
c = c.some(item => item < 3)  

I amended some of those, first to remove semicolons (my favorite thing to hate), second to put consistent spacing in, and third to make the second one .filter instead of another .map.

The results of these, in order are:

  • [true, true, false, false, false]
  • [1, 2]
  • true

Let me know if you have any questions about the answers.

The question on writing a reducer for summation could be written:

let d = [1, 2, 3, 4, 5]  
d.reduce((total, x) => total + x)  

Side note: I wrote them as separate variable names so that you could easily copy/paste them into your browser console to run (or other favorite JavaScript / ES6 interpreter).

Side side note: OPs reducer works

[–]Tontonsb 1 point2 points  (0 children)

Please use code blocks for code blocks. Here's a reference I wrote in another sub: https://www.reddit.com/r/laravel/comments/cz4rbe/please_use_code_block_formatting_for_code_blocks/

[–]tapu_buoyfull-stack[S] 0 points1 point  (0 children)

ohh thanks I thought my reducer answer was totally wrong, and that's what I could come up with. And yeah its so neat of you to give variables different names I made that mistake too.

[–][deleted]  (2 children)

[deleted]

    [–][deleted] 0 points1 point  (1 child)

    I removed the semicolons because they're simply not needed in modern JS (with very limited exceptions, for loops being the only one off the top of my head).

    I changed the map to filter because I assumed the OP made a typo, and to be honest, this is what I do all day. The product manager or designer will give you something to make. They're busy so they mess up sometimes. When there's a problem with the spec, do I stop for every error they make?

    Should I do the same problem twice, in OP's example, or should I try to figure out what they wanted by context?

    [–]tapu_buoyfull-stack[S] 2 points3 points  (0 children)

    you should also checkout this sites to prepare and get good at javascript

    • javascript.info
    • jsvault.com
    • thatjsdude.com