Skip to main content

Posts

Showing posts from 2022

诡异的房子,里面比外面多出一米五的空间,女儿开门出来却消失了

the man blackened by death

动物们的迷惑行为,你能忍住不笑吗?

人类们的尴尬瞬间,你能忍住不笑吗?

大内密探零零发中的金像奖戏份

About the Little Lemon receipt maker exercise

 My homework and exercise of the lesson "About the Little Lemon receipt maker exercise" of the class "programming with Javascript" on coursera. const menu = [     {         Dish : "Italian pasta" ,         price : 9.55     },     {         Dish : "Rice with veggies" ,         price : 8.65     },     {         Dish : "Chicken with potatoes" ,         price : 15.55     },     {         Dish : "Vegetarian Pizza" ,         price : 6.45     } ]; function receiptMaker ( arr , bool ) {     if ( bool == false )     {         console . log ( "Prices without tax:" );         arr . forEach ( element => {             console . log ( `Dish: ${ element . Dish } Price (incl.tax):$ ${ element . price } ` );                     });     }     else     {         console . log ( "Prices with 20% tax:" );         arr . forEach ( element => {             console . log ( `Dish: ${ element . Dish } Price (inc

Write a React Component from Scratch

  // Change code below this line class   MyComponent   extends   React . Component  {    constructor ( props ) {      super ( props );   }    render () {      return  (        // The JSX code you put here is what your component will render       < div >< h1 > My   First   React   Component !</ h1 ></ div >     );   } }; ReactDOM . render (< MyComponent  />, document . getElementById ( 'challenge-node' ));

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

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

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

javascript to reverse a string

  function   reverseString ( str ) {    var   arr  =  str . split ( "" );    console . log ( arr );    arr . reverse ();    console . log ( arr );    var   newstr  =  arr . join ( "" );    console . log ( newstr )    return   newstr ; } reverseString ( "hello" );

Object.keys(obj)返回一个对象的属性数组

  let   users  = {    Alan : {      age :  27 ,      online :  false   },    Jeff : {      age :  32 ,      online :  true   },    Sarah : {      age :  48 ,      online :  false   },    Ryan : {      age :  19 ,      online :  true   } }; function   getArrayOfUsers ( obj ) {    // Only change code below this line    return   Object . keys ( obj );    // Only change code above this line } console . log ( getArrayOfUsers ( users )); [ 'Alan', 'Jeff', 'Sarah', 'Ryan' ]

使用正则表达式提取出hello world

  let   hello  =  "   Hello, World!  " ; let   wsRegex  =  /\w+,\s\w+!/ ;  // Change this line let   result  =  hello . match ( wsRegex )[ 0 ];  // Change this line console . log ( result ); // running tests // tests completed // console output Hello, World! Hello, World!

正则表达式匹配空格\s和特定次数

  let   ohStr  =  "Ohhh no" ; let   ohRegex  =  /Oh{3,6}\sno/ ig ;  let   result  =  ohRegex . test ( ohStr ); {3,6}表示匹配3到6次,包含3,6. {3, )表示最少3次,无上限 { ,8}表示最多8次,无下限 {3}匹配特定次数,这里表示只匹配3次的。 后面跟一个?,表示这个字母可能会出现,也可能不出现。如/colou?r/既能匹配英式英语的colour,也能匹配美式英语的color 在 pwRegex 中使用前瞻来匹配长度大于 5 个字符且具有两个连续数字的密码。 let   sampleWord  =  "astronaut" ; let   pwRegex  =  /(?=\w{6})(?=\w*\d{2})/ gi ;  let   result  =  pwRegex . test ( sampleWord );

正则表达式对username进行检查

 互联网上到处都在使用用户名。 它们为用户在他们最喜欢的网站上提供了独特的身份。 您需要检查数据库中的所有用户名。 以下是用户在创建用户名时必须遵循的一些简单规则。 1. 用户名只能使用字母数字字符。 2. 用户名中唯一的数字必须在末尾。 最后可以有零个或多个。 用户名不能以数字开头。 3. 用户名字母可以是小写和大写。 4. 用户名必须至少有两个字符长。 两个字符的用户名只能使用字母作为字符。 let username = "JackOfAllTrades" ; const userCheck = /^[a-z]([0-9]{2,}|[a-z]+\d*)$/i ; let result = userCheck.test(username);

匹配除字母和数字之外的字符\W

 您已经了解到可以使用快捷方式来匹配字母数字 [A-Za-z0-9_],使用 \w。 您可能想要搜索的自然模式与字母数字相反。 您可以使用 \W 搜索 \w 的反义词。 请注意,相反的模式使用大写字母。 此快捷方式与 [^A-Za-z0-9_] 相同。 let shortHand = / \W / ; let numbers = "42%" ; let sentence = "Coding!" ; numbers . match ( shortHand ) ; sentence . match ( shortHand ) ; 第一个匹配调用将返回值 ["%"],第二个将返回 ["!"]。