// 防抖
function debounce(fn, wait) {
var timeout = null;
return function() {
if(timeout !== null) clearTimeout(timeout);
timeout = setTimeout(fn, wait);
}
}
// 节流throttle代码(定时器):
var throttle = function(func, delay) {
var timer = null;
return function() {
var context = this;
var args = arguments;
if (!timer) {
timer = setTimeout(function() {
func.apply(context, args);
timer = null;
}, delay);
}
}
}
颜色转换
function rgb2hex(sRGB) {
var reg=new RegExp(/^rgb\d1,3,\s∗\d1,3,\s∗\d1,3$/);
if(reg.test(sRGB)){
var num = sRGB.slice(4,-1).split(',');
var res = "#";
for(var i=0;i<num.length;i++){
if(num[i]>=0 && num[i]<=255){
num[i]=('0'+parseInt(num[i]).toString(16)).slice(-2);
res += num[i];
}else{
return sRGB;
}
}
return res;
}else{
return sRGB;
}
}
多维数组转一维数组
var arr = ['mu','zi',['dig',['big','love']]]
function flatten(arr){
var res = [];
for(var i=0;i<arr.length;i++){
if(Array.isArray(arr[i])){
res = res.concat(flatten(arr[i]));
}else{
res.push(arr[i]);
}
}
return res;
}
手写闭包
var test = (function(){
var a= 1;
return function(){
a++;
alert(a);
}
})()
test();//2
test();//3
for (var i= 0;i<3;i++){
document.body.addEventListener('click',(function(i){
return function(){
console.log(i)
}
})(i))
} 闭包的问题.这样可以打印出来012,不然是333
选择排序
直接插入排序
二分插入排序
- 0\ 在Object原型上扩展一个方法实现深度克隆
Extend a method on the Object prototype to achieve deep cloning - 1\ 实现两个字符串相加(大数字)
Implement two string additions (large numbers) - 2\ 冒泡排序
bubble sorting - 3\ 快速排序
Quick sort - 4\ 二叉树反转
binary tree reversal - 5\ [1,2,3]变成[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
高级版: 可以变成 [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1][3, 1, 2][3, 2, 1] - 6\ 去重
Remove duplicates - 7\ 字符串1中替换掉字符串2中的字符
Replace the characters in string 2 in string 1. - 8\ 反转字符串
reverse string - 9\ 字符串中返回数字和连续数字 a1b23c45=>[1,23,45]
Return numbers and consecutive numbers in a string - 10\ add(3)(4)(5)的调用方式 ,相加返回12
- 11\ 手写继承 原型链/es6
Handwriting inheritance - 12\ 去掉字符串的空格
Remove the space of the string - 13\ 二叉树遍历(前序,中序,后序,广度)
- 14\ 二分查找
Binary tree traversal (pre-order(Preorder traversal), in-order, post-order) - 15\ n的阶乘后有几个0
For an integer n find number of trailing zeroes in n! .
-16\ ** 十进制转二进制** - 十大排序算法 冒泡(两个for,第二个,jlength-i-1),选择排序(O(n²),里面的for,j=i+1),插入排序(Insertion Sort),希尔排序(Shell Sort,归并排序(Merge Sort),堆排序(Heap Sort),计数排序(Counting Sort),桶排序(Bucket Sort),基数排序(Radix Sort)
https://www.cnblogs.com/dushao/p/6004883.html
0\在Object原型上扩展一个方法实现深度克隆
1\实现两个字符串相加(大数字)
2\冒泡排序
3\快速排序
3\二叉树反转
5[1,2,3]变成[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
slice() 从数组中返回选定元素,这里slice()相当于新建一个数组.
高级版: 可以变成 [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1][3, 1, 2][3, 2, 1]
6\去重
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;
}
7\字符串1中替换掉字符串2中的字符
var str=“They are staudents”;
var str1=“aeiou”;
for(var i=0;i<str1.length;i++)
{
str=str.split(str1[i]).join('');
}
console.log(str);
8\反转字符串
9\字符串中返回数字和连续数字 a1b23c45=>[1,23,45]
如果只是转数字或者什么的 用正则就好a.replace(/[^\d+]/g,"")
或者a.replace(/[^0-9]/g,"")
10\add(3)(4)(5)的调用方式 ,相加返回12
temp.toString的重写只是为了函数不执行时能够返回最后运算的结果值,所以这个地方是可以任意修改的,你让它返回什么它就返回什么,
11\手写继承
- 原型链继承
2)构造函数继承 利用call,
但是不好是父函数原型链上的东西不能被继承(原型方法不能用!):
var fish = new Fish('小鱼'); fish.run()//undefiend
解决方案:在子类继承父类后,加上一句 :Fish.prototype = Object.create(Sea.prototype); Fish.prototype.constructor = Fish;
即可输出fish.run(),不直接用Fish.prototype = Sea.prototype;是因为会把Fish的constructor也变成Sea的.
3)ES6继承
12\去掉字符串前后所有空格:
代码如下:
function Trim(str)
{
return str.replace(/(^\s*)|(\s*$)/g, "");
}
说明:如果使用jQuery直接使用$.trim(str)方法即可,str表示要去掉前后所有空格的字符串。
去除全部空格:.replace(/\s/g,"")
13\二叉树遍历(前序,中序,后序,广度(一层一层),深度(先中后序))
前序:根-左-右
中序:左-根-右
后序: 右-左-根
14\ 二分查找
15 n的阶乘后有几个0
var num =0
for(var i=1;i<=inputData;i++){
let count = 0;
let j = i
while( j>0 && j%5 ==0){
j = j/5;
count++
}
num += count
}
process.stdout.write("" + num + "\n");
15 十进制转二进制
var arr = [];
var num = 44;
if(num == 1){
arr[0] = 1;
}else{
while(num != 0){
var tmp =num % 2;
arr.push(tmp);
num = (num - tmp) / 2;
}
}
console.log(arr.reverse().join(""));