Learning JavaScript is crucial for web development. JavaScript arrays are important for organizing data in dynamic web applications, playing a crucial role in client-side operations and server communication. Additionally, mastering basic algorithms, such as finding the shortest string in an array, is foundational, enhancing a programmer’s problem-solving skills for complex tasks in software development. JavaScript does provide a vast variety of building blocks for problem-solving, but there are no built-in functions to find the shortest element in the array. Let's see how to find the shortest element in the array using JavaScript.
Let’s consider the following array consisting of five strings:
0 | 1 | 2 | 3 | 4 |
"function" | "variable" | "array" | "algorithm" | "loop" |
The goal is to find the shortest string in this array, i.e., “loop.”
Given an array of strings, we can solve this problem as follows:
Select the string at the 0th index of the array as the shortest string
Starting from the 1st index of the array, iterate through the array and for each of the strings:
Compare the length of the string with the shortest string.
If the length of the current string is less than the shortest string, set this current string as the shortest string.
At the end of the loop, we will have the shortest string in the array.
const stringArray = ["function", "variable", "array", "algorithm", "loop"];let shortestString = stringArray[0];// Starting the loop from index 1 because index 0 is already stored in a variable for comparisonfor (let i = 1; i < stringArray.length; i++){const currentString = stringArray[i];if (currentString.length < shortestString.length) {shortestString = currentString;}}console.log("The shortest string is:", shortestString);
Line 1: We declare a JavaScript array.
Line 2: We initialize the variable shortestString
with the first element of the array.
Line 4: The for
loop is created to iterate through the array, starting from the second element, i.e., i = 1
. (Since shortestString
is initialized with string at index 0, starting the loop at index 1 will save an unnecessary comparison of shortestString
with string at index 0 of the array).
Line 5: The string at the index i
is stored in the currentString
variable.
Line 7–9: The length of each string is being compared with the current shortestString
. If a shorter string is found, shortestString
is updated with the currentString
.
Line 12: The final result is printed to the console.
Here is a dry run of the example illustrating how the algorithm works:
|
|
|
|
|
1 | function | variable | False | No update |
2 | function | array | True | shortestString = array |
3 | array | algorithm | False | No update |
4 | array | loop | True | shortestString = loop |
The time complexity of the provided code is
The space complexity is shortestTerm
, i
, and currentTerm
, and they do not depend on the size of the input array.
In conclusion, this simple algorithm allows us to find the shortest string in an array efficiently. This versatile approach can be adapted to similar problems where you need to find the minimum value based on a specific criterion.
Free Resources