settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Words Uppercase - JavaScript Task

+3 votes

Write a JavaScript program that extracts all words from a passed in string and converts them to upper case. The extracted words in upper case must be printed back on a single line concatenated by “, “.

The input comes as a single string argument - the text from which to extract and convert the words.

Examples:

Input:
'Hi, how are you?'

Output:
HI, HOW, ARE, YOU


Input:
'hello'

Output:
HELLO

The output should be a single line containing the converted string.

asked in JavaScript category by user samfred5830

1 Answer

+2 votes

You might need to use regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Here is the problem's solution with the given example: "Hi, how are you?"

function wordsUppercase(str) {
    let strUpper = str.toUpperCase();
    let words = extractWords();
    words = words.filter(w=>w != "");
    return words.join(", ");
    function extractWords() {
        return strUpper.split(/\W+/);
    }
}

console.log(wordsUppercase("Hi, how are you?"));
answered by user sam
edited by user golearnweb
...