I would like some help with a review of a program that I wrote to identify the longest continuous sub-string that is in alphabetical order within a string. This is a JavaScript adaptation of an exercise originally written in Python. I am new to JavaScript. I would like any help identifying ways that this program could be written more efficiently, specifically if there are techniques or conventions in JavaScript that could improve this program.
One thing that was different between Python and JavaScript was that I had to split the string into an array in order to perform the comparison of characters. I saw another example where this was the technique to assess the strings in JavaScript.
Is there a better way in JavaScript to assess strings when looking for components other than creating an array?
The second part of the program that I would like help with is the way that the search executes immediately, instead of waiting for the call to the function with the button. I left the label in the code to show that the alphabetical sub-string in the place holder language is immediately identified, instead of waiting for a call to the function with the button.
What should I have done to make the function wait for a new search string?
// test string azcbobobegghakl, should yield beggh
function findSubStr() {
document.getElementById("main").style.visibility = "visible";
var string = document.getElementById("enterString").value;
var lastSub = [],
longestSub = [];
var Arr = string.split('');
for (var i = 0; i < Arr.length; i++) {
if (Arr[i] >= lastSub.slice(-1)) {
lastSub = lastSub + Arr[i];
if (lastSub.length > longestSub.length) {
longestSub = lastSub;
}
} else {
lastSub = Arr[i];
}
}
document.getElementById("main").innerHTML = longestSub;
}
console.log(findSubStr());
#start {
top: 25%;
left: 40%;
position: absolute;
}
#main {
top: 50%;
left: 45%;
font-size: 1.5em;
position: absolute;
visibility: hidden;
}
#enterString {
color: gray;
}
<div id="start">
<input id="enterString" value="Please enter string">
<button id="search" onclick="findSubStr()">Sub-String Search</button>
</div>
<div id="main"></div>
A pen on CodePen.