Skip to content

Instantly share code, notes, and snippets.

@lynzz
Last active December 10, 2015 17:29
Show Gist options
  • Save lynzz/4468280 to your computer and use it in GitHub Desktop.
Save lynzz/4468280 to your computer and use it in GitHub Desktop.
2010年网龙面试题
/**
* ==================================================================
* 要求:
* 1, 只能在指定的位置填写自己的代码,本文件里的其他代码不能修改
* 2, 所有题目都不允许添加全局变量名
* 3, 本文件应该能在firebug的console里正常执行,并输出结果
* 4, 代码的执行效率会作为评判的重要标准
* 5, 共3题
* ==================================================================
*/
/**
* ==================================================================
* 题1: 实现一个遍历数组或对象里所有成员的迭代器
* ==================================================================
*/
var each = function(obj, fn){
//+++++++++++答题区域+++++++++++
var toString = Object.prototype.toString;
var isArray = function (val) {
return toString.call(val) == '[object Array]';
};
if (isArray(obj)) {
for (var i = 0, len = obj.length; i < len; i++) {
if (fn.call(obj[i], i + 1) === false)
break;
}
}
else {
for (var i in obj){
if (fn.call(obj[i], obj[i], i) === false)
break;
}
}
//+++++++++++答题结束+++++++++++
};
try{
var data1 = [4,5,6,7,8,9,10,11,12];
var data2 = {
"a": 4,
"b": 5,
"c": 6
};
console.group(data1);
each(data1, function(o){
if( 6 == this )
return true;
else if( 8 == this )
return false;
console.log(o + ": \"" + this + "\"");
});
console.groupEnd();
/*------[执行结果]------
1: "4"
2: "5"
4: "7"
------------------*/
console.group(data2);
each(data2, function(v, n){
if( 5 == this )
return true;
console.log(n + ": \"" + v + "\"");
});
console.groupEnd();
/*------[执行结果]------
a: "4"
c: "6"
------------------*/
}catch(e){
console.error("执行出错,错误信息: " + e);
}
/**
* ==================================================================
* 题2: 实现一个叫Man的类,包含attr, words, say三个方法
* ==================================================================
*/
var Man;
//+++++++++++答题区域+++++++++++
Man = function(options) {
if (!(this instanceof Man)) {
return new Man(options);
}
this.AllWords = [];
this.attrs = {
gender: '<用户未输入>'
};
this.attr(options);
};
Man.prototype = {
attr: function(attrName, attrValue) {
var self = this;
if (attrValue) {
this.attrs[attrName] = attrValue;
} else if (attrName === Object(attrName)) {
for ( key in attrName ) {
this.attrs[key] = attrName[key];
}
} else if (typeof attrName == 'string') {
return this.attrs[attrName];
}
},
words: function(str) {
var self = this;
self.AllWords.push(str);
},
say: function() {
var self = this;
var limit = self.attrs['words-limit'];
var emote = self.attrs['words-emote'];
var words = '';
for(var i = 0; i < limit; i++) {
words += self.AllWords[i];
}
return this.attr('fullname') + emote + ":" + words;
}
};
//+++++++++++答题结束+++++++++++
try{
var me = Man({ fullname: "小红" });
var she = new Man({ fullname: "小红" });
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性别是:" + me.attr("gender"));
console.groupEnd();
/*------[执行结果]------
我的名字是:小红
我的性别是:<用户未输入>
------------------*/
me.attr("fullname", "小明");
me.attr("gender", "男");
me.fullname = "废柴";
me.gender = "人妖";
she.attr("gender", "女");
console.group();
console.info("我的名字是:" + me.attr("fullname") + "\n我的性别是:" + me.attr("gender"));
console.groupEnd();
/*------[执行结果]------
我的名字是:小明
我的性别是:男
------------------*/
console.group();
console.info("我的名字是:" + she.attr("fullname") + "\n我的性别是:" + she.attr("gender"));
console.groupEnd();
/*------[执行结果]------
我的名字是:小红
我的性别是:女
------------------*/
me.attr({
"words-limit": 3,
"words-emote": "微笑"
});
me.words("我喜欢看视频。");
me.words("我们的办公室太漂亮了。");
me.words("视频里美女真多!");
me.words("我平时都看优酷!");
console.group();
console.log(me.say());
/*------[执行结果]------
小明微笑:"我喜欢看视频。我们的办公室太漂亮了。视频里美女真多!"
------------------*/
me.attr({
"words-limit": 2,
"words-emote": "喊"
});
console.log(me.say());
console.groupEnd();
/*------[执行结果]------
小明喊:"我喜欢看视频。我们的办公室太漂亮了。"
------------------*/
}catch(e){
console.error("执行出错,错误信息: " + e);
}
/**
* ==================================================================
* 题3: 实现一个URI解析方法,把url里#之后的参数解析成指定的数据结构
* ==================================================================
*/
function urlParser(s){
//+++++++++++答题区域+++++++++++
if (typeof s !== 'string' || s.indexOf('#') == -1) {
return;
}
var re = [], a, i, t, o={}, reg = /([\w]+)(\/?|=)([\w]+)/g;
a = (s.split('#')[1] || '').match( reg );
//console.log(a)
for(i=0; i<a.length; i++){
if( a[i].indexOf('=') == -1 ){
if( a[i].indexOf('/') == -1 ){
re.push(a[i]);
}else{
t = a[i].split('/');
re.push(t[0],t[1]);
}
}
else if( a[i].indexOf('=') >= 0 ){
t = a[i].split('=');
o[t[0]] = t[1];
}
}
re.push(o);
return re;
//+++++++++++答题结束+++++++++++
}
try{
var url1 = "http://www.abc.com/m/s/#page/2/?type=latest_videos&page_size=20";
var url2 = "http://www.abc.com/m/s/#type=latest_videos&page_size=20";
var url3 = "http://www.abc.com/m/s/#page?type=latest_videos&page_size=20";
console.group();
console.info( urlParser(url1) );
console.info( urlParser(url2) );
console.info( urlParser(url3) );
console.groupEnd();
/*------[执行结果]------
["page", "2", { "type": "latest_videos", "page_size": 20 }]
[{ "type": "latest_videos", "page_size": 20 }]
["page", { "type": "latest_videos", "page_size": 20 }]
------------------*/
}catch(e){
console.error("执行出错,错误信息: " + e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment