Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a JavaScript function countSubstringOccur(arr) that accepts as parameter an array of 2 elements
- arr [keyword, text]. The function finds how many times a substring is contained in a given text
- (perform case insensitive search). Write JS program substringSearch.js that invokes your function
- with the sample input data below and prints the output at the console. */
- "use strict";
- function countSubstringOccur(arr) {
- var text = arr[1].toLowerCase();
- var pattern = new RegExp(arr[0], 'g');
- var counterOccurs = (text.match(pattern) || []).length;
- return counterOccurs;
- }
- console.log(countSubstringOccur(['in', "We are living in a yellow submarine. We don't have anything else." +
- "Inside the submarine is very tight. So we are drinking all the day. We will move out of it in 5 days."]));
- console.log(countSubstringOccur(['your', 'No one heard a single word you said.' +
- 'They should have seen it in your eyes. What was going around your head.']));
- console.log(countSubstringOccur(['but', "But you were living in another world tryin'" +
- "to get your message through."]));
- console.log(countSubstringOccur(['hi', "Hidden networks say 'Hi' only to Hitachi devices. Hi, said Matuhi. HI!"]));
- console.log(countSubstringOccur(['SoftUni', "The Software University (SoftUni) trains software engineers, gives them" +
- "a profession and a job. Visit us at http://softuni.bg. Enjoy the SoftUnification at SoftUni.BG." +
- "Contact us.Email: INFO@SOFTUNI.BG. Facebook: https://www.facebook.com/SoftwareUniversity." +
- "YouTube: http://www.youtube.com/SoftwareUniversity. Google+: https://plus.google.com/+SoftuniBg/." +
- "Twitter: https://twitter.com/softunibg. GitHub: https://github.com/softuni"]));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement