JSFiddle - React, Tailwind, and code Playground

by Graham Fairweather

JavaScript

var testString = "http://stackoverflow.com/questions/askhttp://stackoverflow.com/questions/ask",
    testWord = "http";

function wc(string, word) {
    var length = typeof string === "string" && typeof word === "string" && word.length,
        loop = length,
        index = 0,
        count = 0;

    while (loop) {
        index = string.indexOf(word, index);
        if (index !== -1) {
            count += 1;
            index += length;
        } else {
            loop = false;
        }
    }
    
    return count;
}

console.log(wc(testString, testWord));