更新时间:2023-02-26 12:39:43
尝试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('所有字母至少找到一次');
其他
警报('少数字母丢失');
演示: - JSFiddle [ ^ ]
你可以轻松实现自己,例如
var exists = [];
var s = abcdefoo跨度>; // 输入字符串
for ( var i = 0 ; i < s.length; ++ i)
{
exists [s [i]] = 1 ;
}
for ( var i = 97 ; i< = 122 ; ++ i) / / 遍历所有小写字符
{
c = String 。 fromCharCode(ⅰ);
if (!exists [c])
{
alert(c + 缺少);
break ;
}
}
还有一个解决方案var Your_String =abcdefghijklmnopqrstuvwxyz ;
var Result = Your_String.replace('','')。split('')。filter(function(item,i,ar){return ar.indexOf(item)=== i;})。加入('');
document.writeln(Result.length == 26?全部使用:有些遗失);
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")
tryvar 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[^]
You can easily implement yourself, for instancevar 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; } }
One more solutionvar 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[^]