//手写快速排序:
function quickSort(arr){
//递归的结束条件
if(arr.length<=1){
return arr;
}
//随机找一个数组中的值都可以,我们这里找中间位置的值
//Math.floor()实现一个非整数向下取整
var index=Math.floor(arr.length/2);
var middle=arr.splice(index,1)[0];
//建一个左数组,用于放比middle小的值
var left=[];
//建一个右数组,用于放比middle大或等的值
var right=[];
// 遍历参数数组,把数组中比middle小的值推进left
//把数组中比middle大的值推进right
arr.forEach(function(val){
if(val<middle){
left.push(val)
}else{
right.push(val)
}
});
// 将left middle right 组合起来
// 同时这是进行递归
return quickSort(left).concat(middle,quickSort(right));
}
var data = [50, 100, 10,20,30 ];
var i = quickSort(data);
console.log("data => " + data); // data => 50,100,20,30 //改变了原数组,把中间的元素切下来了
console.log("快速排序过后的数组 => " + i); //快速排序过后的数组 => 10,20,30,50,100
3.字符串API
concat
调用者是否改变:不会改变原字符串
返回值:返回一个合并后的字符串
参数:一个或多个字符串 .concat(str2, [, …strN])
功能:方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
语法:
var s1 ="你好";
var s2 = "世界";
var res =s1.concat(s2);
console.log(s1); //你好
console.log(res); //你好世界
var res =s1.concat(s2,"asdf",212);
console.log(res); //你好世界asdf212
var st = "Hello World. How are you doing?";
var newSt=st.split(" ");
console.log("st => "+ st); //st => Hello World. How are you doing?
console.log("newSt => "+ newSt); //newSt => Hello,World.,How,are,you,doing?
console.log(newSt); // ['Hello', 'World.', 'How', 'are', 'you', 'doing?']
var st = "张三,李四,赵五,孙六,钱七";
var newSt=st.split();
console.log(newSt);//['张三,李四,赵五,孙六,钱七'] 一个长度为1的数组
var newSt=st.split("");
console.log(newSt);//['张', '三', ',', '李', '四', ',', '赵', '五', ',', '孙', '六', ',', '钱', '七'] //每个字符都帮你切出来
var newSt=st.split(",",2);
console.log(st); //张三,李四,赵五,孙六,钱七
console.log(newSt);//['张三', '李四']
var re = "apples";
var str = "apples are round, and apples are juicy.";
var newstr = str.replace(re, "oranges");
console.log(newstr); // oranges are round, and apples are juicy.
console.log(str); //apples are round, and apples are juicy.
var re = "apples";
var str = "apples are round, and apples are juicy.";
// 给指定单词加引号
var newstr = str.replace(re, function(match) {
// match 是匹配到的子串。
return '"' + match + '"'
})
console.log(newstr); // "apples" are round, and apples are juicy.
功能:方法返回一个新字符串,新字符串所有满足 substr 的部分都已被 newSubstr 或 function 返回值替换。
语法:
var re = "apples";
var str = "apples are round, and apples are juicy.";
var newstr = str.replaceAll(re, "oranges");
console.log(newstr); //oranges are round, and oranges are juicy.
console.log(str); //apples are round, and apples are juicy.
var re = "apples";
var str = "apples are round, and apples are juicy.";
// 给指定单词加引号
var newstr = str.replaceAll(re, function(match) {
// match 是匹配到的子串。
return '"' + match + '"'
})
console.log(newstr); //"apples" are round, and "apples" are juicy.
toUpperCase
调用者是否会改变:不会改变原字符串
返回值 字符串
参数 无
功能:把字母字符转大写
语法:
var str = "AbCdsdfe";
var st=str.toUpperCase();
// 把字母字符转大写
console.log(st); //ABCDSDFE
console.log(str); //AbCdsdfe
toLowerCase
调用者是否会改变:不会改变原字符串
返回值 一个字符串
参数 无
功能:会将调用该方法的字符串值转为小写形式,并返回。
语法:
// 把字母字符转小写
var str1 = "AbCDDS";
var st1=str1.toLowerCase();
console.log(st1); //abcdds
console.log(str1); //AbCDDS
charAt
调用者是否会改变:不会改变原字符串
返回值 一个字符
参数 .charAt(index) index 一个介于0 和字符串长度减1之间的整数。默认值为0
功能:方法从一个字符串中返回指定的字符。
语法:
var anyString = "Brave new world";
console.log("The character at index 0 is '" + anyString.charAt(0) + "'"); // The character at index 0 is 'B'
console.log("The character at index 1 is '" + anyString.charAt(1) + "'"); // The character at index 1 is 'r'
console.log("The character at index 2 is '" + anyString.charAt(2) + "'"); // The character at index 2 is 'a'
console.log("The character at index 3 is '" + anyString.charAt(3) + "'"); // The character at index 3 is 'v'
console.log("The character at index 4 is '" + anyString.charAt(4) + "'"); // The character at index 4 is 'e'
console.log("The character at index 999 is '" + anyString.charAt(999) + "'"); // The character at index 999 is ''
console.log(anyString); //Brave new world
charCodeAt
调用者是否会改变:不会改变原字符串
返回值 一个数字 number类型
参数 .charCodeAt(index) index 一个大于等于 0,小于字符串长度的整数。如果不是一个数值,则默认为 0。
功能:指定 index 处字符的 UTF-16 代码单元值的一个数字(小于 65,536 的值);如果 index 超出范围,charCodeAt() 返回 NaN。
语法:
var str="ABC";
var box=str.charCodeAt(0);
console.log(str); //ABC
console.log(box); //65
console.log(typeof box); //number