Find Unique Characters - Functions

Find Unique Characters – Functions Cognizant Handson Program

Write a JavaScript program to find the unique characters in a string. The function uniqueCharacters must take a string as its argument and return the same after removing all the characters that appear more than once in the string. This is the main function.

The function modifyString is the sub-function. This is invoked from within the function uniqueCharacters with the same string as the argument.

This function will remove all the white space characters & convert the entire string to lower case and return the same to the caller.

script.js 

function modifyString(str)
{
    //fill code here
    var newStr = "";
    for (var i=0;i<str.length;i++){
        if (str[i] !== " ") newStr += str[i];
    }
    return newStr.toLowerCase();
}

function uniqueCharacters(str)
{
    //fill code here
    var modifiedStr = modifyString(str);
    var profile = {};
    var result = "";
    for (var i=0;i<modifiedStr.length;i++){
        if (profile[modifiedStr[i]] === undefined){
            profile[modifiedStr[i]] = true;
            result += modifiedStr[i];
        }
    }
    return result;
}  

console.log(uniqueCharacters("Welcome to the Javascript course"));

Similar Posts