资讯专栏INFORMATION COLUMN

Missing letters——Easy algorithm challenge

stormgens / 497人阅读

摘要:并看不懂我不会到处乱说。好吧我努力拆解一下这个正则是什么意思匹配字符串的开始重复一次或更多次匹配除了以外的任意字符原文

problem:

You will create a program that will find the missing letter from a string and return it. If there is no missing letter, the program should return undefined. There is currently no test case for the string missing more than one letter, but if there was one, recursion would be used. Also, the letters are always provided in order so there is no need to sort them.

fearNotLetter("abce") should return "d".
fearNotLetter("abcdefghjklmno") should return "i".
fearNotLetter("bcd") should return undefined.
fearNotLetter("yz") should return undefined.

my solution:
  var arr = [];
  var all;
  for (var i=0; i
发现和上次一样,我在进入循环什么时候return出来这里总是做错,或者想的复杂...
总结一下:return之后会直接结束function;如果for循环一变不出return直接外面来return undefined也是可以的。
basic solution:
function fearNotLetter(str) {

  for(var i = 0; i < str.length; i++) {
    var code = str.charCodeAt(i);

    if (code !== str.charCodeAt(0) + i) {
      return String.fromCharCode(code - 1);
    }  
  }
  return undefined;
}
Intermediate Code Solution:
function fearNotLetter(str) {
  var compare = str.charCodeAt(0), missing;

  str.split("").map(function(letter,index) {
    if (str.charCodeAt(index) == compare) {
      ++compare;
    } else {
      missing = String.fromCharCode(compare);
    }
  });

  return missing;
}
Advanced Code Solution:
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;
}

并看不懂我不会到处乱说。好吧我努力拆解一下这个正则是什么意思...

^    匹配字符串的开始
+    重复一次或更多次 
[^x]    匹配除了x以外的任意字符 

a regular expression notChars which selects everything except str

?: Conditional(ternary)Operator

https://developer.mozilla.org...
Syntax:

condition ? expr1 : expr2

Parameters:

condition (or conditions) An expression that evaluates to true or false. 
expr1, expr2 Expressions with values of any type.

原文:https://forum.freecodecamp.or...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/84102.html

相关文章

  • DNA Pairing——Easy algorithm challenge

    Problem Explanation: You will get a DNA strand sequence and you need to get the pair and return it as a 2D array of the base pairs. Keep in mind that the provided strand should be first always. pairEl...

    luxixing 评论0 收藏0
  • 每日一道算法题 - LetterChanges(easy-4)

    摘要:更多的小算法练习,可以查看我的文章。规则使用语言,使用函数获取传递的参数并使用以下算法对其进行修改。将字符串中的每个字母替换为字母表后面的字母即变为,变为。然后将这个新字符串,,,,中的每个元音大写,并最终返回此修改后的字符串。 虽然都是很简单的算法,每个都只需5分钟左右,但写起来总会遇到不同的小问题,希望大家能跟我一起每天进步一点点。更多的小算法练习,可以查看我的文章。 规则 Usi...

    mo0n1andin 评论0 收藏0
  • 理解基于 docker 的现代化的服务发现

    摘要:我也觉得非常带劲儿,蛋是,我今天相信的才是正确答案,尔切,可以在又贱又容易的网络中使用。工具管理这样的事情在一个中和能够发现,尔切互相可以交谈。这包括了,一个目录这个目录中的注册,尔切,能够查和到目录中的。因为,基于一个简单的,整体简化了。 糙译,[Warning] 继续阅读可能会感到不适 人一生不可能踩到同一滩大便,故而,本文会持续修改。 Understanding Moder...

    _Zhao 评论0 收藏0
  • Awesome Python II

    摘要: Caching Libraries for caching data. Beaker - A library for caching and sessions for use with web applications and stand-alone Python scripts and applications. dogpile.cache - dogpile.cache...

    lx1036 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<