123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //sort
- let persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- {name: "Яков", age: 1132},
- {name: "Яков", age: 22},
- {name: "Яков", age: 142},
- ]
- function arrsort(arr, prop, incline=true) {
- if (arr.length < 2) {
- return arr
- }
- let mid = Math.floor(arr.length / 2)
- let equ = arr.filter(value => value[prop] === arr[mid][prop])
- let more = incline? arr.filter(value => value[prop] > arr[mid][prop]) :
- (arr.filter(value => value[prop] < arr[mid][prop])).reverse()
- let less = incline? arr.filter(value => value[prop] < arr[mid][prop]) :
- (arr.filter(value => value[prop] > arr[mid][prop])).reverse()
- return [...arrsort(less, prop, incline), ...equ, ...arrsort(more, prop, incline)]
- }
- console.log(arrsort(persons, 'age', false))
- //array map
- let mapped = ["1", {}, null, undefined, "500", 700].map((elem) => {
- return isNaN(elem) || typeof elem === 'object'? elem : +elem
- })
- console.log(mapped)
- //array reduce
- let reduced = ["0", 5, 3, "string", null, 2, 100].reduce((prev, curr)=> {
- let sum = typeof prev === 'number'? prev : 1
- let isPresent = false;
- (function() {
- isPresent = typeof prev === 'number' || typeof curr === 'number'? true : false
- if(typeof curr === 'number') {
- sum = sum * curr
- }
- })();
- return isPresent? sum : 0
- })
- console.log(reduced)
- //object filter
- let phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- function objectFilter(obj, f) {
- let filtered = {}
- for(let key in obj) {
- if(f(key, obj[key])) {
- filtered[key] = obj[key]
- }
- }
- return filtered
- }
- let filtered = objectFilter(phone, (key, value) => key === 'model' || value === 'black')
- console.log(filtered)
- //object map
- function objectMap(object, f) {
- let mapped = {}
- for(let key in object) {
- let temp = f(key, object[key])
- for(let prop in temp) {
- console.log('biba',prop, temp[prop])
- mapped[prop] = temp[prop]
- }
- }
- return mapped
- }
- console.log(objectMap({name: "Иван", age: 17}, function(key,value){
- var result = {};
- result[key+"_"] = value + "$";
- console.log('ras', result)
- return result;
- }))
- //sum
- function sum(initial, step, members) {
- var res = initial
- var memb = members
- if(memb === 0) {
- return res
- } else {
- return sum(res + step, step, memb-1)
- }
- }
- console.log('sum', sum(5, 3, 10))
- //HTML Tree
- // blue belt
- class Markup {
- constructor(tagName, [...nestedTags]=[], {...attrs}={}, text='') {
- this.tagName = tagName,
- this.attrs = attrs
- this.text = text
- this.nestedTags = nestedTags
- }
- }
- let md3 = new Markup(
- 'table',
- [
- new Markup(
- 'tr',
- [
- new Markup(
- 'td',
- undefined,
- {disabled:true},
- 'some text'
- ),
- new Markup(
- 'td',
- undefined,
- undefined,
- 'some text2'
- )
- ]
- ),
- new Markup(
- 'tr',
- [
- new Markup(
- 'td',
- [
- new Markup(
- 'div',
- [
- new Markup(
- 'p',
- undefined,
- {disabled:true},
- 'some textp'
- ),
- new Markup(
- 'p',
- undefined,
- {style:'font-weight:800;color:red'},
- 'some textp'
- )
- ]
- )
- ],
- {style:'background-color:purple;color:white;'},
- 'some text div'
- ),
- new Markup(
- 'td',
- undefined,
- {class:'someclass'},
- 'some text4'
- )
- ]
- )
- ],
- {border: 1, style:"color:'red';"}
- )
- function stringifyMdObject(Md_obj) {
- let str = ''
- for(let key in Md_obj) {
- if(key === 'tagName') {
- str += `<${Md_obj[key]}`
- if(Md_obj.attrs.length !== 0) {
- continue;
- } else {
- str += `>`
- }
- }
- if(key === 'nestedTags') {
- for(let i = 0; i < Md_obj[key].length; i++) {
- str += stringifyMdObject(Md_obj[key][i])
- }
- }
- if(key === 'attrs' && Md_obj[key].length !== 0) {
- for(let i in Md_obj.attrs) {
- str += ` ${i}="${Md_obj.attrs[i]}"`
- }
- str += `>`
- }
- }
- str += `${Md_obj.text}`
- str += `</${Md_obj.tagName}>`
- return str
- }
- document.write(stringifyMdObject(new Markup('h2',[new Markup('span',undefined,{style:'color:blue;'},'Blue ')],undefined,'belt')))
- document.write(stringifyMdObject(md3))
|