javascript 在字母表中查找丢失的字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31349855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find missing letter in list of alphabets
提问by smeloa
I am trying to solve the following issue:
我正在尝试解决以下问题:
Find the missing letter in the passed letter range and return it. If all letters are present in the range, return undefined.
在传递的字母范围内找到丢失的字母并返回。如果范围内存在所有字母,则返回 undefined。
the inputs that I will get as strings are:
我将作为字符串获得的输入是:
- abce (Which should return d)
- bcd (which should return undefined)
- abcdefghjklmno (which should return i)
- yz (which should return undefined)
- abce(应该返回 d)
- bcd(应该返回 undefined)
- abcdefghjklmno(应该返回 i)
- yz(应该返回 undefined)
my code currently looks like this:
我的代码目前看起来像这样:
function fearNotLetter(str) {
//create alphabet string
//find starting letter in alphabet str, using str
//compare letters sequentially
//if the sequence doesn't match at one point then return letter
//if all letters in str appear then return undefined
var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
var i = 0;
var j = 0;
while (i<alphabet.length && j<str.length) {
i++;
if (alphabet.charCodeAt(i) === str.charCodeAt(j)) {
i++;
j++;
}
else if (alphabet.charCodeAt(i) !== str.charCodeAt(j)) {
i++;
j++;
if (alphabet.charCodeAt(i) === str.charCodeAt(j-1)) {
return alphabet.charCodeAt(i-1);
}
}
}
}
fearNotLetter('abce');
Thanks for your help as always!
感谢您一如既往的帮助!
回答by blex
I would do it like this:
我会这样做:
function fearNotLetter(str) {
var i, j = 0, m = 122;
if (str) {
i = str.charCodeAt(0);
while (i <= m && j < str.length) {
if (String.fromCharCode(i) !== str.charAt(j)) {
return String.fromCharCode(i);
}
i++; j++;
}
}
return undefined;
}
console.log(fearNotLetter('abce')); // "d"
console.log(fearNotLetter('bcd')); // undefined
console.log(fearNotLetter('bcdefh')); // "g"
console.log(fearNotLetter('')); // undefined
console.log(fearNotLetter('abcde')); // undefined
console.log(fearNotLetter('abcdefghjkl')); // "i"
i
can go from 97 to 122, this interval corresponds to the ASCII codes of the lower case alphabet.
i
可以从 97 到 122,这个区间对应于小写字母的ASCII 码。
If you want it not to be case sensitive, just do str = str.toLowerCase()
at the beginning of the function.
如果您希望它不区分大小写,只需str = str.toLowerCase()
在函数的开头执行即可。
回答by m69 ''snarky and unwelcoming''
I think this is the simplest code to do this:
我认为这是最简单的代码:
function skippedLetter(str) {
for (var i = 0; i < str.length - 1; i++) {
if (str.charCodeAt(i + 1) - str.charCodeAt(i) != 1) {
return String.fromCharCode(str.charCodeAt(i) + 1);
}
}
}
alert(skippedLetter('abce'));
This version will reject illegal input, accept both upper and lower case, check that there is only 1 hole in the range, and that there is exactly 1 character missing.
此版本将拒绝非法输入,接受大小写,检查范围内是否只有 1 个空洞,并且恰好缺少 1 个字符。
function skippedLetter(str) {
if (!str.match(/^[a-zA-Z]+$/)) return;
var letter = "", offset = str.charCodeAt(0);
for (var i = 1; i < str.length; i++) {
var diff = str.charCodeAt(i) - i - offset;
if (diff == 1) letter += String.fromCharCode(i + offset++)
else if (diff) return;
}
if (letter.length == 1) return letter;
}
alert(skippedLetter('123567')); // illegal characters
alert(skippedLetter('')); // empty string
alert(skippedLetter('a')); // too short
alert(skippedLetter('bc')); // nothing missing
alert(skippedLetter('df')); // skipped letter = e
alert(skippedLetter('GHIKLM')); // skipped letter = J
alert(skippedLetter('nOpRsT')); // cases mixed
alert(skippedLetter('nopxyz')); // too many characters missing
alert(skippedLetter('abcefgijk')); // character missing more than once
alert(skippedLetter('abcefgfe')); // out of order
回答by Rick Hitchcock
Note that you have a typo in alphabet
: There are two "e"s.
请注意,您输入了一个拼写错误alphabet
:有两个“e”。
You could split
the string into an array, then use the some
method to short-circuit the loop when you don't find a match:
您可以split
将字符串转换为数组,然后在some
找不到匹配项时使用该方法使循环短路:
function fearNotLetter(str) {
var alphabet = 'abcdefghijklmnopqrstuvwxyz',
missing,
i= 0;
str.split('').some(function(l1) {
var l2= alphabet.substr(i++, 1);
if(l1 !== l2) {
if(i===1) missing= undefined;
else missing= l2;
return true;
}
});
return missing;
}
console.log(fearNotLetter('abce')); //d
console.log(fearNotLetter('bcd')); //undefined
console.log(fearNotLetter('abcdefghjklmno')); //i
console.log(fearNotLetter('yz')); //undefined
回答by Mindastic
Another function that may help:
另一个可能有帮助的功能:
var alphabet = "abcdefgheijklmnopqrstuvwxyz";
function fearNotLetter(a) {
function letterIndex(text, index) {
var letter = text.charAt(0);
if (alphabet.indexOf(letter) !== index) { return alphabet.charAt(index); } else { return letterIndex(text.substring(1), index + 1) }
}
if (alphabet.indexOf(a) === -1) {
return letterIndex(a, alphabet.indexOf(a.charAt(0)));
}
return undefined;
}
fearNotLetter("abc"); //Undefined
fearNotLetter("abce"); //d
fearNotLetter("fgi"); //h
回答by ChadF
This will do what you're looking for:
这将执行您正在寻找的操作:
Hit run and check your console
点击运行并检查您的控制台
function missingLetter (str) {
var alphabet = ("abcdefghijklmnopqrstuvwxyz");
var first = alphabet.indexOf(str[0]);
var strIndex = 0;
var missing;
for (var i = first ; i < str.length ; i++) {
if (str[strIndex] === alphabet[i]) {
strIndex++;
} else {
missing = alphabet[i];
}
}
return missing;
}
console.log(missingLetter("abce"));
console.log(missingLetter("bcd"));
console.log(missingLetter("abcdefghjklmno"));
console.log(missingLetter("yz"));
回答by sergiu reznicencu
I think that you wanted to say that if a string doesn't start with "a" than return "undefined". So here's my code:
我认为您想说的是,如果字符串不以“a”开头,则返回“undefined”。所以这是我的代码:
function fearNotLetter(str) {
var alphabet = ("abcdefgheijklmnopqrstuvwxyz");
var i = 0;
var j = 0;
while (i < alphabet.length && j < str.length) {
if (alphabet.charAt(i) != str.charAt(j)) {
i++;
j++;
if (alphabet.charAt(i - 1) == "a") {
return "undefined";
} else {
return (alphabet.charAt(i - 1));
}
}
i++;
j++;
}
}
alert(fearNotLetter('abce'));
Here's the working JsFiddle.
这是工作中的JsFiddle。
You wanted the code to return the missing letter so I used CharAt.
You can make an array of letters and then search through it to see if it maches with letters from the string....
您希望代码返回丢失的字母,所以我使用了 CharAt。
您可以制作一个字母数组,然后搜索它以查看它是否与字符串中的字母匹配....
回答by Giorgio Giuliani
function fearNotLetter(str) {
var a = str.split('');
var array = [];
var j = 0;
for (var i = 1; i < a.length; i++) {
var d = a[i].charCodeAt(0);
var c = a[i - 1].charCodeAt(0);
var delta = d - c;
if (delta != 1) {
array[i] = String.fromCharCode(a[i - 1].charCodeAt(0) + 1);
}
}
str = array.join('');
if (str.length === 0) {
return undefined;
} else {
return str;
}
}
fearNotLetter('abcefr');
回答by Saba Ahang
How about this one? it finds all missing letters anywhere between the first and the last given letters:
这个怎么样?它在第一个和最后一个给定字母之间的任何位置找到所有丢失的字母:
function fearNotLetter(str) {
var strArr = str.split('');
var missingChars = [], i = 0;
var nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
while (i<strArr.length - 1) {
if (nextChar !== strArr[i+1]){
missingChars.push(nextChar);
nextChar = String.fromCharCode(nextChar.charCodeAt(0)+1);
} else {
i++;
nextChar = String.fromCharCode(strArr[i].charCodeAt(0)+1);
}
}
return missingChars.join('') === '' ? undefined : missingChars.join('') ;
}
console.log(fearNotLetter("ab"));
回答by Saba Ahang
This is an even shorter answer thanks to RegExp()
that allows you to create a Regular Expression on the fly and use match()
to strip off the given letters from a generated String that has all the letters in the given range:
这是一个更短的答案,因为RegExp()
它允许您即时创建正则表达式并用于match()
从生成的字符串中去除给定的字母,该字符串具有给定范围内的所有字母:
function fearNotLetter(str) {
var allChars = '';
var notChars = new RegExp('[^'+str+']','g');
for (var i=0;allChars[allChars.length-1] !== str[str.length-1] ;i++)
allChars += String.fromCharCode(str[0].charCodeAt(0)+i);
return allChars.match(notChars) ? allChars.match(notChars).join('') : undefined;
}
回答by Koushik Das
Here is what I use:
这是我使用的:
function fearNotLetter(str) {
var firstLtrUnicode = str.charCodeAt(0),
lastLtrUnicode = str.charCodeAt(str.length - 1);
var holder = [];
for (var i=firstLtrUnicode; i<=lastLtrUnicode; i++) {
holder.push(String.fromCharCode(i));
}
var finalStr = holder.join('');
if ( finalStr === str ) { return undefined; }
else { return holder.filter( function(letter) {
return str.split('').indexOf(letter) === -1;
}).join(''); } }