Created
June 14, 2022 20:50
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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