Skip to content

Instantly share code, notes, and snippets.

@mark05e
Last active May 29, 2023 19:48
Show Gist options
  • Save mark05e/961bb0e4c80a4b419316a763008ba158 to your computer and use it in GitHub Desktop.
Save mark05e/961bb0e4c80a4b419316a763008ba158 to your computer and use it in GitHub Desktop.
/**
* 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