JSFiddle - React, Tailwind, and code Playground

by BDG

HTML

<h1>Letter...</h1>
<div id="letter"></div>

<hr/>
<h1>checking...</h1>
<div id="checking"></div>

<hr/>
<h1>Word...</h1>
<div id="output"></div>

<hr/>
<h1>cycles...</h1>
<div id="cycles"></div>

<hr/>
<h1>Palindrome...</h1>
<div id="text">FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth
</div>

<hr/>
<h1>Results...</h1>
<div id="results"></div>

JavaScript

var forward = $("#text").html().toLowerCase().split("");
var alphabet = "abcdefghijklmnopqrstuvwxyz".split("");
var palindrome = {"word" : "", "len" : 0}; //I'm using globals because I'm dirty like that.
var cycles = 0;

function palifiy(dat) {
    var rts = "";
    for (i in dat) {
        rts = dat[i] + rts;
    }
    cycles +=1;
    $("#cycles").html(cycles);
    if (rts.split("") === dat) {
        if (palindrome.len > dat.length) {
            palindrome.len = dat.length;
            palindrome.word= dat;
            $("#results").append(palindrome.len + " - " + palindrome.word + "<br/>");
        }
    }
}

function search(letter, fullText) {
    var testWord = [];
    for (j in fullText){
        for (k in fullText){
            if (fullText[k] == letter && fullText[j] == letter && (j > k)) {
                testWord = fullText.slice(j,k+1);
                
                if (testWord.length > palindrome.len) {
                    $("#checking").append(letter +"<" +j + ","+k+ "> - " +testWord + "<br/>");
                    palifiy(testWord);
                }
            }
        }
    }
}

for (i in alphabet) {
    $("#letter").html(i + " - "+ alphabet[i]);
    search(alphabet[i],forward);
}



alert("Done");