level-19

2016-03-31  本文已影响18人  lurker

基础类型有哪些?复杂类型有哪些?有什么特征?

基础类型变量有:

复杂类型有:

    var a=1;
    var b=a;
    a=3;
    console.log(a);//3
    console.log(b);//1
    var arr=[1,2,3,4];
    var arr2=arr;
    arr.push(1);
    console.log(arr.join(""));//12345
    console.log(arr2.join(""));//12345

写一个函数getIntv,获取从当前时间到指定日期的间隔时间

    function getIntv(arr){
        var a=Date.parse("2016-02-08");//除夕日期
        var b=Date.parse(arr);
        var day=Math.floor(((a-b)/(24*3600*1000)));
        var hour=Math.floor(((a-b)-(day*24*3600*1000))/(3600*1000));
        var min=Math.floor(((a-b)-(day*24*3600*1000)-(hour*3600*1000))/(1000*60));
        var sec=Math.floor(((a-b)-(day*24*3600*1000)-(hour*2600*1000)-(min*1000*60))/1000);

        var day1=Math.floor(((b-a)/(24*3600*1000)));
        var hour1=Math.floor(((b-a)-(day1*24*3600*1000))/(3600*1000));
        var min1=Math.floor(((b-a)-(day1*24*3600*1000)-(hour1*3600*1000))/(1000*60));
        var sec1=Math.floor(((b-a)-(day1*24*3600*1000)-(hour1*2600*1000)-(min1*1000*60))/1000);
        if(a-b>0){
            str="距除夕还有"+day+"天"+hour+"小时"+min+"分"+sec+"秒";
        return(str);
        }
        else{
            str="距除夕过去"+day1+"天"+hour1+"小时"+min1+"分"+sec1+"秒";
            return(str);
        }
    }

    var str = getIntv("2016-02-11");
console.log(str); 

把数字日期改成中文日期

        var str = getChsDate('2015-01-08');
    console.log(str);  // 二零一五年一月八日
    function  getChsDate(arr){
        var today=new Date(arr);
        var year=["零","一", "二","三","四","五","六","七","八","九"];
        var month=["一", "二","三","四","五","六","七","八","九","十","十一","十二"];
        var day=["一", "二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五","二十六","二十七","二十八","二十九","三十","三十一"];


        year1=((today.getFullYear())+"").split("");
        // console.log(year1);
        month1=((today.getMonth())+"");
        // console.log(month1);
        day1=((today.getDate()-1)+"");//索引计算不一样。减去一个。
        // console.log(day1);
        var year2=[];
        var month2=month[month1];
        var day2=day[day1];
        for(var i=0;i<year1.length;i++){
            year2[i]=year[year1[i]];
        }
        var year3=year2.join("");

        var str="输入日期为"+year3+"年"+month2+"月"+day2+"日";
        return str;
var lastWeek =  getLastNDays(7); // ‘2016-01-08’
var lastMonth = getLastNDays(30); //'2015-12-15'
    function getLastNDays(arr){
        var today=new Date();
        // console.log(today);
        today.setDate(today.getDate()-arr);
        // console.log(today);
        var year=today.getFullYear();
        var month=today.getMonth();
        var day=today.getDate();

        var content=(arr+"天前的日期是"+year+"年"+month+"月"+day+"日");
        console.log(content);
    }

完善如下代码,如:

  var Runtime = (function(){
//初始化类型
var startTime,endTime,getTime;
  return {
  start: function(){
    startTime=new Date();
    return startTime;

  },
  end: function(){
    endTime=new Date();
    return endTime;
  },
  get: function(){
    getTime=endTime-startTime;
    return ("程序运行时间为"+getTime+"毫秒");
  }
  };
  }());
  Runtime.start();
  for(var i=0;i<2000;i++){
    console.log(0);
  }
  Runtime.end();
  console.log(  Runtime.get() );

楼梯有200级,每次走1级或是2级,从底走到顶一共有多少种走法?用代码(递归)实现

    function a(num){
            if(num ==1){
                return 1;
            }
                else if (num==2){
                        return 2;
                }
                    else{
                        return a(num-2)+a(num-1);
                        }
        }

写一个json对象深拷贝的方法,json对象可以多层嵌套,值可以是字符串、数字、布尔、json对象中的任意项

    var json={
        "name":"yahoo",
        "age":"14",
        "sex":"man",
        "address":
    {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021"
    }
    }
        function JSON(arr){
            var newjson= {};
            for(key in arr){
                if(typeof arr[key]=="object"){
                    newjson[key]=JSON(arr[key]);
                }
                else{
                    newjson[key]=arr[key];
                }
            }
            return newjson;
            // console.log(newjson)
        }

写一个数组深拷贝的方法,数组里的值可以是字符串、数字、布尔、数组中的任意项目

    var arr=[
    1,
    "2",
    3,
    [1,2,3,4],
    true]
    function JSON(arr){
    var newarr=[];
    for(key in arr){
        if(typeof arr[key] =='Array') {
            newarr[key]=Json(arr[key]);
        }
        else{
            newarr[key]=arr[key];
        }
    }
    return newarr;
    }

写一个数组的深拷贝

    var arr=[
    1,
    "2",
    3,
    [1,2,3,4],
    true]
    function JSON(arr){
    var newarr=[];
    for(key in arr){
        if(arr[key] instanceof Array) {
            newarr[key]=Json(arr[key]);
        }
        else{
            newarr[key]=arr[key];
        }
    }
    return newarr;
    }

写一个深拷贝的方法,拷贝对象以及内部嵌套的值可以是字符串、数字、布尔、数组、json对象中的任意项.

    var arr={
        name:"yahoo",
        age:14,
        other:[1,2,true,"yahoo",3],
        man:{
            "man1":"woman",
            "man2":"man2"
            },
        aid:true
    }
    function JOSN(arr){
        var newarr={};
    for(key in arr){
        if (typeof arr[key] =='object'){
            newarr[key]=JOSN(arr[key]);
        }
        else{
            newarr[key]=arr[key];
        }   
        // return newarr;
    }

return newarr;
    }
上一篇下一篇

猜你喜欢

热点阅读