Problem Description:
You'll be given a number and you'll need to return it as a string in Expanded Form. For example:
expandedForm(12); // Should return ‘10 + 2‘expandedForm(42); // Should return ‘40 + 2‘expandedForm(70304); // Should return ‘70000 + 300 + 4‘
Note:all numbers'll be whole numbers greater than 0.
My answer:
1 functionexpandedform (num) {2 //Your Code here3 varJ=0;4 varStr=num.tostring (). Split ("");5 varresult=[];6 for(vari=str.length-1;i>=0;i--){7Str[i]=str[i]*math.pow (10, j);8J + +; 9 if(Str[i]) {Tenresult=Result.concat (Str[i]); One } A } - - returnResult.reverse (). Join (' + '); the}
Excellent answer:
1 Const Expandedform = n = n.tostring ()2 . Split ("")3 . Reverse ()4 . Map ((A, I) + A * MATH.POW (i))5 . Filter (a = a > 0 )6 . Reverse ()7 . Join ("+");
Summary: Excellent answers are the same as their own ideas, but good answers use the array of methods filter, map.
Filter,map,foreach,reduce will not change the original array.
Methods of arrays |
Role |
Traversal array foreach () |
Each element of an array executes a callback function once |
Filter () |
Detects a numeric element and returns an array of all elements that match the condition. |
Map () |
Processes each element of an array by specifying a function and returns the processed array. |
Reduce () |
Evaluates an array element to a value (left to right) |
1,foreach traversing an array
[].foreach ( value, index, array) {//value: traversed array contents, index: corresponding array index, array: arrays themselves
Code something
},thisvalue);
(1) Comparison with for:
for
is the basic syntax of the loop, can have for...in
, and foo...of
for(let i = 0; i < len; i++)
so on. for
can be used in loops continue
break
to control loops.
forEach
Can be used as for(let i = 0; i < len; i++)
shorthand, but cannot complete i + n
this cycle, but also does not support continue
and break
, only through return
to control the loop. In addition, it forEach
is not possible to exit the loop itself; foreach is good for sparse matrix processing and does not process an empty array.
(2) Comparison with map
Https://www.cnblogs.com/liuruyi/p/6483526.html
Common:
1. All loops iterate through each item in the array.
Each execution of the anonymous function in 2.forEach () and map () supports 3 parameters: the current item in the array item, the index of the current item, and the original array input.
3. This in the anonymous function refers to window.
4. Only the array can be traversed.
Different points:
ForEach () has no return value
Map () has a return value
2,map ()
[ ]. map(function(currentvalue,index,arr), thisvalue) Returns the new array in which the elements in the array call the function-processed values for the original array
3,reduce ()
[ ]. Reduce (function (total , currentvalue, Currentindex, arr), initialvalue); Returns a value
4,filter ()
[ ] . Filter (function ( currentvalue, Index, arr), thisvalue); Creates a new array of elements in the new array by examining all the elements in the specified array that meet the criteria.
Js--codewars--write number in Expanded form-filters, map, reduce, ForEach