Find Unique Characters - Functions
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.
Note: You may verify the correctness of your function by invoking it from within console.log
console.log(uniqueCharacters("Welcome to the Javascript course"));
Console Output:
Solution:
function modifyString(str)
{
//fill code here
str=str.replace(/\s/g,'').toLowerCase();
return str;
}
function uniqueCharacters(str)
{
//fill code here
str=modifyString(str);
var ans="";
for(var i=0;i<str.length;i++){
if(ans.includes(str[i])===false){
ans+=str[i];
}
}
return ans;
}
console.log(uniqueCharacters("Welcome to the Javascript course"));
Why is the solution blank?.
ReplyDelete