Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm quite new to programming and started with JavaScript.
I'm curious if there is a method to check if a variable has all letters of the alphabet at least once.

What I have tried:

So I have the variable

var string = prompt("Type in message","");

Curious how to check if the message typed in has every letter a to z. if yes alert("All letters found at least once")
Posted
Updated 5-Oct-17 0:22am
v2

try

var alphabets26 = 'abcdefghijklmnopqrstuvwxyz';
       var input = prompt();
       input = input.toLowerCase();
       var icount = 0;
       for (var i = 0; i < alphabets26.length; i++) {
           var letter = alphabets26[i];
           if (input.indexOf(letter) > -1)
               icount++;
       }
       if (icount == 26)
           alert('All letters found at least once');

       else
           alert('Few letters missing');


Demo : - JSFiddle[^]
 
Share this answer
 
v3
Comments
Member 13446970 5-Oct-17 4:38am    
Thank you. It works as expected.
Karthik_Mahalingam 5-Oct-17 4:39am    
welcome
CPallini 5-Oct-17 4:45am    
You have a 'z' instead of a 'x' in you alphabets26 string.
Karthik_Mahalingam 5-Oct-17 5:32am    
:) typo, Hawk Eye
Member 13446970 5-Oct-17 4:57am    
Could you please add some comments as in why did you use a for statement there?
You can easily implement yourself, for instance
JavaScript
var exists =  [];

var s = "abcdefoo"; // the input string


for (var i=0; i < s.length; ++i)
{
  exists[s[i]] = 1;
}


for ( var i = 97; i<=122; ++i) // loop over all lowercase characters
{
  c = String.fromCharCode(i);
  if ( !exists[c])
  {
    alert(  c + " is missing");
    break;
  }
}
 
Share this answer
 
One more solution
var Your_String= "abcdefghijklmnopqrstuvwxyz";
var Result = Your_String.replace(' ','').split('').filter(function(item, i, ar){ return ar.indexOf(item) === i; }).join('');
document.writeln(Result.length == 26 ? "All used" : "Some missing");

Made use of : javascript - Showing unique characters in a string only once - Stack Overflow[^]
 
Share this answer
 
v3
Comments
Member 13446970 5-Oct-17 6:40am    
Tried this but it will just pop up the message all used
Thanks7872 5-Oct-17 6:45am    
Its just an example. Once you get the length, you can use it anyway you want.
Member 13446970 5-Oct-17 8:49am    
oh I see. Cheers

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900