Home  >  Article  >  Web Front-end  >  About JS time object and recursion issues

About JS time object and recursion issues

jacklove
jackloveOriginal
2018-05-21 11:30:241387browse

JS time objects and recursion are often encountered in learning, and this article will explain them.

What are the basic types? What are the complex types? What are the characteristics?
Generally speaking, the basic types of JS are mainly divided into 5 types, which refer to simple data segments stored in stack memory;

Number: number

Boolean values: true, false

String: string

null: Control pointer

underfined: There is a pointer but no value is assigned

Complex types only have object: including arrays, objects, and functions , Regular refers to the object saved in the heap memory. What is saved in the variable is actually just a pointer (coordinate). This pointer executes another location in the memory, and the object is saved at this location.

The output of the following code? Why?

var obj1 = {a:1, b:2};var obj2 = {a:1, b:2};console.log(obj1 == obj2); //Determine whether obj1 is equal to obj2console.log(obj1 = obj2); //Assign obj2 to obj1console.log(obj1 == obj2); //Determine again whether obj1 is equal to obj2 because of the characteristics of complex types. The two objects are at different positions, so the result is: falseObject {a: 1, b: 2}true

Code

Write a function getIntv to get the time from the current time to Specify the interval between dates

function getIntv(haha){
  a= Date.now();
  b= Date.parse(haha);
  c= b-a;
  d= Math.floor(c/(1000*60*60*24));
  e= Math.floor((c-d*1000*60*60*24)/(1000*60*60));
  f= Math.floor((c-d*1000*60*60*24-e*1000*60*60)/(1000*60));
  g= Math.floor((c-d*1000*60*60*24-e*1000*60*60-f*1000*60)/1000);  return ("距除夕还有"+d+"天"+e+"小时"+f+"分钟"+g+"秒")
}var str = getIntv("2017-01-27");console.log(str);

Change the digital date to a Chinese date

function getChsDate(lala){
  num=['零','一','二','三','四','五','六','七','八','九','十','十一','十二','十三','十四','十五','十六','十七','十八','十九','二十','二十一','二十二','二十三','二十四','二十五','二十六','二十七','二十八','二十九','三十','三十一']  var damo = lala.split('-')     
  var b = damo[0].split('')    
 for( var e =0 ;e< damo.length; e++) { 
  for(var i = 0 ;i< b.length ; i++) {  
    for(var f = 0 ;f< num.length ;f++) {   
      if( damo[e] == f.toString() ) {
        damo[e] = num[f]
      }      if( b[i] == f.toString() ) {
        b[i] = num[f]
      }
    }
  }
 }  
  return ( b.join("")+"年"+damo[1]+"月"+damo[2]+"日" )
}var str = getChsDate("2015-11-31")console.log(str)

Write a function to get the date n days ago

function getLastNDays(lala) {  var damo = new Date().getTime()-(lala*1000*60*60*24);  var d2 = new Date(damo)  var ad =(d2.getMonth()+1)  console.log(d2.getFullYear()+&#39;-&#39;+ad+&#39;-&#39;+d2.getDate())   
}
 getLastNDays(40);

Complete the following code to obtain execution The time is like:

var Runtime = (function(){    var a,b,c;    return {        start: function(){
             a = Date.now();   //获取现在的时间
             console.log(a);
        },        end: function(){
             b = Date.now();    //获取执行后的时间
             console.log(b);
        },        get: function(){ 
             c = b - a;              //两者相减得出执行代码的时间。            
             return (c+"毫秒");        //单位为毫秒
        }
    };
}());
Runtime.start();             
for(var i = 0; i<60000 ;i++ ){         
                                   //这里计算i循环60000次所需要的时间}
Runtime.end();console.log(Runtime.get());

There are 200 steps in the stairs. Each time you take 1 or 2 levels, how many ways are there in total from the bottom to the top? Use code (recursion) to implement

function fn(sum){  if(sum===1){    return 1;
  }  if(sum===2){    return 2;
  }  return fn(sum-1)+fn(sum-2)

}//The walking method of the stairs is equivalent to (the subsequent walking method after taking one step) (the subsequent walking method after taking two steps)//taking one step or two steps is Condition console.log(fn(200));                                                                                                             ’ to ’ s ’ to ’ s ’ s           ‐ ‐ ‐ ‐ ‐‐‐‐ ​ ​ ​ // If the value is too large, it will get stuck.

Write a deep copy method of json object. The json object can be nested in multiple layers, and the value can be a character. String, number, Boolean, any item in the json object

var obj={  sex:18,  vala:"hello",  arr:{    name:"damo"
  }
};function objcopy(obj){  var newobj={};  for(var i in obj){    //遍历对象obj
    if(typeof obj[i]=== "object"){       //如果对象的值为对象
      newobj[i] = objcopy(obj[i])    //将对象赋值为新对象
    }else{
      newobj[i]=obj[i];
    }
  }  return newobj;
}var newobj= objcopy(obj);console.log(newobj)                 //一个新的对象与原对象无关系

Write a deep copy method of the array. The value in the array can be a string, number, Boolean, or any item in the array

var arr=[18, "hello", [2,3,5,6,9],true,];function objcopy(arr){  var newarr=[];  for(var i in arr){    //遍历数组arr
    if( arr[i] instanceof Array){    //条件:如果对象的值为数组
      newarr[i] = objcopy(arr[i])    //将数组内容赋值为新数组
    }else{
      newarr[i]=arr[i];
    }
  }  return newarr;
}var newarr= objcopy(arr);console.log(newarr)                 //一个新的数组与原数组无关系

Write a deep copy method. The copied object and the internal nested value can be any item among strings, numbers, Boolean, arrays, and json objects

function   arrcopy(num){   var lala;  if(num instanceof Array){
    lala = [];
  } else{
    lala={};
  }
 for(var key in num){    if(num[key] instanceof Array){
       lala[key] = arrcopy(num[key]);
    }    else{
       lala[key] = num[key];
    }
  }  return lala;
}var lala= arrcopy(arr);

This article will do it

Related recommendations:

What is the difference between innerText and innerHTML of dom objects?

Some basic questions about JS

How to modularize require.js with front-end js

The above is the detailed content of About JS time object and recursion issues. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn