main.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // //cube
  2. // let cube = n => n*n*n
  3. // alert(cube(10))
  4. // //avg2
  5. // let avg2 = (a,b) => (a+b)/2
  6. // alert (avg2 (10,7))
  7. // //sum3
  8. // let sum3 = (a,b,c) => {
  9. // a = a || 0;
  10. // b = b || 0;
  11. // c = c || 0;
  12. // var result = a+b+c
  13. // alert (result)
  14. // }
  15. // sum3(10,5,17)
  16. // sum3(10,5)
  17. // //intrandom
  18. // let intRandom = (low,high) =>Math.round(Math.random()*((high || 0) - (low ||0)) + (low || 0))
  19. // alert(intRandom(10,5))
  20. // //greetAll
  21. // let greetAll = (arguments) => {
  22. // let str = " "
  23. // for (let i = 0;i < arguments.length;i++){
  24. // str += arguments[i]
  25. // }
  26. // str.slice(0 , -1)
  27. // alert ("Hello" + "," + str)
  28. // }
  29. // greetAll("SuperMan")
  30. // //sum (//nerabotaet)
  31. // let sum = (arguments) =>{
  32. // var result = 0;
  33. // for (var i = 0;i <arguments.length; i++){
  34. // result += arguments[i]}
  35. // }
  36. // alert(sum(1,5,6,7,10))
  37. //sort
  38. let persons = [
  39. {name: "Иван", age: 17},
  40. {name: "Мария", age: 35},
  41. {name: "Алексей", age: 73},
  42. {name: "Яков", age: 12},
  43. ]
  44. function sort(field, boolean = true) {
  45. if (boolean) {
  46. return (a, b) => a[field] > b[field] ? 1 : -1
  47. }else {
  48. return (a, b) => a[field] < b[field] ? 1 : -1
  49. }
  50. }
  51. console.log(persons.sort(sort('age', false)))
  52. //arr.map
  53. let arr = ["1", {}, null, undefined, "500", 700];
  54. let newArr = arr.map(function(a){
  55. return typeof(a) === 'string' ? parseInt(a) : a;
  56. });
  57. console.log(arr);
  58. console.log(newArr);
  59. //arr.reduce
  60. var arr = ["0", 5, 3, "string", null]
  61. var result = arr.reduce(function(previousValue, currentValue){
  62. if(typeof(currentValue)==='number'){
  63. previousValue*=currentValue;
  64. };
  65. return previousValue
  66. }, 1)
  67. alert(result);
  68. //object filter
  69. var phone = {
  70. brand: "meizu",
  71. model: "m2",
  72. ram: 2,
  73. color: "black",
  74. };
  75. function filter(object, someF) {
  76. for(let key in object){
  77. if(!someF(key, object[key])) {
  78. delete object[key]
  79. }
  80. }
  81. }
  82. filter(phone,(key,value) => key == "color" || value == 2);
  83. console.log(phone)
  84. //object map
  85. let obj = map({name: "Иван", age: 17},function(key,value){
  86. var result = {};
  87. result[key+"_"] = value + "$";
  88. return result;
  89. })
  90. function map (object , callback){
  91. let obj = {}
  92. for(let key in object) {
  93. Object.assign(obj,callback(key, object[key]));
  94. }
  95. return obj
  96. }
  97. console.log(obj);
  98. //рекурсия (считает сумму до какого-то числа)
  99. function sumTo(n) {
  100. if (n == 1) return 1;
  101. return n + sumTo(n - 1);
  102. }
  103. alert( sumTo(10) );