We have an array that holds many web site names. Eg, www.google.com , www.msn.com , www.amazon.co.in , in.answers.yahoo.com, en.m.wikipedia.com, codehs.gitbooks.io,www.coderanch.com etc. Search for all sites that begin with “www” and display the total number of such sites. Eg. for the above, the total is 4.
I have tried using string.search()
and string.match()
methods and also used Regular Expressions
but none of them worked. Can anyone help me out? I tried:
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);
}
function myFun(){ let 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" ]; let count = arr.reduce((c,i)=> { i.split(".")[0]==='www' && c++ ; return c; },0); document.write(count); } myFun();
```function searchWebsite(){
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 pattern = new RegExp("www", "i");
for(i=0;i<arr.length;i++){
var result = pattern.test(arr[i]);
if(result){
count ++;
}
}
document.write(count);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.