/**
* Returns the last word from a string.
* Empty or whitespace-only strings return an empty string.
*/
function getLastWord(input) {
const trimmed = input.trim();
if (trimmed === "") {
return "";
}
// Split on whitespace and return the last element
return trimmed.split(/\s+/).pop();
}
const tests = [
"nodejs javascript php c c# python c++",
"",
"c#",
"c c++ java ",
" "
];
tests.forEach((t, i) => {
console.log(`${i + 1}. ${getLastWord(t)}`);
});
/*
run:
1. c++
2.
3. c#
4. java
5.
*/