Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. words_count = (sentences, word) => {
  2. let wordCount = 0
  3. // Hapus semua karakter kecuali alphabet, numeric, space dan ubah menjadi bentuk Array.
  4. sentencesArr = sentences.replace(/[^\w\s]/gi, '').split(' ')
  5.  
  6. for (let i = 0; i < sentencesArr.length; i++) {
  7. if(sentencesArr[i].toLowerCase() == word.toLowerCase()) wordCount++
  8. }
  9.  
  10. return wordCount
  11. }
  12.  
  13. let setences = "`A wonderful serenity has taken possession of my entire soul, like these sweet
  14. mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in
  15. this spot, which was created for the bliss of souls like mine. I am so happy `
  16. console.log(words_count(setences, "like")) // 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement