Freecodecamp Advanced algorithm address stamp here.
Freecodecamp Elementary and intermediate algorithm, basic to a thought can be completed, and advanced algorithm a little trouble, so I will own the solution to write clearly, if there are errors or better solution, welcome message.
Validate US Telephone Numbers
If the incoming string is a valid US phone number, it is returned true
.
In short, the rule of the U.S. number is that the country code (must be 1), then the 3,3,4 number combination, the first three numbers can be wrapped in parentheses. The other is to use spaces or "-" at intervals.
Because the input value is definitely a string, the rules are more, so consider using regular.
Paste the code First:
functionTelephonecheck (str) {//Good luck! varreg=/^ (1\s?)? \ (? \d{3}\)? (\s|-)? \d{3} (\s|-)? \d{4}/;//Regular Rules varIndex1=str.indexof ("("); varIndex2=str.indexof (")");//query to two parentheses if((index1!=-1 && index2!=-1) | | (Index1==-1 && index2==-1)) {//there are double brackets or no parentheses if(Index2!=index1 && index2-index1!=4) {//If there are double brackets, and there are 3 characters between the ordinals return false; } varStr2=str.replace (/[\ (\) \s-]/g, "");//replace parentheses and spaces with a "-" global null to facilitate statistical length if(str2.length==11 && str2.substr (0,1)!=1 ){ return false; } }Else{ return false; } returnreg.test (str); }telephonecheck ("27576227382");
When the first attempt to directly match the number of the time we found that no, because we can not match the double bracket, the regular rule there are some blind spots, these blind spots are the problem of double brackets, and then there is the length of the problem, for the characters beyond the length of we do not match the ability to verify, this requires that we use JS to make some
My approach is to first verify that there are double brackets, with or without both, and if there is only one, return false. Then at the same time there are or no double brackets inside the addition of two judgments, if there are double brackets, then the characters between two brackets must be three, otherwise return false, if it does return 3, then we do not have to make too much judgment, because the regular has been written. The next step is to remove all interfering elements by using replace, verifying that the length of the string has not exceeded 11, and that the first number is 1 when the length is 11 o'clock. Complete these to perfect the judgment, finally carries on the regular match to be possible.
Symmetric difference
Create a function that accepts two or more arrays and returns an array of equivalent differential (symmetric difference) ( △
or) of the given array ⊕
The input array may be multiple, and the requirements for the topic are processed in order 22. That is, we make a new array of the elements of the first two arrays, and then we work with the third array, and so on, and eventually we return an array. This pattern reminds us of the reduce method of the array, the first two processing a result, processing the result and the next processing, until finally a result is obtained.
So at the end of the main function, as long as you use reduce, the current problem is to resolve how to eliminate all the same elements between two arrays, and then return a new, well-ordered array. Because there may be duplicate elements in an array, if only two of the arrays are deleted the same, they may also be deleted cleanly.
My idea is this, since our goal is only value, but do not care about the quantity, so one can start on the two array to do a single deduplication, then two to delete the same element and then stitching sorting. I am here, stole a lazy, with or go to the heavy function, is equal to save a function.
functionsym (args) {vararrs=[]; for(varA of arguments) {Arrs.push (a); } varRes=arrs.reduce (function(A, b) {a=del (a); b=del (b);//arrays are processed separately varArr=A.concat (b); returnDel (arr,true);//after stitching into a large array, do one more processing }); returnRes;} functionDel (Arr,flag) {//sort and go heavy flag to true means delete clean, otherwise leave a varStart,end; Arr.sort (function(A, B) {//array from small to large sort returnA-b; }); for(vari=0;i<arr.length;i++){ if(Arr[i]==arr[i+1]) {//Duplicate DiscoveryStart= (start===undefined) I:start;//start is the starting position of the repetitionend=i+1;//End is the repeating ending position}Else{ if(End && End==i) {//If there is a repetition, end has a value, which is handled according to the flag array. if(flag) {Arr.splice (Start,end-start+1); I=i-(end-start+1); }Else{arr.splice (start,end-start); I=i-(end-start); } Start=undefined;//no repetition, start to restore } } } returnarr; }sym ([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]);
Not to be continued ...
Freecodecamp Advanced Algorithm (personal to)