FCC Intermediate algorithm to find missing letters

Source: Internet
Author: User

Missing Letters

Find the missing letter from the sequence of letters passed in and return it.

Returns undefined if all letters are in the sequence.

String.charcodeat ()

String.fromCharCode ()

Ideas:

(1) Converts a string directly into a number in sequential order into arr;

(2) If there is no missing letter, the maximum value of the array minus the minimum value plus 1 should be equal to the length of the array, using this feature, create such an ideal no missing array newarr;

(3) Remove the same element as the array arr from the newarr, so we find the missing letter number, because there may be more than one omission, so I put it into an array;

(4) Convert the element numbers of an array into letters, and use the Join () method to convert to a string;

Knowledge Points:

(1) String.charcodeat () The return value is a number that represents the UTF-16 code unit value of the character at the given index, or returns if the index is out of range NaN ;

1    for (var i = 0; i < str.length; i++) {2         // Converts a string directly into a number sequentially into an array in arr 3         Arr.push (str.charcodeat (i)); 4     }

(2) The String.fromCharCode () method returns a string created using the specified sequence of Unicode values.

1    for (var k = 0; k < miss.length; k++) {2             // Converts a number in an array to a character again, at this point in the form of an array 3             Miss[k] = string.fromcharcode (miss[k]); 4         }

Note that the usage is string.fromcharcode (num), and the parentheses enclose the number required for conversion;

Code:

1 functionFearnotletter (str) {2     vararr = [];3     varNEWARR = [];4     varMiss = [];5      for(vari = 0; i < str.length; i++) {6         //Converts a string directly into a number sequentially into an array in arr7 Arr.push (Str.charcodeat (i));8     }9     varA = arr[arr.length-1]-arr[0];Ten  One     if(Arr.length-1! =a) { A         //if there are no missing letters, the maximum value of the array minus the minimum plus 1 should be equal to the length of the array -          for(varj = 0; J < A + 1; J + +) { -             //using this feature, create an ideal array of newarr that is not missing. theNEWARR[J] = arr[0] +J; -         } -         //remove the same elements from the newarr as the array arr -Miss = Newarr.filter (function(value) { +             returnArr.indexof (value) = = = 1; -         }); +          for(vark = 0; K < Miss.length; k++) { A             //Converts a number in an array to a character again, at this point in the form of an array atMISS[K] =String.fromCharCode (Miss[k]); -         } -         //Convert an array to a string (I consider a case where more than one letter is missing) -         varletter = Miss.join (' '); -         returnLetter ; -}Else { in         //no missing letters, return to undefined -         returnundefined; to     } +  - } the  *Fearnotletter ("ABCE");

FCC Intermediate algorithm to find missing letters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.