123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- "use strict";
- //TODO: ES6
- // let a = msg => alert(msg);
- // let cube = number => number * number * number;
- // let avg2 = (a, b) => (a + b) / 2;
- // let sum3 = (a = 0, b = 0, c = 0) => a + b + c;
- // let intRandom = (a = 0, b = 0) => Math.round(Math.random() * (b - a)) + a;
- // let greetAll = (...arguments) => {
- // for (let i in arguments) {
- // alert(arguments[i])
- // }
- // }
- // let sum = (...arguments) => {
- // let sumAll = 0
- // for (let i in arguments) {
- // sumAll += arguments[i]
- // }
- // return sumAll;
- // }
- function sort() {
- let persons = [{
- name: "Иван",
- age: 17
- },
- {
- name: "Мария",
- age: 35
- },
- {
- name: "Алексей",
- age: 73
- },
- {
- name: "Яков",
- age: 12
- },
- ]
- function sort(field, boolean = true) {
- if (boolean) {
- return (a, b) => a[field] > b[field] ? 1 : -1
- } else {
- return (a, b) => a[field] < b[field] ? 1 : -1
- }
- }
- console.log(persons.sort(sort('age', false)))
- }
- // sort()
- function arrayMap() {
- let arr = ["1", {}, null, undefined, "500", 700];
- let newArr = arr.map(item => typeof (item) === "string" ? parseInt(item) : item);
- console.log(newArr)
- }
- // arrayMap()
- function arrayReduce() {
- let arr = ['0', 5, 3, "string", null];
- let result = arr.reduce((sum, currentItem) => {
- if (typeof (currentItem) === "number") {
- sum *= currentItem;
- }
- return sum
- }, 1)
- console.log(result);
- }
- // arrayReduce()
- function objectFilter() {
- let phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- function filter(obj, func) {
- for (let key in obj) {
- console.log(func)
- if (!func(key, obj[key])) {
- delete obj[key]
- }
- }
- }
- filter(phone, (key, value) => key == "color" || value == 2);
- console.log(phone)
- }
- // objectFilter()
- //OR
- // let newObj = Object.entries(phone)
- // .filter(item => item[0] === "color" || item[0] === "ram")
- // .map(item => item);
- // console.log(newObj)
- function objectMap() {
- function map(object, callback) {
- let obj = {};
- for (let key in object) {
- Object.assign(obj, callback(key, object[key]));
- }
- return obj
- }
- let obj = map({
- name: "Иван",
- age: 17
- }, function (key, value) {
- let result = {};
- result[key + "_"] = value + "$";
- return result;
- })
- console.log(obj);
- }
- // objectMap()
- function recursion() {
- let sum = (firstNumber, step, n) => {
- return n == 1 ? firstNumber : firstNumber + sum(firstNumber + step, step, n - 1);
- }
- }
- // recursion()
- //TODO: HTML Tree (in process...)
|