Skip to main content

Ivan Teoh

Something personal yet public

167-exercise44.js

(Source)

function startsWith (firstString, secondString) {
    var result = false;
    firstLength = firstString.length;
    secondLength = secondString.length;
    if (secondLength > firstLength)
        return result;

    if (firstString.slice(0, secondLength) == secondString)
        result = true;
    return result;
}
document.write(startsWith("oranges", "orange")); // true
document.write(startsWith("peach", "orange"));   // false
document.write(startsWith("peaches", "grape"));  // false