Message from JavaScript discussions

July 2017

— Is there a better way to write this code?


What is there is another word apart from valentine in that string in var text like frank how do i write the code to sort only for a particular word in the string for example valentine alone

— Are the names in the string necessarily separated by commas ?

— Yes they are

— You can split the string to form an array and apply a filter to the array ^-^

— But if you are familiar with regex it might be the best method

— Can you write the codes so that i can better understand what you are saying?

— Text.split()
text.filter("valentine") ???


text.split(',').filter( (e) => e === 'valentine').length

— It returns an array of 'valentine', you can call length to find the number of hits, but it doesn't seem like the cleanest solution so maybe someone else has a better one :)

Message permanent page

— Also you need to remove the final full stop x)

— Alright thanks alot