问题
1. 基础类型有哪些?复杂类型有哪些?有什么特征?
- 基本类型:( undefined、null、Boolean、Number、String )指的是,保存在栈内存中的简单数据段。
- 复杂类型:( Object、Array、Function、正则......) 指的是,保存在堆内存中的对象,变量中保存的实际上只是一个指针,这个指针指向内存的另一个位置,由该位置保存对象。
2. 如下代码输出?为什么?
var obj1 = { a:1, b:2 };
var obj2 = { a:1, b:2 };
console.log(obj1 == obj2); // 虽然 obj1 和 obj2 内容一样,但是地址不同,因此,不是同一个对象。
console.log(obj1 = obj2); // 赋值操作 (赋的值是,b 的地址)
console.log(obj1 == obj2); // 既然地址一样,也就是同一个对象了
代码
1. 写一个函数 getInv
,获取从当前时间到指定日期的间隔时间
var str = getIntv("2017-01-28");
console.log(str);
function getIntv(specifiedTime){
var perDay = 24 * 60 * 60 * 1000,
perHour = 60 * 60 * 1000,
perMinute = 60 * 1000;
var intervalsTime = Date.parse(specifiedTime) - Date.now();
var days = Math.floor(intervalsTime / perDay);
var hours = Math.floor((intervalsTime - days * perDay) / perHour);
var minutes = Math.floor((intervalsTime - days * perDay - hours * perHour) / perMinute);
var seconds = Math.floor((intervalsTime - days * perDay - hours * perHour - minutes * perMinute) / 1000);
var result = "距离2017年春节还有:" + days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒";
return result;
}
2. 把数字日期改为中文日期
var dateExample = convertDate('2015-01-08');
console.log(dateExample); // 二零一五年一月八日
function convertDate(dateString) {
var dateDictionary = {
"0": "零",
"1": "一",
"2": "二",
"3": "三",
"4": "四",
"5": "五",
"6": "六",
"7": "七",
"8": "八",
"9": "九",
"10": "十",
"11": "十一",
"12": "十二",
"13": "十三",
"14": "十四",
"15": "十五",
"16": "十六",
"17": "十七",
"18": "十八",
"19": "十九",
"20": "二十",
"21": "二十一",
"22": "二十二",
"23": "二十三",
"24": "二十四",
"25": "二十五",
"26": "二十六",
"27": "二十七",
"28": "二十八",
"29": "二十九",
"30": "三十",
"31": "三十一"
}
var dateArray = dateString.split('-'),
year = dateArray[0],
month = dateArray[1],
day = dateArray[2];
var yearConverted = dateDictionary[year[0]] + dateDictionary[year[1]] + dateDictionary[year[2]] + dateDictionary[year[3]] + "年",
monthConverted = dateDictionary[Number(month)] + "月",
dayConverted = dateDictionary[Number(day)] + "日";
return (yearConverted + monthConverted + dayConverted);
}
3. 写一个函数获取 n 天前的日期
var lastWeek = getLastNDays(7);
var lastMonth = getLastNDays(30);
function getLastNDays(Days) {
var intervalTimes = Days * 24 * 3600 * 1000,
currentTime = Date.now(),
fewDaysAgoTime = currentTime - intervalTimes;
var tempTime = new Date(fewDaysAgoTime),
fewDaysAgoString = tempTime.toLocaleDateString(); // XXXX/XX/XX
var tempTimeArray = fewDaysAgoString.split("/"),
result = tempTimeArray.join("-");
return result;
}
4. 完善如下代码,用于获取执行时间如:
var Runtime = (function(){
var obj = {
start: function(){
startTime = Date.now();
return startTime;
},
end: function(){
endTime = Date.now();
return endTime;
},
get: function(){
var intervalTime = (endTime - startTime) / 1000;
return (intervalTime + "秒");
}
};
return obj;
}());
Runtime.start();
// todo something
for(var i = 0; i < 1000; i++){
console.log("testing");
}
Runtime.end();
console.log( Runtime.get() );
5. 楼梯有 200 级,每次走 1 级或是 2级,从底走到顶一共有多少种走法?用代码(递归)实现
function resolveStairs(steps){
if(steps == 1){
return 1;
}else if(steps == 2){
return 2;
}else{
return resolveStairs(steps - 1) + resolveStairs(steps - 2);
}
}
console.log(resolveStairs(20));
6. 写一个 json 对象深拷贝的方法,json 对象可以多层嵌套,值可以是字符串、数字、布尔、json 对象中的任意项
function deepCopy(obj){
var tempObj = {};
for(var key in obj){
if(typeof(obj[key]) === 'object'){
tempObj[key] = deepCopy(obj[key]);
}else{
tempObj[key] = obj[key];
}
}
return tempObj;
}