Skip to content

Instantly share code, notes, and snippets.

@harryWonder
Created June 14, 2022 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harryWonder/fad79fd69646e7d21df3fc041f40d57e to your computer and use it in GitHub Desktop.
Save harryWonder/fad79fd69646e7d21df3fc041f40d57e to your computer and use it in GitHub Desktop.
This function takes a string and limits it from the bottom based on the output length.
/**
* This function takes a string and limits it from the bottom based on the output length.
*
* The following strings are invalid as answers "Codility we test code": This is because we need to return coders instead.
* The string output should not be less than the output length as well.
* The string can return an empty string as an answer too,
*
* @param {string} message
* @param {number} outputLength
* @returns {string}
*/
function trimString(message = "Codility We test coders", outputLength) {
let messageArray = message.split(" ");
let strippedMessage = message.substring(0, outputLength);
let strippedMessageArray = strippedMessage.split(" ");
/* Compare each words match */
let newMessage = strippedMessageArray.map((el) => {
if (messageArray.includes(el)) {
return el;
}
return null;
}).filter((el) => el != null);
newMessage = newMessage.join(" ");
console.log(newMessage);
return newMessage;
}
trimString("Codility We test coders", 14);
trimString("The quick brown fox jumps over the lazy dog", 21);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment