Compare length of two strings: Difference between revisions

Line 573:
 
=={{header|JavaScript}}==
JavaScript (ECMA Script) file stringlensort.js.
<lang javascript>/**
* Compare and report strings lengths.
*
* @param {Element} input - a TextArea DOM element with input
* @param {Element} output - a TextArea DOM element for output
*/
function compareStringsLength(input, output) {
 
// Safe defaults.
//
output.value = "";
let output_lines = [];
 
// Split input into an array of lines.
//
let strings = input.value.split(/\r\n|\r|\n/g);
 
// Is strings array empty?
//
if (strings && strings.length > 0) {
 
// Remove leading and trailing spaces.
//
for (let i = 0; i < strings.length; i++)
strings[i] = strings[i].trim();
 
// Sort by lengths.
//
strings.sort((a, b) => a.length - b.length);
 
// Remove empty strings.
//
while (strings[0] == "")
strings.shift();
 
// Check if any strings remain.
//
if (strings && strings.length > 0) {
 
// Get min and max length of strings.
//
const min = strings[0].length;
const max = strings[strings.length - 1].length;
 
// Build output verses - longest strings first.
//
for (let i = strings.length - 1; i >= 0; i--) {
let length = strings[i].length;
let predicate;
if (length == max) {
predicate = "is the longest string";
} else if (length == min) {
predicate = "is the shortest string";
} else {
predicate = "is neither the longest nor the shortest string";
}
output_lines.push(`"${strings[i]}" has length ${length} and ${predicate}\n`);
}
 
// Send all lines from output_lines array to an TextArea control.
//
output.value = output_lines.join('');
}
}
}
 
document.getElementById("input").value = "abcd\n123456789\nabcdef\n1234567";
compareStringsLength(input, output);</lang>
HTML file (with embeded CSS) to run the script.
<lang HTML><html>
 
<head>
<style>
div {
margin-top: 4ch;
margin-bottom: 4ch;
}
 
label {
display: block;
margin-bottom: 1ch;
}
 
textarea {
display: block;
}
 
input {
display: block;
margin-top: 4ch;
margin-bottom: 4ch;
}
</style>
</head>
 
<body>
<main>
<form>
<div>
<label for="input">Input:
</label>
<textarea rows="20" cols="80" id="input"></textarea>
</div>
<input type="button" value="press to compare strings" onclick="compareStringsLength(input, output);">
</input>
Output:<br>
<textarea rows="20" cols="80" id="output"></textarea>
</form>
</main>
<script src="stringlensort.js"></script>
</body>
 
</html></lang>
{{Output}}
"123456789" has length 9 and is the longest string
"1234567" has length 7 and is neither the longest nor the shortest string
"abcdef" has length 6 and is neither the longest nor the shortest string
"abcd" has length 4 and is the shortest string
 
=={{header|Julia}}==