function commafy(num){
return num && num
.toString()
.replace(/(\d)(?=(\d{3})+\.)/g, function($1, $2){
return $2 + ',';
});
}
//方案一:
function sortRandom1(arr) {
let length = arr.length;
for(let i = 0 ; i < length ; i++) {
let rand = parseInt(Math.random()*length);
let temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
console.log(rand)
}
console.log(arr);
}
sortRandom1(arr);
//方案二:
arr.sort(() => {
return Math.random() - 0.5;
})
console.log(arr);
//方案三
function sortRandom2(arr) {
let midArray = [];
while (arr.length > 0 ) {
let randIndex = parseInt(Math.random() * arr.length);
midArray.push(arr[randIndex]);
arr.splice(randIndex,1);
}
}
sortRandom2(arr);
function require() {
let str = '0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM'
let length = str.length;
let requireStr = '';
for(let i = 0 ; i < 4 ; i++) {
requireStr += str[parseInt(Math.random() * length)];
}
return requireStr;
}
console.log(require())
function require() {
let str = '0123456789qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM'
let length = str.length;
let requireStr = '';
for(let i = 0 ; i < 4 ; i++) {
let random = parseInt(Math.random() * length);
if(requireStr.indexOf(str[random]) !== -1) {
continue;
}
requireStr += str[random];
}
return requireStr;
}
console.log(require())
let str = "adadfdfseffqdjhuserfefsefseetsdg";
let aimStr = "qdjhu";
let start = str.indexOf(aimStr);
console.log(start)
let end = start + aimStr.length;
console.log(end)
let result = str.substr(start,aimStr.length)
// let result = str.substring(start,end);
console.log(result)
思路:利用split()方法转化为数组,利用数组的reverse()方法反转数组,再利用join()方法转为字符串即可.
let str = "abcdef";
function reverse(str) {
let arr = str.split('')
let revArr = arr.reverse();
let result = revArr.join('');
return result;
}
console.log(reverse(str));
function huiwen(str) {
//先将字符串转化为数组
let arr = str.split('')
//利用数组反转
let revArr = arr.reverse();
//翻转后的数组转化为字符串
let result = revArr.join('');
if(result === str) {
console.log(str + '是回文字符串');
}else{
console.log(str + '不是回文字符串');
}
}
function isString(str) {
if(typeof str == 'String' || str.constructor == String) {
return true;
}else{
return false;
}
}
function fun(n) {
if(n === 1) {
return 1;
}
if(n === 2) {
return 2;
}
return fun(n-1) +fun(n-2)
}
Array.prototype.distinct = function(){
let result = new Array;
for(var i = 0 ; i < this.length ; i++) {
for(var j = i+1; j < this.length;j++) {
if(this[i] == this[j]) {
result.push(this.splice(j,1));
j--;
}
}
}
return result;
}
var C = function () { this.foo = 'bar'; this.baz = 'bim'; };
C.prototype.bop = 'bip';
let obj = new C();
function iterate(obj) {
let arr = [];
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
arr.push(`${key}:${obj[key]}`)
}
}
return arr;
}
console.log(iterate(obj));
let str = 'qwqabd';
function fun(str) {
let length = str.length;
for(let i =0 ; i < length ; i++) {
if(Number(str[i])) {
return true;
}
}
return false;
}
console.log(fun(str));
输入:3, 0.0001
输出:0.0003
//最后计算的精度是两数精度之和
function multiply(a, b) {
if (Math.floor(a) == a && Math.floor(b) == b) {
return a * b
} else {
let stra = a.toString();
let strb = b.toString();
let len1 =stra.split('.').length > 1 ? stra.toString().split(".")[1].length:0;
let len2 =strb.split('.').length > 1 ? strb.toString().split(".")[1].length:0;
return (a*b).toFixed(len1+len2)
}
}
//输入 [1, 2, 3, 4, 2], 2
//输出 [1, 3, 4]
let arr = [1, 2, 3, 4, 2];
function remove(arr, item) {
let newArr = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== item) {
newArr.push(arr[i])
}
}
return newArr;
}
console.log(remove(arr, 2));
//构造函数方式
function createModule(str1, str2) {
function obj() {
this.greeting = str1;
this.name = str2;
this.sayIt = function () {
return (`${this.greeting}+${this.name}`)
}
}
return new obj();
}
//工厂模式
function createModule(str1, str2) {
function createObj() {
let obj = new Object;
obj.greeting = str1;
obj.name = str2;
obj.sayIt = function () {
return (`${obj.greeting}+${obj.name}`)
}
return obj;
}
return createObj();
}
//组合模式
function createModule(str1, str2) {
function obj() {
this.greeting = str1;
this.name = str2;
}
obj.prototype.sayIt = function() {
return (`${this.greeting},${this.name}`)
}
return new obj();
}
在多数情况下,调用toString()方法不必传递参数.但是,在调用数值的 toString()
方法,可以传递一个参数,输出数值的基数.默认情况下, toSting()
方法以十进制格式返回数值的字符串表示.二通过传递基数,toString()
可以输出二进制,八进制,十六进制,乃至其他任意有效进制格式表示的字符串
var num = 10;
alert(num.toString()); //"10"
alert(num.toString(2)); //"1010"
alert(num.toString(8)); //"12"
alert(num.toString(10)); //"10"
alert(num.toString(16)); //"a"
function convertToBinary(num) {
let str = parseInt(num).toString(2);
let arr = str.split('');
while(arr.length < 8) {
arr.unshift(0);
}
return arr.join('')
// console.log(arr.join(''));
}