Forum : To find a Number of unique letters in string
Brief description  about Online courses   join in Online courses
View Santosh  Hegde 's Profile

To find a Number of unique letters in string

<SCRIPT LANGUAGE="JavaScript">
function unique() {
var st = window.prompt("Enter string: ");
var arr=st.split("");
var resulted = new Array();
for (var j=0; j<arr.length; j ) {
var key = arr[j].toString();
if (!resulted[key]) {
resulted[key] = 1
} else {
resulted[key] = resulted[key] 1;
}
}
var str = "";
for (var j in results) {
if(resulted[j]==1){
str = j;
}
}
alert(str.length);
}
</script>



<input type="button" onclick="unique()" value="Click Me!"style="color:white; background-color:#FF9200;"
onmouseover="this.style.color='#999999'; this.style.backgroundColor='#F88017'"
onmouseout="this.style.color='white'; this.style.backgroundColor='#FF9200'"
onmousedown="this.style.color='white'; this.style.backgroundColor='#4E387E'"
onmouseup="this.style.color='black'; this.style.backgroundColor='#F88017'"/>

I provided onClick event for this function and this code not working properly, i mean in the prompt box if entered any letters or number it will goes out without alert or without prompt...!!
Asked by Santosh Hegde | Nov 28, 2010 |  Reply now
Replies (3)
View teacher siliconindia 's Profile
Replied through mail.
Dec 1, 2010
View santosh hegde 's Profile
Yes sir, i know that,
i found something wrong with these code.
and posted here to clear my doubt
ok..
i`ll Mail you the same event again..
Nov 29, 2010
View teacher siliconindia 's Profile
Hi Santosh,

<SCRIPT LANGUAGE="JavaScript">
function unique() {
var st = window.prompt("Enter string: ");
var arr=st.split("");
var resulted = new Array();
for (var j=0; j<arr.length; j) { // specify J should be incremented or not.. if not it ll goto endless loop.. script error will come.
var key = arr[j].toString();
if (!resulted[key])
// brace missing..
resulted[key] = 1
} else {
resulted[key] = resulted[key] 1; // syntax error
}
}
var str = "";
for (var j in results) {
if(resulted[j]==1){
str = j;
}
}
alert(str.length);
}
</script>



<input type="button" onclick="unique()" value="Click Me!"style="color:white; background-color:#FF9200;"
onmouseover="this.style.color='#999999'; this.style.backgroundColor='#F88017'"
onmouseout="this.style.color='white'; this.style.backgroundColor='#FF9200'"
onmousedown="this.style.color='white'; this.style.backgroundColor='#4E387E'"
onmouseup="this.style.color='black'; this.style.backgroundColor='#F88017'"/>


invalid code posted.. some syntax errors.. post again. or send me a mail at vishwanath@siliconindia.com
Nov 28, 2010