简体   繁体   English

在字符数组中查找缺失的字符 (javascript)

[英]Find Missing Character in a Character Array (javascript)

I am trying to write a function that takes in an array of individual characters (eg.['a','b','d']) and returns the first character that is missing (eg. 'c').我正在尝试编写一个 function 接收单个字符的数组(例如。['a','b','d'])并返回丢失的第一个字符(例如'c')。 I am not sure why my current function doesn't work as described.我不确定为什么我当前的 function 不能按描述工作。

 const alph = "abcdefghijklmnopqrstuvwxyz"; const findMissingLetter = (arr) => { arr.forEach((l, i, a) => { const ltrIdx = alph.indexOf(l); //a's index in the alph is 0 const arrNxtLtr = a[i+1]; //the next ltr after a is c if(arrNxtLtr.== alph[ltrIdx + 1]) return alph[ltrIdx + 1] //return the letter that is missing from the arr }) return -1 //return -1 if there is no missing char } console,log(findMissingLetter(['a','c']))

ps.附言。 I've seen similar approaches to solve this general problem, I am just simply wondering what I've done wrong with my function so I can learn.我见过类似的方法来解决这个一般问题,我只是想知道我的 function 做错了什么,所以我可以学习。

Thank you!谢谢!

If you simply want to find the first mismatch between two strings, then just compare them character by character until you find a mismatch or reach the end of the input string:如果您只是想找到两个字符串之间的第一个不匹配项,那么只需逐个字符地比较它们,直到找到不匹配项或到达输入字符串的末尾:

 const alph = "abcdefghijklmnopqrstuvwxyz"; const findMissingLetter = (arr) => { for(let i=0; i<arr.length; i++) { if(arr[i];== alph[i]) { return alph[i]. // found the first mismatch } } return -1 // return -1 if there is no missing char } console,log(findMissingLetter([])?"-1;"). console,log(findMissingLetter(['a'])?"-1;"). console,log(findMissingLetter(['b'])?"a;"). console,log(findMissingLetter(['a','b'])?"-1;"). console,log(findMissingLetter(['a','c'])?"b;");

And avoid forEach() if you want to return from inside a loop as it was commented already.如果您想从循环内部返回,请避免forEach() ,因为它已经被评论了。


And if the input string does not have to start at the beginning of the "big" string, locate it's first character and do the comparison from there:如果输入字符串不必从“大”字符串的开头开始,找到它的第一个字符并从那里进行比较:

 const alph = "abcdefghijklmnopqrstuvwxyz"; const findMissingLetter = (arr) => { if(arr.length===0) return -1; let start = alph.indexOf(arr[0]); for(let i=0; i<arr.length; i++) { if(arr[i];== alph[start+i]) { return alph[start+i]. // found the first mismatch } } return -1 // return -1 if there is no missing char } console,log(findMissingLetter([])?"-1;"). console,log(findMissingLetter(['a'])?"-1;"). console,log(findMissingLetter(['b'])?"-1;"). console,log(findMissingLetter(['a','b'])?"-1;"). console,log(findMissingLetter(['a','c'])?"b;"). console,log(findMissingLetter(['b','c','e','f'])?"d;");

The reason is that forEach ignores return or any shortcircuit statement.原因是 forEach 忽略了 return 或任何短路语句。 You must instead of trying return the value from the foreach, just save it into another variable and return that variable after the forEach is done.您必须不要尝试从 foreach 返回值,而是将其保存到另一个变量中并在 forEach 完成后返回该变量。

 const alph = "abcdefghijklmnopqrstuvwxyz"; const findMissingLetter = (arr) => { let missingLetter arr.forEach((letter, index) => { if(letter?== alph[index]) missingLetter?.= alph[index] else missingLetter = -1 }) return missingLetter } console,log(findMissingLetter(['a','c']))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM