have to filter out messages with a length shorter then 50

67 views
Skip to first unread message

Roelof Wobben

unread,
Jan 20, 2015, 11:06:34 AM1/20/15
to nod...@googlegroups.com
Hello,

I try to learn functional javascript by following the functonial workshop of nodeschool.

Now I have to filter out all the messages which lenght are shorter then 50.

So I thought this would work :

~~~

module.exports = function getShortMessages(messages) {

    function small_message(item)

    { return item.length < 50 ; }


    messages.map(function(message)

     { return small_message(message) }) }

but when I test it I see a empty array as answer  

Can someone explain where my thinking took the wrong path ?

Roelof


Scott Ganyo

unread,
Jan 20, 2015, 11:30:44 AM1/20/15
to nod...@googlegroups.com
Hey Roelof,

I don’t see you returning the results of your map anywhere. I would expect to see this:

  return messages.map( …

Best,
Scott


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2bb9460e-96ef-4160-98e5-afe713f6ea96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Adrien Risser

unread,
Jan 20, 2015, 11:30:56 AM1/20/15
to nod...@googlegroups.com

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2bb9460e-96ef-4160-98e5-afe713f6ea96%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Adrien Risser,
Freelance Node.js Consultant

Roelof Wobben

unread,
Jan 20, 2015, 11:33:29 AM1/20/15
to nod...@googlegroups.com
I thought that this part took part of that : { return small_message(message) }) }


there I return the output of the filter.

Roelof



Op dinsdag 20 januari 2015 17:30:44 UTC+1 schreef Scott Ganyo:

Roelof Wobben

unread,
Jan 20, 2015, 11:42:57 AM1/20/15
to nod...@googlegroups.com
I try to use it .

When I do this :

~~~

module.exports = function getShortMessages(messages) {
   
    function smallerfifty(element) {
        return element.length > 50;
        }
   
    return messages.filter(smallerfifty);
              
    }

~~~

I see a empty array.



Op dinsdag 20 januari 2015 17:30:56 UTC+1 schreef Adrien Risser:

Scott Ganyo

unread,
Jan 20, 2015, 11:48:21 AM1/20/15
to nod...@googlegroups.com
That too. :)  And... you could simplify your function if you wish:

module.exports = function getShortMessages(messages) {

    function small_message(item) { return item.length < 50; }

    return messages.filter(small_message);
}

Scott

Alexander Behrens

unread,
Jan 20, 2015, 11:48:21 AM1/20/15
to nod...@googlegroups.com
Also, you can pass the function as an argument directly:

return messages.filter(small_message);

Roelof Wobben

unread,
Jan 20, 2015, 11:52:13 AM1/20/15
to nod...@googlegroups.com
oke,

But still my answer is failing . See this output :

~~~

ACTUAL                             EXPECTED                                        
------                             --------                                        
"[]"                            != "[ 'Et anim veniam dolor ipsum consectetur.',"  
""                              != "  'Magna velit non do ea aliquip nulla minim.',
"                                                                                  
null                            != "  'Minim magna proident consectetur dolore.'," 
null                            != "  'Minim sint minim reprehenderit laboris minim
.',"                                                                               
null                            != "  'Nisi duis exercitation nisi tempor ex magna.
',"                                                                                
null                            != "  'Dolor nulla deserunt cupidatat pariatur.'," 
null                            != "  'Aute laboris eiusmod nulla do cupidatat.'," 
null                            != "  'Labore non sit mollit irure labore.',"      
null                            != "  'Anim non ullamco culpa ea sint do sunt.',"  
null                            != "  'Non amet duis dolore officia anim.',"       
null                            != "  'Irure ex commodo amet id tempor do cillum.',
"                                                                                  
null                            != "  'Laborum fugiat sunt eu veniam aute.' ]"     
null                            != ""                                              
# FAIL                                              


~~~

So something is not right here.

Roelof



Op dinsdag 20 januari 2015 17:48:21 UTC+1 schreef Alexander Behrens:

Roelof Wobben

unread,
Jan 20, 2015, 11:53:56 AM1/20/15
to nod...@googlegroups.com
the input variable looks like this :

[ { message: 'Cillum quis laborum incididunt aliqua est et consectetur ipsum elit d
eserunt.' },                                                                       
  { message: 'Dolor incididunt velit sit deserunt laboris sunt nostrud cillum Lorem
 sit occaecat dolore aute ex.' },                                                  
  { message: 'Id fugiat veniam do eu.' },                                          
  { message: 'Consequat id non nulla culpa dolore eu irure deserunt.' },           
  { message: 'Mollit qui elit tempor eiusmod eu.' },                               
  { message: 'Aliquip quis aute cupidatat adipisicing irure nisi consectetur.' },  
  { message: 'Adipisicing ea cupidatat quis sunt eu id velit voluptate officia cill
um elit aute aliqua.' },                                                           
  { message: 'Qui incididunt aute eiusmod Lorem cillum.' },                        
  { message: 'Sunt non ut eiusmod id magna sunt culpa labore aliquip.' },          
  { message: 'Mollit exercitation sunt esse reprehenderit consequat laboris irure i
n dolore non.' },                                                                  
  { message: 'Incididunt Lorem minim non sunt est mollit non do nulla elit qui ipsu
m.' },                                                                             
  { message: 'Ea exercitation occaecat nulla irure eiusmod pariatur enim ipsum duis
 pariatur pariatur.' },                                                            
  { message: 'Consequat ad labore occaecat magna.' },                              
  { message: 'Sunt commodo veniam consequat ea exercitation incididunt esse nostrud
 elit labore ipsum aliquip.' },                                                    
  { message: 'Do deserunt consectetur id pariatur culpa.' },                       
  { message: 'Magna consectetur irure consequat duis consequat dolore duis exercita
tion eiusmod id aute exercitation.' } ]   



Maybe on some way use the map function so every item in the array is used. ?


Op dinsdag 20 januari 2015 17:48:21 UTC+1 schreef Alexander Behrens:
Also, you can pass the function as an argument directly:

return messages.filter(small_message);

Alexander Behrens

unread,
Jan 20, 2015, 12:34:44 PM1/20/15
to nod...@googlegroups.com
@Roloef: Ist your sample array full of short texts? Because your example filters out any short text. Therefore your returning array is empty. You have to change the condition from element.length > 50 to element.length <= 50

Roelof Wobben

unread,
Jan 20, 2015, 12:43:36 PM1/20/15
to nod...@googlegroups.com
you are right. I have misread the challenge

Still one problem :

The output now looks like this :

~~~

[ { message: 'Cupidatat reprehenderit qui dolor ex labore amet.'  } ,


~~~

where the output must look like this :

~~~

[  message: 'Cupidatat reprehenderit qui dolor ex labore amet.' ,

~~~

So I think I need to use map to fetch the messages one by one or is there another way .

Roelof




Op dinsdag 20 januari 2015 18:34:44 UTC+1 schreef Alexander Behrens:

Alexander Behrens

unread,
Jan 20, 2015, 1:23:10 PM1/20/15
to nod...@googlegroups.com
Change return item.length < 50 to return item.message.length < 50

Roelof Wobben

unread,
Jan 20, 2015, 1:37:41 PM1/20/15
to nod...@googlegroups.com
I already did .

I have now this

~~~

module.exports = function getShortMessages(messages) {
   
    function smallerfifty(element) {
        return element.message.length < 50;
     }
   
    return messages.filter(smallerfifty)
 
              
}

~~~

and this output :


~~~

[ { message: 'Eiusmod adipisicing esse labore magna elit irure.' },                                      
  { message: 'Amet esse excepteur mollit excepteur.' },  
]

~~~

where it schould be

~~~

[  message: 'Eiusmod adipisicing esse labore magna elit irure.' ,                                      
   message: 'Amet esse excepteur mollit excepteur.' ,  


~~~



Op dinsdag 20 januari 2015 19:23:10 UTC+1 schreef Alexander Behrens:

Roelof Wobben

unread,
Jan 20, 2015, 2:18:10 PM1/20/15
to nod...@googlegroups.com
Solved this one with a little try and error.

Roelof


Op dinsdag 20 januari 2015 19:37:41 UTC+1 schreef Roelof Wobben:
Reply all
Reply to author
Forward
0 new messages