123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- //Literals
- {
- var ford = {
- type: "hybrid",
- model: "fusion",
- power: 180,
- size: "4800/150/130",
- fuel: "petrol",
- };
- }
- //Literals expand
- var ford = {
- type: "hybrid",
- [prompt('Введите название ключа')]: [prompt('Введите параметр')].join(' '),
- power: 180,
- size: "4800/150/130",
- fuel: "petrol",
- };
- //Literals copy
- let property = prompt('Введите свойство')
- const obj = {...ford}
- obj.type = property
- console.log(obj)
- //Html tree
- let body = {
- tagName: 'body',
- children: [{
- tagName: 'div',
- children: [{
- tagName: 'span',
- children: ["Enter a data please:"]
- },
- {
- tagName: 'br',
- },
- {
- tagName: 'input',
- attrs: {
- children: {
- type:'text',
- id:'name',
- }
- },
- },
- {
- tagName: 'input',
- attrs: {
- children: {
- type:'text',
- id:'surname',
- }
- },
- }]
- },
- {
- tagName: 'div',
- children: [{
- tagName: 'button',
- attrs: {
- [attributes]: "ok",
- },
- children:['OK']
- },
- {
- tagName: 'button',
- attrs: {},
- }]
- }]
-
- }
- body.children[1].children[1].children=["Cancel"];
- body.children[1].children[1].attrs.id= "cancel";
- //Parent
- body.children[0].parent = body;
- body.children[1].parent = body;
- body.children[0].children[0].parent = body.children[0];
- body.children[0].children[1].parent = body.children[0];
- body.children[0].children[2].parent = body.children[0];
- body.children[0].children[3].parent = body.children[0];
- body.children[1].children[0].parent = body.children[1];
- body.children[1].children[1].parent = body.children[1];
- //destruct array
- {
- let arr = [1,2,3,4,5, "a", "b", "c"]
- let [odd1,even1,odd2,even2,odd3,...arr2]=arr
- console.log(odd1,even1,odd2,even2,odd3,arr2)//1 2 3 4 5 [ 'a', 'b', 'c' ]
- }
- //destruct string
- {
- let arr = [1, "abc"]
- let [number,arr2]=arr
- let [s1,s2,s3]=arr2
- console.log(number,s1,s2,s3)// 1 a b c
- }
- //destruct 2
- {
- let obj = {
- name: 'Ivan',
- surname: 'Petrov',
- children: [{name: 'Maria'}, {name: 'Nikolay'}]
- }
- let {children:[{name:name1},{name:name2}]}= obj
- console.log(name1,name2)//Maria Nikolay
- }
- //destruct 3
- {
- let arr = [1,2,3,4, 5,6,7,10]
- let {0:a,1:b,length}= arr
- console.log(a,b,length)//1 2 8
- }
- //Destructure и Change OK
- {
- let attributes = prompt('Введите название атрибута')// это к заданию Change OK
- let body = {
- tagName: 'body',
- children: [{
- tagName: 'div',
- children: [{
- tagName: 'span',
- children: ["Enter a data please:"]
- },
- {
- tagName: 'br',
- },
- {
- tagName: 'input',
- attrs: {
- children: {
- type:'text',
- id:'name',
- }
- },
- },
- {
- tagName: 'input',
- attrs: {
- type:'text',
- id:'surname',
- },
- }]
- },
- {
- tagName: 'div',
- children: [{
- tagName: 'button',
- attrs: {
- [attributes]: "ok",
- },
- children:['OK']
- },
- {
- tagName: 'button',
- children:["Cancel"],
- attrs: {
- id: "cancel"
- },
- }]
- }]
-
- }
- let {children:[{children:[{children:[textSpan]},{},{},{attrs:{id:textSurname}}]},{children:[{},{children:[textCancel]}]}]}=body//это к заданию Destructure
- console.log(textSpan,textCancel,textSurname)//Enter a data please: Cancel surname //это к заданию Destructure
- body.children[1].children[0].attrs.attributes= prompt('Введите значение атрибута');// это к заданию Change OK
- }
- //Copy delete
- {
- let ford = {
- type: "hybrid",
- model: "fusion",
- power: 180,
- size: "4800/150/130",
- fuel: "petrol",
- };
- let key = prompt('Введите ключ для удаления')
- const {[key]:x, ...obj} = ford
- console.log(obj)
- }
- //Currency real rate
- {
- fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- .then(data => {
- let currency = prompt('Введите исходную валюту').toUpperCase()
- let currency2 = prompt('валюту, в которую происходит конвертация').toUpperCase()
- let sum = +prompt('Введите сумму в '+currency2)
- let rates1 = data.rates[currency]
- let rates2 = data.rates[currency2]
- data = (rates1/rates2)*sum
- if (currency && currency2 && sum && data){
- alert('Результат '+data+' '+currency)}
- else{alert('Ошибка')}
- })
- }
- //Currency drop down
- {
- fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- .then(data => {
- let key = data.rates
- key = Object.keys(key)
- key = key.map(x=> "<option>"+x+"</option>")
- key = '<select>'+key+'</select>'
- console.log(document.write(key))
- })
- }
- //Currency table
- {
- fetch('https://open.er-api.com/v6/latest/USD').then(res => res.json())
- .then(data => {
- let dataObj = data.rates
- let arrValues = Object.values(dataObj)
- let arrKeys = Object.keys(dataObj)
- let arrData = []
- let i = 0
- for(let valuesEl of arrValues){
- arrData.push(arrValues.map(x=>(valuesEl/x).toFixed(3)))
- arrData[i].unshift(arrKeys[i])
- i++
- }
- arrKeys.unshift('')
- arrData.unshift(arrKeys)
- let currencyTable = "<table border='1'>"
- for (let arrEl of arrData){
- currencyTable += '<tr>'
- for (let cell of arrEl){
- currencyTable += '<td>'+cell+'</td>'
- }
- currencyTable+= '</tr>'
- }
- currencyTable+= "</table>"
- document.write(currencyTable)
- })
- }
- //Form
- {
- const car = {
- "Name":"chevrolet chevelle malibu",
- "Cylinders":8,
- "Displacement":307,
- "Horsepower":130,
- "Weight_in_lbs":3504,
- "Origin":"USA",
- "in_production": false
- }
- let keysArrCar = Object.keys(car)
- let valuesArrCar = Object.values(car)
- let i = 0
- let strType
- let formObjCar = '<form>\n'
- for (let a of keysArrCar){
- if (typeof valuesArrCar[i]=='string'){
- strType = 'text'
- }
- else if(typeof valuesArrCar[i]=='boolean'){
- strType = 'checkbox'
- }
- else{strType=typeof valuesArrCar[i]}
- formObjCar += ' <label>'+a+': <input type="'+strType+'" value="'+valuesArrCar[i]+'"/></label>\n'
- i++
- }
- formObjCar += '</form>'
- document.write(formObjCar)
- }
- //Table
- // На тестовых данных проверено
- {
- const persons = [
- {
- name: 'Мария',
- fatherName: 'Ивановна',
- surname: 'Иванова',
- sex: 'female'
- },
- {
- name: 'Николай',
- fatherName: 'Иванович',
- surname: 'Иванов',
- age: 15
- },
- {
- name: 'Петр',
- fatherName: 'Иванович',
- surname: 'Иванов',
- married: true
- },
- ]
- let i = 0
- let arrKeys = []
- for (let x of persons){
- arrKeys += Object.keys(persons[i])+','
- i++
- }
- arrKeys = arrKeys.split(',').slice(0,-1)
- const makeUniq = (arr) => {
- return arr.filter((el1, el2) => arr.indexOf(el1) === el2);
- }
- arrKeys = makeUniq(arrKeys)
- let arrValue = []
- for (let objPersons of persons){
- arrValue.push(arrKeys.map(el=>objPersons[el]===undefined?objPersons[el]=' ':objPersons[el]))
- }
- arrValue.unshift(arrKeys)
-
- let arrTable = "<table border='1'>"
- for (let arrEl of arrValue){
- arrTable += '<tr>'
- for (let cell of arrEl){
- arrTable += '<td>'+cell+'</td>'
- }
- arrTable+= '</tr>'
- }
- arrTable+= "</table>"
- document.write(arrTable)
- }
|