123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Функции 2</title>
- </head>
- <body>
- <script>
- //---------sort
- /*let persons=[
- {name:'Ivan',age:17},
- {name:'Maria',age:35},
- {name:'Aleksei',age:73},
- {name:'Iykov',age:12}
- ];
- function sort(arr,field,bool=true){
- if(bool==false){
- arr.sort((a,b)=>a[field]>b[field]?-1:1);
- }
- if(bool==true){
- arr.sort((a,b)=>a[field]>b[field]?1:-1);
- }
- }
- sort(persons,'age');
- console.log(persons);*/
- /*so(persons,'name',false);
- console.log(persons);*/
- //---------array map (done)
- /*let arr=['1',{},null,undefined,'500',700];
- let arr2=arr.map(function(x){
- if(x===String(x)){
- return Number(x);
- }
- return x;
- })
- console.log(arr2);
- let arr2 = function mAp(arr){
- let arr2=[];
- for(let num of arr){
- if(num===String(num)){
- arr2.push(Number(num));
- }else{
- arr2.push(num);
- }
- }
- return arr2;
- }
- console.log(arr2(arr))*/
- //---------array reduce (done)
- /*let arr=['0',5,3,'string',null];
- let num=arr.reduce((res,num)=>{
- if(num===Number(num)){
- res*=num;
- }
- return res;
- },1);
- console.log(num); //15
- let num2=function(arr){
- let res=1;
- for(let i of arr){
- if(i===Number(i)){
- res*=i;
- }
- }
- return res;
- }
- console.log(num2(arr)); //15*/
- //----------object filter
- /*let phone={
- brand:'meizu',
- model:'m2',
- ram:2,
- color:'black'
- };
- function filter(obj,cb){
- return Object.entries(obj).filter(e => cb(...e)).reduce((a,[b, c] = b) => (a[b] = c, a),{});
- }
- console.log(filter(phone,(key,value)=>key=='color'|| value==2));*/
- //---------object map
- /*function map(obj,cb){
- let obj2 = {};
- for(var key in obj){
- Object.assign(obj2,cb(key, obj[key]));
- }
- return obj2;
- }
- console.log(map({name:"Ivan",age:17},function(key,value){
- let result={};
- result[key+'_']=value+'$';
- return result;
- }));*/
- //---------sum (done)
- /*function sum(n){
- if(n==1){
- return n;
- }else{
- return n+sum(n-1);
- }
- }
- console.log(sum(10));*/
-
- //----------HTML Tree (blue belt - recursive) (done)
- /*var someTree = {
- tagName: "table",
- subTags: [
- {
- tagName: "tr",
- subTags: [
- {
- tagName: "td",
- text: "some text",
- },
- {
- tagName: "td",
- text: "some text 2",
- }
- ]
- }
- ],
- attrs: {
- border: 1,
- },
- }
- let str='';
- function createTree(obj){
- for(let item in obj){
- if(item=='tagName'){
- if(obj[item]=='table'){
- let [key] = Object.keys(someTree['attrs']);
- let [value]= Object.values(someTree['attrs']);
- str+=`<${someTree['tagName']} ${key}=${value}>`;
- }else{
- str+=`<${obj['tagName']}>`;
- }
- }
- if(item=='subTags'){
- for(let item2 of obj[item]){
- createTree(item2);
- }
- }
- if(item=='text'){
- str+=`${obj['text']}`;
- }
- }
- }
- createTree(someTree);
- console.log(str);
- document.write(str);*/
-
- </script>
- </body>
- </html>
|