We are currently working with an array that contains a list of various website names. Some examples include www.google.com, www.msn.com, www.amazon.co.in, in.answers.yahoo.com, en.m.wikipedia.com, codehs.gitbooks.io,www.coderanch.com, and more. Our goal is to search for all the sites that start with "www" and then display the total count of such websites. For the given examples, the total count would be 4.
I have attempted to tackle this issue using methods like string.search()
and string.match()
, as well as experimenting with Regular Expressions
. Unfortunately, none of these approaches have been successful so far. Can anyone offer some assistance?
I attempted the following:
function myFun(){
var arr = ["www.google.com", "www.msn.com", "www.amazon.co.in", "in.answers.yahoo.com",
"en.m.wikipedia.com", "codehs.gitbooks.io", "www.coderanch.com"];
var count = 0;
var str= arr.toString();
console.log(str);
if(str == /www/g){
count ++;
}
document.write(count);
}