Last active
May 29, 2023 19:48
-
-
Save mark05e/961bb0e4c80a4b419316a763008ba158 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Checks if a given string contains any space character. | |
* | |
* @param {string} str - The input string to be checked. | |
* @throws {Error} Throws an error if the string contains a space. | |
* @returns {boolean} - Returns true if the string does not contain a space. | |
*/ | |
function checkForSpace(str) { | |
// Check if the string includes a space character | |
if (str.includes(' ')) { | |
// If a space is found, throw an error with a specific message | |
throw new Error('String should not contain space'); | |
} | |
// If the string does not contain a space, return true or perform any other desired operation | |
return true; | |
} | |
function checkForSpace_test() { | |
checkForSpace('hello'); // Returns true | |
checkForSpace('hello world'); // Throws an error with message 'String should not contain space' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment