|
| 1 | +/* |
| 2 | +Description: |
| 3 | +Training JS #30: |
| 4 | +methods of arrayObject---reduce() and reduceRight() |
| 5 | +
|
| 6 | +This lesson we learn two methods of array: reduce() and reduceRight(). The two method is approximate. It applies a function against an accumulator and each value of the array to reduce it to a single value. The only difference is: the order of reduce() is from left to right, but reduceRight() is from right to left. |
| 7 | +
|
| 8 | +For more information, please refer to: |
| 9 | +
|
| 10 | +Array.prototype.reduce() |
| 11 | +Array.prototype.reduceRight() |
| 12 | +Before looking at some examples, we need to understand that the callback function used for the reduce() can use up to 4 parameters: previousValue, currentValue, index and array. In general, we use only two parameters: previousValue and currentValue. index and array are optional, and we use it when necessary. |
| 13 | +
|
| 14 | +Here are some examples to help us understand the use of reduce() and reduceRight(): |
| 15 | +
|
| 16 | +The first is a classic example, we can use reduce() method to get the sum of the number array: |
| 17 | +
|
| 18 | +var arr=[1,2,3,4,5]; |
| 19 | +console.log(arr.reduce((prev,curr)=>prev+curr)); //output: 15 |
| 20 | +//My habit is to use a and b |
| 21 | +console.log(arr.reduce((a,b)=>a+b)); //output: 15 |
| 22 | +In addition to the callback function, reduce() also has an optional parameter: initialvalue. In the example above, the initialvalue is omitted. Then the initialvalue is the first element, and then start the cumulative operation from second element. |
| 23 | +
|
| 24 | +The following example uses the initialvalue: |
| 25 | +
|
| 26 | +var arr=[1,2,3,4,5]; |
| 27 | +console.log(arr.reduce((a,b)=>a+b,"")); //output: 12345 |
| 28 | +This example uses an empty string as the initialvalue, followed by these operation: |
| 29 | +
|
| 30 | +""+1-->"1","1"+2-->"12",......,"1234"+5-->"12345" |
| 31 | +As long as you have enough imagination, reduce() can do a lot of work. |
| 32 | +
|
| 33 | +Calculated average: |
| 34 | +
|
| 35 | +function average(arr){ |
| 36 | + return arr.reduce((a,b)=>a+b)/arr.length |
| 37 | +} |
| 38 | +console.log(average([1,2,3,4,5])); //output: 3 |
| 39 | +Count something |
| 40 | +
|
| 41 | +var arr=[1,2,3,4,5]; |
| 42 | +//count odd number: |
| 43 | +console.log(arr.reduce((a,b)=>a+(b%2?1:0),0)); //output: 3 |
| 44 | +//count even number: |
| 45 | +console.log(arr.reduce((a,b)=>a+(b%2?0:1),0)); //output: 2 |
| 46 | +The following is a complex example, which uses a callback function of the four parameters and the initialvalue: |
| 47 | +
|
| 48 | +var arr1=[1,2,3,4,5]; |
| 49 | +var arr2=[6,7,8,9,10]; |
| 50 | +var result=arr1.map(x=>x*x).reduce((a,b,i,ar)=>a.concat(ar[i],arr2[i]),[]) |
| 51 | +console.log(result); //output: [ 1, 6, 4, 7, 9, 8, 16, 9, 25, 10 ] |
| 52 | +In the example above, Arr1 uses the map method to turn each element into its square. |
| 53 | +
|
| 54 | +[1,2,3,4,5]--->[1,4,9,16,25] |
| 55 | +Then, using the reduce method. the two elements (ar[i] and arr2[i]) of the same index in turn are put into the initial value (an empty array). |
| 56 | +
|
| 57 | +[1,4,9,16,25] |
| 58 | + ---> [ 1, 6, 4, 7, 9, 8, 16, 9, 25, 10 ] |
| 59 | +[6,7,8,9,10] |
| 60 | +It should be noted that when reading the value of the first array, we used the ar[i] (fourth parameters of the callback function). This value is changed by the map() method([1,4,9,16,25]). If we used arr1[i] reading the value, will be the value of the original array([1,2,3,4,5]) |
| 61 | +
|
| 62 | +Ok, lesson is over. let's us do some task. |
| 63 | +
|
| 64 | +#Task |
| 65 | +Coding in function ```tailAndHead```. function accept 1 parameter ```arr```(a number array). |
| 66 | +
|
| 67 | +We use an example to explain this task: |
| 68 | +
|
| 69 | +You got an array ```[123,456,789,12,34,56,78]```. |
| 70 | +
|
| 71 | +First, from left to right, the tail of an element and the head of the next element are added together. The results are put into an array. like this: |
| 72 | +``` |
| 73 | +[123,456,789,1 2,3 4,5 6,78] |
| 74 | + 3+4 6+7 9+1 2+3 4+5 6+7 |
| 75 | + | | | | | | |
| 76 | + [7 ,13 ,10 , 5 , 9 ,13] |
| 77 | +``` |
| 78 | +And then, Calculate their product and return it(```7x13x10x5x9x13=532350```) |
| 79 | +
|
| 80 | +So, ```tailAndHead([123,456,789,12,34,56,78]) should return 532350 ``` |
| 81 | +
|
| 82 | +All elements of arr are positive integer. If a number is less than 10, its head and tail are the same. Please try to use ```reduce()``` solve this kata. |
| 83 | +
|
| 84 | +Some example: |
| 85 | +``` |
| 86 | +tailAndHead([1,2,3,4,5]) should return 945 |
| 87 | +tailAndHead([111,2345,66,78,900]) should return 7293 |
| 88 | +tailAndHead([35456,782,569,2454,875]) should return 12012 |
| 89 | +``` |
| 90 | +*/ |
| 91 | +function tailAndHead(arr){ |
| 92 | + let m=[]; |
| 93 | + for(let i=0;i<arr.length-1;i++) |
| 94 | + m.push(arr[i]%10+Number((arr[i+1]+'')[0])) |
| 95 | + return m.reduce((a,b)=>a*b) |
| 96 | +} |
0 commit comments