Freecodecamp's JavaScript basic algorithm challenge
Https://www.freecodecamp.com
2016-07-03
JavaScript is not very familiar with the knowledge to solve these problems, it is estimated that some algorithms will be very stupid.
1. Invert strings
Str.split (""). Reverse (). Join ("");
2. Factorial (factorial 0 results need to be 1)
function factorialize (num) { var n=1; for (var i=num;i>0;i--) { n*=i;} return N;}
I can't think of a way to add temporary variables without a moment.
3. Judging a palindrome string
The so-called palindrome is that the string reversal is the same as before the reversal, and ignores other characters except the alphanumeric and ignores the case.
Because I do not understand the regular expression, so spent a lot of time, it seems to take a good look at the regular line.
Str.match (/[A-ZA-Z0-9]/GI). Join (""). toLowerCase () ==str.match (/[a-za-z0-9]/gi). Reverse (). Join (""). toLowerCase ();
4. Find the longest word in a string
Str.split (""). Sort (function(A, b) {return b.length-a.length;}) [0];
5. Capitalize the first letter of each word in the string, with the other letters lowercase
Str.split (""). Map (function(ARG) {return arg.charat (0). toUpperCase () +arg.substr (1). toLowerCase ( );}). Join ("");
6. Let each sub-array of a two-dimensional integer array retain only the largest number
for (var i=0;i<arr.length;i++) { arr[i]=arr[i].sort (function(A, b) {return b-a;}). Shift ();}
7. Determine if the tail of the string is the same as the target string
Str.substr (-target.length) ==target;
8. Generate a repeating string at a given number of times
function repeatstringnumtimes (str, num) { var str= ""; while (num>0) { str+ =str; Num--; } return Str;}
Another question requires a temporary variable, and back again to see how it can be optimized.
9. Abbreviated string (the substring is truncated from the beginning of the string and "..." 3 pips generate a new string with a new string length equal to the specified length)
function truncatestring (str, num) { if(str.length>num) { if(num>3) { str=str.slice (0,num-3) + "..."; } Else { str=str.slice (0,num) + "..."; } } return str;}
To do this you feel that your coding and algorithms are really getting worse ...
10. Dwarf Monkey (What a ghost, it's so hard) split a one-dimensional array into two-dimensional arrays at a specified length
function chunkarrayingroups (arr, size) { var myarr=[]; for (var i=0;i<arr.length/size;i++) { Myarr[i]=arr.slice (i*size,i*size+size); } return Myarr;}
I feel this way, although I can achieve the effect, but always feel strange, should be easy to use arr return, I can only use temporary variable Myarr return.
11. Thriller (another name) The new array is intercepted from the specified position in the array, which looks like it is difficult to look at.
function Slasher (arr, howmany) { arr=arr.slice (howmany); return arr;}
Then I tried to use that sentence and then passed ... Passed and passed? What's the situation, shouldn't it be so easy? To the name of such a scary subject!
12. Mutation, a one-dimensional array has two string elements that determine whether the first element contains all the characters of the second element, ignoring the case.
function mutation (arr) { for (var i=0;i<arr[1].length;i++) { if(arr [0].tolowercase (). IndexOf (Arr[1].substr (i,1). toLowerCase ()) {returnfalse ; } } return true ;}
13. Delete all Flasy (false) values in the array
Arr.filter (function(val) {if(val) {return val;}});
14.
Rx Learning Note: Freecodecamp's JavaScript basic algorithm challenge