06Week/functional-javascript - filter()

#1

Hi All,

I’m working on functional-javascript is good and stuck on filter().

Right now I’m getting an error when I run the test:


// Filter: getShortMessages takes an array of objects with ‘.message’ properties and returns an array of messages that are less than < 50 characters long.

function getShortMessages(messages) {

// SOLUTION GOES HERE

// var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

let newMessage = messages.filter(message => messages.length < 50);

return newMessage;

}

module.exports = getShortMessages;

and here is the return I get:


submission: [ { message: ‘Ex elit enim tempor ex.’ }, { message: ‘Minim reprehenderit nulla cupidatat sint esse mollit dolore.’ }, { message: ‘Laborum nulla commodo cupidatat ex excepteur laborum commodo reprehenderit non esse Lorem ea esse consectetur.’ }, { message: ‘Adipisicing do esse in labore anim.’ },{ message: ‘Ex ullamco fugiat occaecat duis nisi.’ }, { message: ‘Id non voluptate nostrud qui sunt tempor eu mollit dolore sunt.’ }, { message: ‘Dolore dolore reprehenderit exercitation ex culpa eiusmod veniam ea ad pariatur.’ }, { message: ‘Deserunt velit ipsum aliqua veniam fugiat quis culpa eu ea excepteur duis laboris ad nostrud.’ }, { message: ‘Veniam ullamco enim do ad.’ }, { message: ‘Quis id sint sunt non nulla exercitation do nostrud ullamco deseruntLorem elit cupidatat sit.’ }, { message: ‘Ipsum esse elit eu ex.’ }, { message: ‘Commodo cupidatat sit labore occaecat consequat.’ }, { message: ‘Id ea deserunt eu dolor aute aliqua ut consectetur proident minim ullamco fugiat.’ }, { message: ‘Reprehenderit qui proident quis nulla sint sint quis.’ }, { message: ‘Qui aliquip id consectetur duis occaecat consectetur elit ipsum nostrud officia.’ }, { message: ‘Ullamco sint est enim sit occaecat qui id.’ }, { message: ‘Consectetur eiusmod eiusmod tempor ea Lorem aute irure qui nisi cupidatat adipisicing in mollit.’ }, { message: ‘Occaecat reprehenderit esse voluptate ea veniam.’ }, { message: ‘Amet mollit exercitation nulla consequat voluptate ea minim sit.’ }, { message: ‘Ea minim in qui culpa pariatur nisi labore laborum incididunt et.’ }, { message: ‘Velit ipsum id anim incididunt cillum in duis veniam proident reprehenderit deserunt velit.’ }, { message: ‘Aliquip mollit nulla commodo nulla adipisicing velit consequat non esse excepteur sunt eiusmod commodo ad.’ }, { message: ‘Eiusmod labore commodo irure laborum quis reprehenderit nostrud amet fugiat sunt et ex do.’ }, { message: ‘Lorem ea consequat aliquip minim labore.’ }, { message: ‘Dolor dolor anim consectetur eu reprehenderit aliqua laboris irure officia non cillum elit.’ } ]
solution: [ ‘Ex elit enim tempor ex.’, ‘Adipisicing do esse in labore anim.’, ‘Ex ullamco fugiat occaecat duis nisi.’, ‘Veniam ullamco enim do ad.’, ‘Ipsum esse elit eu ex.’, ‘Commodo cupidatat sit labore occaecat consequat.’, ‘Ullamco sint est enim sit occaecat qui id.’, ‘Occaecat reprehenderit esse voluptate ea veniam.’, ‘Lorem ea consequat aliquip minim labore.’ ]

I tried to use map() on the return as well, but It returns an error and breaks.

Any help is greatly appreciated!

#2

Shortly after my original post, I got some help from Cynthia and was able to get the following solution:


function getShortMessages(messages) {
// SOLUTION GOES HERE
// var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])
let newMessage = messages
.filter(item => item.message.length < 50)
.map(item => item.message);
return newMessage;
}

module.exports = getShortMessages;

the main issue I had to overcome in this exercise is to understand targeting ‘message’ within the object

1 Like