Skip to main content

Posts

Showing posts with the label javascript

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" );

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 );

javascript Return an array consisting of the largest number

 Return an array consisting of the largest number .  题目: 返回一个数组,该数组由每个提供的子数组中的最大数组成。  比如: arraysInArray = [[ 4 ,  5 ,  1 ,  3 ], [ 13 ,  27 ,  18 ,  26 ], [ 32 ,  35 ,  37 ,  39 ], [ 1000 ,  1001 ,  857 ,  1 ]] 我的思路是: 外圈一个大的for循环对大数组中的小数组遍历,内部一个for循环对每个小数组的每个元素进行遍历,用一个变量存储最大的值,用另一个变量记录最大的值所在的小数组在大数组 中的index,最后返回这个arr[index],也就是一个小数组。 function   largestOfFour ( arr ) {    let   largestNum  =  0 ; //存储最大数    let   index  =  0 ; //存储最大数所在的array的序号    for  ( let   i = 0 ; i < arr . length ; i ++) //数组中的数组   {      for ( let   j = 0 ; j < arr [ i ]. length ; j ++) //对每个小数组进行循环判断     {        if  ( largestNum <= arr [ i ][ j ])       {          largestNum  =  arr [ i ][ j ];          index  =  i ; //每次更新index为最大值所在的小数组的index       }     }   }    return   arr [ index ]; } 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 , 

javascript Find the Longest Word in a String

  Return the length of the longest word in the provided sentence. Your response should be a number. 思路:先把句子转化为单词数组,然后创建一个变量,并赋初值为0,让这个变量与每个单词的长度进行比较,如果这个变量小于等于这个单词长度,则将这个变量更新为单词的长度。在一个for循环中比较下去,就可以得到最长的单词的长度。 function   findLongestWordLength ( str ) {    var   strarr  =  str . split ( " " ); //用空格进行分割.    var   longest  =  0 ; //记录并更新每两个比较    console . log ( strarr );    for ( let   i = 0 ; i < strarr . length ; i ++){      if  ( longest <= strarr [ i ]. length )     {        longest  =  strarr [ i ]. length ;     }   }    console . log ( longest );    return   longest ; } findLongestWordLength ( "The quick brown fox jumped over the lazy dog" );