function shortestWordLength(text) {
if (!text || text.trim().length === 0) {
return 0;
}
// Split on any whitespace (spaces, tabs, newlines)
const words = text.trim().split(/\s+/);
let minLen = Infinity;
for (const word of words) {
const len = word.length;
if (len < minLen) {
minLen = len;
}
}
return minLen === Infinity ? 0 : minLen;
}
function main() {
const text = "Find the shortest word length in this string";
const result = shortestWordLength(text);
console.log("Shortest word length:", result);
}
main();
/*
run:
Shortest word length: 2
*/