123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- // ES6 ==============================================================================
- // a =======================================
- let a = (...arguments) => alert(arguments)
- // a("Привет")
- // a("Как дела?")
- // a("Как погода?")
- // cube =======================================
- let cube =(...arguments) => arguments**=3
- cube(17)
- // avg2 =======================================
- let avg2 = (...arguments) => (arguments[0]+arguments[1])/2
- // avg2(7,10)
- // sum3 =======================================
- let sum3 = (...arguments) => arguments[0]+(arguments[1] ? arguments[1] : 0)+(arguments[2]?arguments[2]:0)
- // sum3(1,2,3)
- // intRandom =======================================
- let intRandom = (...arguments) => (Math.round(Math.random() * ((arguments[1]?arguments[1]:0) - arguments[0] ) + arguments[0]))
- // intRandom (0,1)
- // greetAll =======================================
- let greetAll = (...arguments) => {
- for(let i=0; i<arguments.lenght; i++){
- arguments+=arguments[i]
- }
- alert('Hello' +' '+ arguments.join(', '))
- }
- // greetAll('Kate','Vlad','Marina','Sveta','Andrey','Cat','Superman')
- // sum =======================================
- let sum = (...arguments) =>{
- let result = 0
- for(let i=0; i<arguments.length; i++){
- result+=arguments[i]
- }
- return result
- }
- // sum(10,20,40,100)
- // Union =======================================
- aChoice = () => a("Привет")
- cubeChoice = () => cube(17)
- avg2Choice = () => avg2(7,10)
- sum3Choice = () => sum3(1,2,3)
- intRandomChoice = () => intRandom(0,1)
- greetAllchoice = () => greetAll('Kate','Vlad','Marina','Sveta','Andrey','Cat','Superman')
- sumChoice = () => sum(10,20,40,100)
- let taskChoice
- let taskChoiceFunc = () => {
- taskChoice= prompt('Введите название задания: \n a - cube - avg2 - sum3 - intRandom - greetAll - sum')
- switch(taskChoice?.toLowerCase()){
- case 'a': aChoice()
- break;
- case 'cube': console.log(cubeChoice())
- break;
- case 'avg2': console.log(avg2Choice())
- break;
- case 'sum3': console.log(sum3Choice())
- break;
- case 'intrandom': console.log(intRandomChoice())
- break;
- case 'greetall': greetAllchoice()
- break;
- case 'sum': console.log(sumChoice())
- }
- taskChoice?taskChoiceFunc():false
- }
- taskChoiceFunc()
- // Union declarative =======================================
- let taskChoiceTwo = prompt('Введите название задания')
- const functionObj = {
- 'a': aChoice,
- 'cube': cubeChoice,
- 'avg2': avg2Choice,
- 'sum3': sum3Choice,
- 'intrandom': intRandomChoice,
- 'greetall': greetAllchoice,
- 'sum': sumChoice
- }
- functionObj[taskChoiceTwo.toLowerCase()]()
- // sort ==============================================================================
- var persons = [
- {name: "Иван", age: 17},
- {name: "Мария", age: 35},
- {name: "Алексей", age: 73},
- {name: "Яков", age: 12},
- ]
- let sort = (arr,key,Boolean) =>
- arr.sort((a,b) =>
- Boolean? (a-b) || (a[key]<b[key] ? -1 : 1) : (b-a) || a[key]>b[key] ? -1 : 0
- )
- console.log(sort(persons,'name',true))
- console.log(sort(persons,'age',true))
- console.log(sort(persons,'name',false))
- console.log(sort(persons,'age',false))
- console.log(sort(persons,'name'))
- console.log(sort(persons,'age'))
- // array map ==============================================================================
- let arr = ["1", {}, null, undefined, "500", 700]
- let arrMap = arr.map((index) => parseInt(index)?Number(index):index)
- console.log(arrMap)
- // array reduce ==============================================================================
- let arr = ["0", 5, 3, "string", null]
- let arrReduce = arr.reduce((index1,index2) => typeof index2 ==='number' ? index1*index2 : index1,1)
- console.log(arrReduce)
- // object filter ==============================================================================
- var phone = {
- brand: "meizu",
- model: "m2",
- ram: 2,
- color: "black",
- };
- const filter =(obj,func) =>{
- for(let key in obj){
- !func(key,obj[key]) ? delete obj[key] : obj[key]
- }
- console.log(obj)
- }
- filter(phone,(key,value) => key == "brand" || value == 2)
- // object map ==============================================================================
- const map =(obj,func) => {
- let result={}
- for (let key in obj){
- func(key,obj[key]) ? obj : result[key+'_'] = obj[key]+'$'
- }
- console.log(result)
- }
- map({name: "Иван", age: 17},function(key,value){
- var result = {};
- result[key+"_"] = value + "$";
- return
- }) //должен вернуть {name_: "Иван$", age_: "17$"}
- // Sum ==============================================================================
- let result = 0
- const sum = (start,step,finish) => {
- if (finish===0){
- return console.log(result)
- }
- else{
- result+=start
- sum(start+step,step,finish-1)
- }
- }
- sum(7,5,15)
- // HTML Tree ==============================================================================
- var someTree = {
- tagName: "table", //html tag
- subTags: [ //вложенные тэги
- {
- tagName: "tr",
- subTags: [
- {
- tagName: "td",
- text: "some text",
- },
- {
- tagName: "td",
- text: "some text 2",
- }
- ]
- }
- ],
- attrs:
- {
- border: 1,
- },
- }
- let result = ''
- const HTMLtree = (obj) => {
- const openTag = '<' + obj.tagName + (obj.attrs? ' ' + Object.keys(obj.attrs) + ' = ' + Object.values(obj.attrs): '') + '>'
- const closeTag = '</' + obj.tagName + '>'
- const content = obj.text? obj.text: ''
- result += openTag + content
- if(typeof(obj.subTags) === 'object'){
- for (let value of obj.subTags){
- HTMLtree(value)
- }
- }
- result += closeTag
- return result
- }
- HTMLtree(someTree)
|