【前端】互联网公司2014前端笔试面试题JavaScript篇,待续


// 网上找的题目,自己做了下
 1 /**************************** 
 2  *1. 用js实现随机选取10–100之间的10个数字,存入一个数组,并排序
 3  ****************************/
 4 
 5 function f1(arr) {
 6     for (var i = 0; i < 10; i++) {
 7         var randomNum = Math.floor(Math.random() * 91 + 10); // 得到10~100之间的整数
 8         arr.push(randomNum);
 9     }
10     arr.sort(function (a, b) {return  a - b});
11     return arr;
12 }
13 // ======test======
14 var arr = [];
15 console.log(f1(arr));
16 
17 
18 
19 /****************************
20  * 2. 有这样一个URL:http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e,
21  * 请写一段JS程序提取URL中的各个GET参数(参数名和参数个数不确定),
22  * 将其按key-value形式返回到一个json结构中,
23  * 如{a:'1', b:'2', c:'', d:'xxx', e:undefined}。
24  ****************************/
25 
26 function f2(url) {
27     var json = {};
28     var regExp = /[\?\&](\w{1,})(={0,1})(\w{0,})/g;
29 
30     do {
31         arr = regExp.exec(url);
32         // console.log(arr); // arr = [完整的字符串, key, 等号或'', value或'']
33 
34         if (arr) {
35             //  arr[2] === ''时, value = undefined
36             //  arr[2] === '='时, value = arr[3]
37             var key = arr[1];
38             var value = undefined;
39 
40             if (arr[2] === '=')
41                 value = arr[3];
42 
43             json[key] = value;
44         }
45     } while (arr);
46 
47     return json;
48 }
49 // ======test======
50 var url = 'http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e';
51 console.log(f2(url));
52 
53 
54 
55 
56 /****************************
57  * 3. 我们要给每个log方法添加一个(app)前缀,
58  * 比如'hello world!'->'(app)hello world!'
59  ****************************/
60 
61 console.oldLog = console.log; // 保存原函数
62 
63 console.log = function (str) {
64     console.oldLog('(app)' + str);
65 };
66 
67 // ======test======
68 console.log('zhaokang');