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();
var alphabet = "abcdefghijklmnopqrstuvwxyz";
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 === dat) {
if (palindrome.len > dat.length) {
palindrome.len = dat.length;
palindrome.word= dat;
$("#results").append(palindrome.len + " - " + palindrome.word + "<br/>");
$("#results").append(palindrome.len + " - " + dat + "<br/>");
}
}
}
//Letter to letter, lover to lover.. something something, sappy music... la la la...
function search(letter, fullText) {
$("#output").append(letter + " - " + fullText +"<br/>");
if (fullText.lastIndexOf(letter) != fullText.indexOf(letter)) {
testWord = fullText.substring(0, fullText.indexOf(letter,1));
$("#checking").append(testWord + "<br/>");
if (testWord.length > palindrome.len) {
palifiy(testWord);
search(letter, fullText.substr(testWord.length)); //To first recursion you must understand??
}
}
}
for (i in alphabet) {
$("#letter").html(i + " - "+ alphabet[i]);
search(alphabet[i],forward);
}
alert("Done");