js algorithm count

function count(arr, item) {
    
     
    var j=0;
    for(var i=0;i<arr.length;i++)
        {
    
    
            if(arr[i]==item)
                {
    
    
                    ++j
                }
        }
    return j;
}

Insert picture description here

Idea: loop through the array, and then determine whether the elements in the array are the same as the item, and create a variable ++j;

Guess you like

Origin blog.csdn.net/qq_37805832/article/details/115190269