云游笔试

for(var i=0,j=1;i<5,j<10;i++,j++){console.log("等于"+i+","+"j等于"+j)}
VM8147:2 等于0,j等于1
VM8147:2 等于1,j等于2
VM8147:2 等于2,j等于3
VM8147:2 等于3,j等于4
VM8147:2 等于4,j等于5
VM8147:2 等于5,j等于6
VM8147:2 等于6,j等于7
VM8147:2 等于7,j等于8
VM8147:2 等于8,j等于9
var k = 0;
for(var i=0,j=1;i<5,j<7;i++,j++){k+=(i+j)}

36

2、

function f(n){if(n<=0)return 0;return n%2+f(n-1);}
undefined
f(5)
3

3、

var str = "hello world";
str.replace("l","k");
str.substring(1,6);
"ello "

var str = "hello world";
str.replace("l","k").substring(1,6);
"eklo "

4、

定义正则表达式
在js中定义正则表达式很简单,有两种方式,一种是通过构造函数,一种是通过//,也就是两个斜杠。

var   re =new RegExp("\\?(\\w{1,}=\\w{1,}&){1,}\\w{1,}=\\w{1,}");
var   re =/\?(\w{1,}=\w{1,}&){1,}\w{1,}=\w{1,}/;

可以和构造函数达到同样的效果,但仔细分析,发现,通过构造函数需要更多的转义字符\

关于验证的三个正则表达式方法
使用正则表达式的主要有字符串的方法match,正则表达式的方法exec,test
正则表达式方法test测试给定的字符串是否满足正则表达式,返回值是bool类型的,只有真和假,如果只是单纯的判断,不需要其他的处理,可以使用尤其是验证时。

正则表达式方法exec测试给定的字符串是否满足正则表达式,返回匹配到的字符串,如果没有匹配的则返回null,和test基本一致,如果需要获取匹配的各个子字符串,可以使用下标的方式

match其实是字符串的方法,但参数确是一个正则表达式

5、

console.log(document.getElementById(""))
null

console.log(document.getElementById)
function getElementById()

6、

var msg = "hello";
for(var i=0;i<10;i++){var msg = 'hello'+i*2+i;console.log(msg);}
VM19861:3 hello00
VM19861:3 hello21
VM19861:3 hello42
VM19861:3 hello63
VM19861:3 hello84
VM19861:3 hello105
VM19861:3 hello126
VM19861:3 hello147
VM19861:3 hello168
VM19861:3 hello189

7、

1)随机产生一个正偶数

2*Math.floor(Math.random()*5000)

2)在二维中,(0,0)中心,半径为R,在这个圆随机产生n个坐标

function num(n){
    var arr = [];
    for(var i=0;i<n;i++){
        arr[i] = Math.floor(Math.random()*10)+' '+Math.floor(Math.random()*10);
    }
    return arr;
}

 

posted @ 2016-10-21 10:58  chenxj  阅读(170)  评论(0)    收藏  举报