Skip to main content

Posts

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

javascript Falsy Bouncer

 Remove all falsy values from an array. Falsy values in JavaScript are false, null, 0, "", undefined, and NaN. function   bouncer ( arr ) {    var   temarr  =[];    for  ( let   i = 0 ; i < arr . length ; i ++)   {      if  ( Boolean ( arr [ i ])== true )     {        temarr . push ( arr [ i ]);     }   }    console . log ( temarr );    return   temarr ; } bouncer ([ 7 ,  "ate" ,  "" ,  false ,  9 ]);

javascrip Copy each element of the first array into the second array with second array unchanged

  You are given two arrays and an index. Copy each element of the first array into the second array, in order. Begin inserting elements at index  n  of the second array. Return the resulting array. The input arrays should remain the same after the function runs. 给你两个数组和一个索引。 按顺序将第一个数组的每个元素复制到第二个数组中。 开始在第二个数组的索引 n 处插入元素。 返回结果数组。 函数运行后,输入数组应保持不变。 这个题目的难点在于输入数组不能改变,所以需要创建第三个数组,把输入数组深度拷贝过去。 function   frankenSplice ( arr1 ,  arr2 ,  n ) {    var   newarr  = [];    for  ( let   j  =  0 ; j < arr2 . length ; j ++)   {      newarr . push ( arr2 [ j ]);   }    for  ( let   i = arr1 . length - 1 ; i >= 0 ; i --)   {      newarr . splice ( n , 0 , arr1 [ i ]);   }    console . log ( newarr );    return   newarr ; } frankenSplice ([ 1 ,  2 ,  3 ], [ 4 ,  5 ,  6 ],  1 );

JavaScript capitalize first letter of words in a sentence

  function   titleCase ( str ) {    var   newarr  =  str . split ( " " );    console . log ( newarr );    for  ( let   i = 0 ; i < newarr . length ; i ++)   {      var   firstChar  =  newarr [ i ]. charAt ( 0 );      var   upper  =  firstChar . toUpperCase ();      var   newWords  =  upper + newarr [ i ]. slice ( 1 ). toLowerCase ();      newarr [ i ] =  newWords ;   }    console . log ( newarr );    str  =  newarr . join ( " " );    console . log ( str );    return   str ; } titleCase ( "I'm a little tea pot" );

JavaScript Repeat a String

 题目要求: Repeat a given string str (first argument) for num times (second argument). Return an empty string if num is not a positive number. For the purpose of this challenge, do not use the built-in .repeat() method. 将给定的字符串 str (第一个参数)重复 num 次(第二个参数)。 如果 num 不是正数,则返回一个空字符串。 出于本次挑战的目的,请勿使用内置的 .repeat() 方法。不允许使用内置的.repeat。 原来思路是用string*num,结果发现JavaScript没有字符串的乘法方法。可以用for循环。 function   repeatStringNumTimes ( str ,  num ) {    var   temString  = "" ;    if  ( num <= 0 )   {      temString  =  "" ;   }    else   {      for ( let   i = 0 ; i < num ; i ++)     {        temString += str ;     }   }    return   temString ; } console . log ( repeatStringNumTimes ( "abc" ,  3 )); repeatStringNumTimes ( "abc" ,  3 );

Return Largest Numbers in Arrays

  Return an array consisting of the largest number from each provided sub-array. 题目要求:返回两层嵌套数组中,每个小数组中的最大值,组成一个数组,返回这个数组。 思路:程序中创建一个空数组,然后在两层for循环中,空数组中的index就是外层数组的index。 function   largestOfFour ( arr ) {       let   largestArr  = []; //将每个子数组中的最大值push进这个数组    for  ( let   i = 0 ; i < arr . length ; i ++) //数组中的数组   {      largestArr [ i ] = arr [ i ][ 0 ];//初始化为每个小数组第一个值      for ( let   j = 0 ; j < arr [ i ]. length ; j ++) //对每个小数组进行循环判断     {        if  ( largestArr [ i ]<= arr [ i ][ j ])       {          largestArr [ i ] =  arr [ i ][ j ];                }     }   }    return   largestArr ; } console . log ( largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]])); largestOfFour ([[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]]);