js算法题
一、将字符串qin_chuang_qin_chuang转为qinChuangQinChuang;
let arr = str1.split("_");
let str = arr[0];
for (let i = 1; i < arr.length; i ++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].substring(1);
str += arr[i]
}
二、输出啥
var a = 10;
function add() {
var a = 0;
return function () {
a += 1;
console.log(a);
}
}
var a1 = add();
var a2 = add();
a1(); // 1
a2(); // 1
a1(); // 2
a2(); // 2
三、将金额转为千分位
let num = 12345678.9
let arr = num.toString().split(".");
var result="", index = 0, len = arr[0].length-1;
while(len >= 0) {
index % 3 === 0 && index !== 0 ? result += "," + arr[0][len] : result += arr[0][len];
len --;
index ++;
};
result = result.split("").reverse().join("") + "." + arr[1];
四、判断数组的方式
//通过原型链做判断 obj.__proto__ === Array.prototype;
//通过ES6的Array.isArray()做判断 Array.isArrray(obj);
//通过instanceof做判断 obj instanceof Array;
//通过Array.prototype.isPrototypeOf Array.prototype.isPrototypeOf(obj);
//通过Object.prototype.toString.call()做判断 Object.prototype.toString.call(obj).slice(8, -1) === "Array";
Talk is cheap,show me the code