What steps can I take to tackle this issue using JavaScript?

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);
            }

Answer №1

function checkWebsites(){
    let websites = [
      "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 = websites.reduce((c,i)=> {
      i.split(".")[0]==='www' && c++ ;
      return c;
    },0);
    document.write(count);
}

checkWebsites();

Answer №2

function searchWebsite(){
    var websites = ["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 countWebsites = 0;
    var pattern = new RegExp("www", "i");
    for(i=0;i<websites.length;i++){
        var result = pattern.test(websites[i]);
        if(result){
            countWebsites++;
         }
    }
    document.write(countWebsites);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to implement jQuery on dynamically added content?

I created a page that closely resembles a Facebook wall, with posts, comments for each post, and a "send" button to add new comments. In this example: The posts are identified by the class "mypost". The "send-comment" button is located within a div with ...

updating the HTML DOM elements using JavaScript is not yielding any response

One way that I am trying to change the background of a div is by using a function. Below is an example of the html code I am working with: $scope.Background = 'img/seg5en.png'; document.getElementById("Bstyle").style.background = "url("+$scope.B ...

How can I add an active CSS class to an li element depending on the href attribute of its child <a> tag?

Looking for a way to dynamically add an 'active' class to a navigation menu item based on the current URL? Here's what I have so far: <ul class="nav sf-menu"> <li><a href="@Url.Action("Index","Home")">Home<span>& ...

Is there a method to eliminate quotation marks directly from an array in Python?

My array looks like this: value = ["['A','B','C']" , "['B','A','D']" , "['E','A','C']"] Is there a way to eliminate the double quotes from each index in the array? ...

Populate a node with every element from the array

Here I am once again! So, I have an array that is populating a JSON structure, and I now need to translate that array structure into an XML format. How can I achieve this? The array is correctly formatted with JSON syntax and I need to insert the data in ...

Clear out all current cookies

I utilized the following JavaScript code to generate a pop-up window on the website that would only appear once. However, my client now wants to launch a new promotion and I am attempting to remove existing cookies so that the pop-up window displays again ...

Show data stored in a MongoDB collection as an HTML table by utilizing Angular CLI as the frontend interface

Hey there, I am currently diving into the world of nodejs, mongodb, and angular cli. As part of my college project, I'm facing some challenges. Specifically, I need to showcase the array data from mongodb on an existing table within the angular compon ...

A PHP download button that does not require submitting back to the page

Having an issue with my code provided below: <?php if(empty($_POST['download_button']) === false && isset($_POST['download_button'])){ header('Content-Description: File Transfer'); header('Content-Type ...

What is the most effective way to extract an array of #tags from a given string using parsing methods?

I have a string that looks something like "#tag1 #tag2 #tag3 not_tag1 not_tag2 #tag4" (note the varying number of spaces between tag2 and tag4). I'm trying to extract just the tag1, tag2, and so on from this string. These tags are simil ...

Display the kth lowest value within a given array

After analyzing the following code, it is evident that an incorrect output is being generated: Take a look at the code below: int[] x = {7, 10, 4, 20, 15}; int n = x.length; int k = 4; int ctr = 0; for (int i = ...

Tips for utilizing generateFunc within the server.cache() method in Hapi.js?

Can anyone provide guidance on how to utilize generateFunc in the code snippet below? server.cache({ expiresIn: 1000*60*60, segment: 'test', generateFunc: function(key,next){} }); Although I understand that g ...

Can a form be submitted to two different pages based on the button name?

I have a form that directs to another page and performs various database operations. <form id="form1" method="post" action="goes_to_page1.php" onsubmit="return checkformvalidation();"> <input type="text" id="field1" name="field1" onblur="checkva ...

Conceal element when unchecked

There is a checkbox pre-selected labeled "Use profile address" with the address displayed below. If the customer unchecks the checkbox, the address that was shown before will disappear and a new input field for adding a different address will appear. I a ...

Is it possible to extract all parameters, including nested or within arrays, from a Swagger File using Node.js?

Looking for a method to extract and interpret data such as parameters from a Swagger file. Specifically, I am working with the Petstore Swagger API ( ). The definitions within the file contain references to other components, like parameters. One example ...

The Redux toolkit is failing to update/PUT properly, as the data being passed from the frontend to the slice is showing as undefined. Feel

My create(POST) and view(GET) functions are working correctly, but I am having issues with the PUT or update operation. In my FrontEnd, I need to send the Invoice VIN number for modification/update using Redux-Toolkit: import Navigation from "../Auth ...

The Server Components render encountered a glitch

Screenshot of the errorI am encountering a strange error only in the production environment. The lack of additional information leads me to believe it may be due to security measures put in place for production. Unfortunately, I have been unable to repli ...

Performing a task after a process has finished

Currently, I'm working with node JavaScript and facing an issue where I need to run a new function after a loop has completed. In the code snippet provided below, // loop through objects in data, to process it represents a basic for loop iterating ove ...

What sets apart these two JavaScript namespaces?

My main goal is to expertly organize my javascript code by eliminating any global elements. I came across two namespace declaration methods in this informative post, and now I'm looking for your input on the advantages and disadvantages of each. ...

Error code 405 (METHOD NOT ALLOWED) is received when attempting to make a post request to an API

Struggling to develop a basic front end that can communicate with my API. The API functions properly as I am able to retrieve and send data using the POSTMAN client. Fetching data from the server and displaying it on the frontend works fine, but encounteri ...

When passing parameters as an array in PHP, remember to use "[ ]"

My function receives parameters as an array: public myFunction($options = array("option1"=>true, "option2"=>true, "option4"=>"astring"), $anotherparameter = 0) { if($options["option1"] === true) { //Perform all the necessary operatio ...