Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Check if a string is sorted in JavaScript
We are required to write a JavaScript function that takes in a string and checks whether it is sorted or not.
For example −
isSorted('adefgjmxz') // true
isSorted('zxmfdba') // true
isSorted('dsfdsfva') // false
Example
Following is the code −
const str = 'abdfhlmxz';
const findDiff = (a, b) => a.charCodeAt(0) - b.charCodeAt(0);
const isStringSorted = (str = '') => {
if(str.length 0){
res += 'u';
}else if(findDiff(str[i+1], str[i]) Output
This will produce the following output in console −
true
Advertisements
