打印和编程

打印

promise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
console.log('a')
setTimeout(() =>{
console.log('b')
},0)
new Promise((resolve)=>{
console.log('c')
resolve()
}).then(()=>{
console.log('d')
})
new Promise((resolve)=>{
console.log('e')
resolve()
}).then(()=>{
console.log('f')
})
setTimeout(() =>{
console.log('g')
},0)
console.log('h')
//acehdfbg

this

1
2
3
4
5
6
7
8
9
10
var obj = {
func: function(){
console.log(this)
}
}
var func1 = obj.func
obj.func()
func1()
//{func: ƒ}func: ƒ ()arguments: nullcaller: nulllength: 0name: "func"prototype: {constructor: ƒ}__proto__: ƒ ()[[FunctionLocation]]: VM2010:2[[Scopes]]: Scopes[1]__proto__: Object
//Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, parent: Window, …}

代码题

匹配qq号

1
String regex = "[1-9][0-9]{4,14}"; // 第一位1-9之间的数字,第二位0-9之间的数字,数字范围4-14个之间  

在一段英文中找出出现频率最高的单词

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function counts(article){
article = article.trim().toUpperCase();
var array = article.match(/[A-z]+/g);
article = " "+array.join(" ")+" ";
var max = 0,word,num = 0,maxword="";
for(var i = 0; i < array.length; i++) {
word = new RegExp(" "+array[i]+" ",'g');
num = article.match(word).length;
if(num>max){
max=num;
maxword = array[i];
}
}
console.log(maxword+" "+max);
}
counts("Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day;");

数组去重

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 最简单数组去重法 
/*
* 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中
* IE8以下不支持数组的indexOf方法 *
* */
function uniq(array){
var temp = []; //一个新的临时数组
for(var i = 0; i < array.length; i++){
if(temp.indexOf(array[i]) == -1){
temp.push(array[i]);
}
}
return temp;
}
var aa = [1,2,2,4,9,6,7,5,2,3,5,6,5];
console.log(uniq(aa));

千位符分割

1
(1234567.8).toLocaleString() "1,234,567.8" 

字符串反转

1
string.split('').reverse().join('') 

img

字符串变数组

1
split('')

数组变字符串

1
join('')