JSFiddle - React, Tailwind, and code Playground

by rasikasampath

JavaScript

//Uses the substring function
//Checks from the start of the string to the
//d.length number of characters
String.prototype.startsWith = function (d) {
    var first = this.substr(0,d.length);
    if(d===first){
    return true;
    }else{
    return false;
    }    
};

//Uses the substing functio
//Checks d.length number of characters from the end
String.prototype.endsWith = function (d) {
    var startIndex = this.length - d.length;
    if(startIndex>0){
    var last = this.substr(startIndex,d.length );
        if(d===last){
            return true;
        }else{
        return false;
        }    
    }else{
        return false;
    }
};
/*
var toCheck = "hang the dj";
alert(toCheck.startsWith("hang"));
alert(toCheck.startsWith("Hang"));
alert(toCheck.startsWith("I’ve got a room for rent"));
alert(toCheck.startsWith(""));
alert(toCheck.startsWith("hang the"));
alert(toCheck.startsWith("42"));
alert(toCheck.startsWith("{ first: “Johnny” }"));

alert(toCheck.endsWith("dj"));
alert(toCheck.endsWith("panic on the streets"));
alert(toCheck.endsWith(""));
alert(toCheck.endsWith("the dj"));
alert(toCheck.endsWith("42"));
alert(toCheck.endsWith("{ first: “Johnny” }"));
*/