main.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* function a (text="called function alert") {
  2. alert(text);
  3. } */
  4. let a = (text="called function alert") => {alert(text)};
  5. let cube = (number=1) => {number**3};
  6. let avg = (a=0,b=0) => {(a+b)/2};
  7. let sum = (a=0,b=0,c=0) => {a+b+c};
  8. let intRnd =(min,max=0) => {
  9. if(max=0){
  10. return Math.floor(Math.random()*(min-max))+max;
  11. }
  12. return Math.floor(Math.random()*(max-min))+min;
  13. }
  14. let greetAll = (...names) => {
  15. let greetArr = names;
  16. let someGreet = []
  17. for(let i=0; i<greetArr.length; i++){
  18. someGreet.push(names[i])
  19. }
  20. alert(someGreet);
  21. }
  22. /* greetAll('Kyrka','Vetal','Spydik','Vadik'); */
  23. let sumArr = (...numarr) => {
  24. /* let arrOfSum = numarr;
  25. let sum=0;
  26. for(let i=0; i<arrOfSum.length;i++){
  27. sum=sum+arrOfSum[i];
  28. }
  29. alert(sum); */
  30. return alert(numarr.reduce((a,b) => a+b));
  31. }
  32. /* sumArr(...[10,20,30]); */
  33. var persons = [
  34. {name: "Иван", age: 17},
  35. {name: "Мария", age: 35},
  36. {name: "Алексей", age: 73},
  37. {name: "Яков", age: 12},
  38. ];
  39. function sortArray(array, property, direction=true) {
  40. if(direction === true){
  41. array.sort(function compare(a, b) {
  42. let result = (a[property] > b[property]) ? 1 :-1;
  43. return result;
  44. });
  45. return array;} else if(direction === false) {
  46. array.sort(function compare(a, b) {
  47. let result = (a[property] < b[property]) ? 1 :-1;
  48. return result;
  49. });
  50. return array;
  51. }
  52. }
  53. let sortedobj = sortArray(persons,"name");
  54. console.log(sortedobj);
  55. let mappedArray = ["1", {}, null, undefined, "500", 700].map((currentElement)=>{
  56. return typeof currentElement == 'string' ? Number(currentElement) : currentElement;
  57. });
  58. let reducedArray = ["0", 5, 3, "string", null].reduce((total,currentElement)=>{
  59. if (total == undefined){
  60. return total = +total;
  61. }
  62. if( typeof total === "number" && typeof currentElement === "number"){
  63. return total * currentElement;
  64. } else {
  65. return total = total *1;
  66. }
  67. },1);
  68. console.log(reducedArray);
  69. function filterdObj(){
  70. return Object.filter = (obj, predicate) =>
  71. Object.fromEntries(Object.entries(obj).filter(predicate));
  72. }
  73. let filter = filterdObj();
  74. var phone = {
  75. brand: "meizu",
  76. model: "m2",
  77. ram: 2,
  78. color: "black",
  79. };
  80. console.log(filter(phone, ([key,value])=> value == "meizu" || key == "color"));
  81. function sumrecurs(n) {
  82. if (n === 0){
  83. return 0;
  84. } else {
  85. return n + sumrecurs(--n)
  86. }
  87. }
  88. console.log(sumrecurs(5));
  89. /* let map = function (obj, fn, ctx) {
  90. return Object.keys(obj).reduce((a, b) => {
  91. a[b] = fn.call(ctx || null, b, obj[b]);
  92. return a;
  93. }, {});
  94. };
  95. const x = map({name: "Иван", age: 17},function(key,value){
  96. var result = {};
  97. result[key+"_"] = value + "$";
  98. return result;
  99. }); */
  100. let f = function(key,value) {
  101. var result = {};
  102. result[key+"_"] = value + "$";
  103. return result;
  104. }
  105. let map = function(obj,f) {
  106. let result = {};
  107. Object.entries(obj).forEach((item)=>{
  108. result = {...result,...f(item[0],item[1])};
  109. })
  110. return result;
  111. }
  112. let mappedObj
  113. /* console.log(x); */