Skip to main content

Posts

Palindrome Checker回文检查

 Return true if the given string is a palindrome. Otherwise, return false. A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing. Note: You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes. 主要思路是,把字符串转换为一个数组,然里利用数组的reverse()方法,得到一个新的数组,这个新的数组再合成字符串,和原来的字符串进行比较。 function   palindrome ( str ) {    var   pattern  =  /[^a-z0-9A-Z]/ gi ;    var   newstr  =  str . replace ( pattern , "" ). toLowerCase ();    console . log ( newstr );    var   newarr  =  newstr . split ( "" ). reverse ();    console . log ( newarr );    var   reversedStr  =  newarr . join ( "" );    console . log ( reversedStr );    if  ( reversedStr == newstr )   {      return   true ;   }     return   false ; } palindrome ( "eye" );

Sum All Numbers in a Range

 We'll pass you an array of two numbers. Return the sum of those two numbers plus the sum of all the numbers between them. The lowest number will not always come first. For example, sumAll([4,1]) should return 10 because sum of all the numbers between 1 and 4 (both inclusive) is 10. 解决的思路如下: function   sumAll ( arr ) {    var   big ;    var   small ;    var   sum  = 0 ;    if ( arr [ 0 ]>= arr [ 1 ])   {      big  =  arr [ 0 ];      small  =  arr [ 1 ];   }    else {      big  =  arr [ 1 ];      small  =  arr [ 0 ];   }    for  ( let   i  =  small ; i <= big ; i ++)   {      sum += i ;   }    return   sum ; } console . log ( sumAll ([ 1 ,  4 ])); sumAll ([ 1 ,  4 ]);

Use the split method to split str into an array of words

 Use the split method inside the splitify function to split str into an array of words. The function should return the array. Note that the words are not always separated by spaces, and the array should not contain punctuation. 使用 splitify 函数中的 split 方法将 str 拆分为单词数组。 该函数应返回数组。 请注意,单词并不总是用空格分隔,并且数组不应包含标点符号。 function   splitify ( str ) {    // Only change code below this line    var   pattern  =  /[\s,-.]/ gi ;    var   strarr  =  str . split ( pattern );    console . log ( strarr );    return   strarr ;    // Only change code above this line } splitify ( "Hello World,I-am code" );

javascript实现自己版本的filter

  // The global variable const   s  = [ 23 ,  65 ,  98 ,  5 ]; Array . prototype . myFilter  =  function ( callback ) {    // Only change code below this line    const   newArray  = [];    for  ( let   i = 0 ; i < this . length ; i ++)   {      if  ( callback ( this [ i ])){        newArray . push ( this [ i ]);     }        }    // Only change code above this line    return   newArray ; }; const   new_s  =  s . myFilter ( function ( item ) {    return   item  %  2  ===  1 ; }); console . log ( new_s );

javascript map() and filter()

  The variable  watchList  holds an array of objects with information on several movies. Use a combination of  filter  and  map  on  watchList  to assign a new array of objects with only  title  and  rating  keys. The new array should only include objects where  imdbRating  is greater than or equal to 8.0. Note that the  rating  values are saved as strings in the object and you may need to convert them into numbers to perform mathematical operations on them. // The global variable const   watchList  = [   {      "Title" :  "Inception" ,      "Year" :  "2010" ,      "Rated" :  "PG-13" ,      "Released" :  "16 Jul 2010" ,      "Runtime" :  "148 min" ,      "Genre" :  "Action, Adventure, Crime" ,      "Director" :  "Christopher Nolan" ,      "Writer" :  "Christopher Nolan" ,      "Actors" :  "Leonardo DiCaprio, Josep

javascript Mutations

 Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array. For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case. The arguments ["hello", "hey"] should return false because the string hello does not contain a y. Lastly, ["Alien", "line"], should return true because all of the letters in line are present in Alien. function   mutation ( arr ) {    var   temArr  =[];    for ( let   i = 0 ; i < arr [ 1 ]. length ; i ++) //要比较的是第二个数组的,所以放在外层,内层每次都要遍历第一个数组,让第二个数组中的字符去与每一个第一个数组中的字符比较,相等就push一个true,然后马上跳出内循环   {      for  ( let   j = 0 ; j < arr [ 0 ]. length ; j ++)     {        if  ( arr [ 1 ][ i ]. toUpperCase ()== arr [ 0 ][ j ]. toUpperCase ())       {          temArr . push ( true );          break ; //这个跳出很重要,如果继续循环,后来又比对成功,再push一个true就会导致我们的标志length不同       

Return the lowest index at which a value should be inserted into an array

  Return the lowest index at which a value (second argument) should be inserted into an array (first argument) once it has been sorted. The returned value should be a number. For example,  getIndexToIns([1,2,3,4], 1.5)  should return  1  because it is greater than  1  (index 0), but less than  2  (index 1). Likewise,  getIndexToIns([20,3,5], 19)  should return  2  because once the array has been sorted it will look like  [3,5,20]  and  19  is less than  20  (index 2) and greater than  5  (index 1). function   getIndexToIns ( arr ,  num ) {       arr . sort (( a , b )=>{ return   a - b });    if  ( num >= arr [ arr . length - 1 ]){      return   arr . length ;   }    else   if ( num <= arr [ 0 ]|| arr . length == 0 )   {      return   0 ;   }    else   {      for ( let   i = 0 ; i < arr . length ; i ++)   {      if ( num > arr [ i ]&& num <= arr [ i + 1 ])     {        return   i + 1 ;     }   }   }    } console . log ( getIndexToIns ([ 40 ,  60 ],  50 )); getI