|
@@ -0,0 +1,100 @@
|
|
|
+//sort
|
|
|
+
|
|
|
+// var persons = [
|
|
|
+// { name: "Иван", age: 17 },
|
|
|
+// { name: "Мария", age: 35 },
|
|
|
+// { name: "Алексей", age: 73 },
|
|
|
+// { name: "Яков", age: 12 }
|
|
|
+// ];
|
|
|
+// function sort(persons, name, boolean) {
|
|
|
+// if (boolean === true || boolean == null) {
|
|
|
+// for (let i = 0; i < persons.length; i++) {
|
|
|
+// for (let k = 0; k < persons.length - 1; k++) {
|
|
|
+// if (persons[k][name] > persons[k + 1][name]) {
|
|
|
+// let c = persons[k];
|
|
|
+// persons[k] = persons[k + 1];
|
|
|
+// persons[k + 1] = c;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// for (let i = 0; i < persons.length; i++) {
|
|
|
+// for (let k = 0; k < persons.length - 1; k++) {
|
|
|
+// if (persons[k][name] < persons[k + 1][name]) {
|
|
|
+// let c = persons[k];
|
|
|
+// persons[k] = persons[k + 1];
|
|
|
+// persons[k + 1] = c;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+// sort(persons, "age");
|
|
|
+// console.log(persons);
|
|
|
+// sort(persons, "name",false);
|
|
|
+// console.log(persons);
|
|
|
+
|
|
|
+
|
|
|
+//array Map
|
|
|
+
|
|
|
+// arr = ["1", {}, null, undefined, "500", 700];
|
|
|
+// let result = arr.map(function(item,index,arr){
|
|
|
+// var numbers = parseInt(item);
|
|
|
+// return isNaN(numbers) ? item : numbers;
|
|
|
+// })
|
|
|
+
|
|
|
+//array reduce
|
|
|
+// arr = ["0", 5, 3, "string", null]
|
|
|
+// let result = arr.reduce(function(accumulator, currentValue){
|
|
|
+// if (typeof(currentValue) == 'number'){
|
|
|
+// accumulator *= currentValue
|
|
|
+// }
|
|
|
+// return accumulators;
|
|
|
+// },1)
|
|
|
+// alert(result)
|
|
|
+
|
|
|
+//object filter
|
|
|
+// var phone = {
|
|
|
+// brand: "meizu",
|
|
|
+// model: "m2",
|
|
|
+// ram: 2,
|
|
|
+// color: "black",
|
|
|
+// };
|
|
|
+
|
|
|
+// function filter(arr,f){
|
|
|
+// let result = []
|
|
|
+// for (let item in arr){
|
|
|
+// if(f(item,arr[item])){
|
|
|
+// result[item] = arr[item]
|
|
|
+// }
|
|
|
+// }
|
|
|
+// console.log(result)
|
|
|
+// }
|
|
|
+// filter(phone,(key,value) => key == "color" || value == 2);
|
|
|
+
|
|
|
+// object map
|
|
|
+
|
|
|
+// function map(obj,callback){
|
|
|
+// for(let key in obj){
|
|
|
+// var obj1 = callback(key,obj[key]);
|
|
|
+// delete obj[key]
|
|
|
+// for (key in obj1)
|
|
|
+// obj[key] = obj1[key]
|
|
|
+// return obj
|
|
|
+// }
|
|
|
+// }
|
|
|
+// let obj = map({name: "Иван", age: 17},function(key,value){
|
|
|
+// var result = {};
|
|
|
+// result[key+"_"] = value + "$";
|
|
|
+// return result;
|
|
|
+// })
|
|
|
+// console.log(obj)
|
|
|
+
|
|
|
+
|
|
|
+// Sum
|
|
|
+function sumRec(a){
|
|
|
+ if (a > 1){
|
|
|
+ return a + sumRec(a-1)}
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+ console.log(sumRec(3))
|