Skip to main content

Posts

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