Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active May 29, 2023 19:49
Show Gist options
  • Save mark05e/1f0028f4e198b8985643a79d7b40df79 to your computer and use it in GitHub Desktop.
Save mark05e/1f0028f4e198b8985643a79d7b40df79 to your computer and use it in GitHub Desktop.
/**
* Returns the first five characters followed by an ellipsis (...) and the last five characters of a given string.
* If the string length is less than or equal to 10, the original string is returned.
*
* @param {string} str - The input string.
* @returns {string} - The modified string with the first and last five characters, separated by an ellipsis.
*/
function showFirstAndLastFive(str) {
// Check if the string length is less than or equal to 10
if (str.length <= 10) {
return str;
}
// Extract the first five characters from the string
let firstFive = str.substr(0, 5);
// Extract the last five characters from the string
let lastFive = str.substr(str.length - 5);
// Return the modified string with the first and last five characters, separated by an ellipsis
return `${firstFive}...${lastFive}`;
}
function showFirstAndLastFive_test() {
console.log(showFirstAndLastFive('Hello, world!')); // "Hello...orld!"
console.log(showFirstAndLastFive('abcdefghijhiklmnopqrst')); // "abcde...pqrst"
console.log(showFirstAndLastFive('a')); // "a"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment