成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

Missing letters——Easy algorithm challenge

stormgens / 602人閱讀

摘要:并看不懂我不會(huì)到處亂說(shuō)。好吧我努力拆解一下這個(gè)正則是什么意思匹配字符串的開(kāi)始重復(fù)一次或更多次匹配除了以外的任意字符原文

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
發(fā)現(xiàn)和上次一樣,我在進(jìn)入循環(huán)什么時(shí)候return出來(lái)這里總是做錯(cuò),或者想的復(fù)雜...
總結(jié)一下:return之后會(huì)直接結(jié)束function;如果for循環(huán)一變不出return直接外面來(lái)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;
}

并看不懂我不會(huì)到處亂說(shuō)。好吧我努力拆解一下這個(gè)正則是什么意思...

^    匹配字符串的開(kāi)始
+    重復(fù)一次或更多次 
[^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...

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/84102.html

相關(guān)文章

  • 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 評(píng)論0 收藏0
  • 每日一道算法題 - LetterChanges(easy-4)

    摘要:更多的小算法練習(xí),可以查看我的文章。規(guī)則使用語(yǔ)言,使用函數(shù)獲取傳遞的參數(shù)并使用以下算法對(duì)其進(jìn)行修改。將字符串中的每個(gè)字母替換為字母表后面的字母即變?yōu)椋優(yōu)?。然后將這個(gè)新字符串,,,,中的每個(gè)元音大寫,并最終返回此修改后的字符串。 雖然都是很簡(jiǎn)單的算法,每個(gè)都只需5分鐘左右,但寫起來(lái)總會(huì)遇到不同的小問(wèn)題,希望大家能跟我一起每天進(jìn)步一點(diǎn)點(diǎn)。更多的小算法練習(xí),可以查看我的文章。 規(guī)則 Usi...

    mo0n1andin 評(píng)論0 收藏0
  • 理解基于 docker 的現(xiàn)代化的服務(wù)發(fā)現(xiàn)

    摘要:我也覺(jué)得非常帶勁兒,蛋是,我今天相信的才是正確答案,爾切,可以在又賤又容易的網(wǎng)絡(luò)中使用。工具管理這樣的事情在一個(gè)中和能夠發(fā)現(xiàn),爾切互相可以交談。這包括了,一個(gè)目錄這個(gè)目錄中的注冊(cè),爾切,能夠查和到目錄中的。因?yàn)?,基于一個(gè)簡(jiǎn)單的,整體簡(jiǎn)化了。 糙譯,[Warning] 繼續(xù)閱讀可能會(huì)感到不適 人一生不可能踩到同一灘大便,故而,本文會(huì)持續(xù)修改。 Understanding Moder...

    _Zhao 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<