Search for longest substring in alphabetical order in a given string 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 &lt; Arr.length; i++) {
    if (Arr[i] &gt;= lastSub.slice(-1)) {
      lastSub = lastSub + Arr[i];
      if (lastSub.length &gt; 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;
}
&lt;div id="start"&gt;
  &lt;input id="enterString" value="Please enter string"&gt;
  &lt;button id="search" onclick="findSubStr()"&gt;Sub-String Search&lt;/button&gt;
&lt;/div&gt;
&lt;div id="main"&gt;&lt;/div&gt;




A pen on CodePen.
You don't have to split a string into an Array, you can work on a String directly.

Note that I have removed you inline onclick handler. https://en.wikipedia.org/wiki/Unobtrusive_JavaScript

How your ES3 code probably should have looked



var main = document.getElementById('main');
main.style.visibility = 'visible';
var enterString = document.getElementById('enterString');
// test string azcbobobegghakl, should yield beggh
function findSubStr() {
  var string = enterString.value;
  var lastSub = '';
  var longestSub = '';
  for (var index = 0; index &lt; string.length; index += 1) {
    var character = string.charAt(index);
    if (character &gt;= lastSub.slice(-1)) {
      lastSub += character;
      if (lastSub.length &gt; longestSub.length) {
        longestSub = lastSub;
      }
    } else {
      lastSub = character;
    }
  }
  if (main.firstChild) {
    main.firstChild.nodeValue = longestSub;
  } else {
    main.appendChild(document.createTextNode(longestSub));
  }
  return false;
}

document.getElementById('search').onclick = findSubStr;
#start {
  top: 25%;
  left: 40%;
  position: absolute;
}
#main {
  top: 50%;
  left: 45%;
  font-size: 1.5em;
  position: absolute;
  visibility: hidden;
}
#enterString {
  color: gray;
}
&lt;div id="start"&gt;
  &lt;input id="enterString" placeholder="Please enter string"&gt;
  &lt;button id="search"&gt;Sub-String Search&lt;/button&gt;
&lt;/div&gt;
&lt;div id="main"&gt;&lt;/div&gt;




How it could have looked in ES5



var forEach = Function.prototype.call.bind(Array.prototype.forEach);
var main = document.getElementById('main');
main.style.visibility = 'visible';
var enterString = document.getElementById('enterString');
// test string azcbobobegghakl, should yield beggh
function findSubStr() {
  var lastSub = '';
  var longestSub = '';
  forEach(enterString.value, function(character) {
    if (character &gt;= lastSub.slice(-1)) {
      lastSub += character;
      if (lastSub.length &gt; longestSub.length) {
        longestSub = lastSub;
      }
    } else {
      lastSub = character;
    }
  });
  main.textContent = longestSub;
}

document.getElementById('search').addEventListener('click', findSubStr, false);
#start {
  top: 25%;
  left: 40%;
  position: absolute;
}
#main {
  top: 50%;
  left: 45%;
  font-size: 1.5em;
  position: absolute;
  visibility: hidden;
}
#enterString {
  color: gray;
}
&lt;div id="start"&gt;
  &lt;input id="enterString" placeholder="Please enter string"&gt;
  &lt;button id="search"&gt;Sub-String Search&lt;/button&gt;
&lt;/div&gt;
&lt;div id=main "&gt;&lt;/div&gt;




How it could have looked in ES6



const main = document.getElementById('main');
main.style.visibility = 'visible';
const enterString = document.getElementById('enterString');
// test string azcbobobegghakl, should yield beggh
function findSubStr() {
  let lastSub = '';
  let longestSub = '';
  for (let character of enterString.value) {
    if (character &gt;= lastSub.slice(-1)) {
      lastSub += character;
      if (lastSub.length &gt; longestSub.length) {
        longestSub = lastSub;
      }
    } else {
      lastSub = character;
    }
  }
  main.textContent = longestSub;
}

document.getElementById('search').addEventListener('click', findSubStr, false);
#start {
  top: 25%;
  left: 40%;
  position: absolute;
}
#main {
  top: 50%;
  left: 45%;
  font-size: 1.5em;
  position: absolute;
  visibility: hidden;
}
#enterString {
  color: gray;
}
&lt;div id="start"&gt;
  &lt;input id="enterString" placeholder="Please enter string"&gt;
  &lt;button id="search"&gt;Sub-String Search&lt;/button&gt;
&lt;/div&gt;
&lt;div id="main"&gt;&lt;/div&gt;




There are surely other ways that you could have tackled this, but essentially there is nothing wrong with your approach.

If you have any particular questions about the changes/code posted, then just comment and I will expand in the areas that most need it.