Here's my JavaScript question:
What is the best way to write a comparator function in JavaScript that takes 2 strings s1 and s2 and returns true if s1 is alphabetically before s2, and false otherwise?
You cannot use the built in sort function, you must create your own sort function. Input strings s1 and s2 are not case sensitive and might not be equal length.
Here's two examples: compare('aaa', 'aab') returns true, and compare('aaa', 'aaab') returns true..
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function check(str1,str2) {
return str1.localeCompare(str2)==-1;
}
function myFunction(){
alert(check("aaa","aab"));
}
</script>
</body>
</html>
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME
Get Answers For Free
Most questions answered within 1 hours.