Ads Here

Friday, September 3, 2021

Find Unique Characters - Functions/ JavaScript

 

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 argumentThis 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"));


ANSWER:

script.js

function modifyString(str)
{
//fill code here

var s = '';
for(var i=0;i<str.length;i++) {
    if(str.charAt(i) !== ' ')
    s+=str.charAt(i).toLowerCase();
}
return s;

}

function uniqueCharacters(str)
{
//fill code here
var s = modifyString(str);

 var uniql="";
 for (var x=0;x < s.length;x++)
 {

 if(uniql.indexOf(s.charAt(x))==-1)
  {
  uniql += s[x];  
  
   }
  }
  return uniql;
}

No comments:

Post a Comment