// 1.原地反转字符串
function reverseString(str) {
return str.split(' ').reverse().join(' ').split('').reverse().join('')
}
//console.log(reverseString('I am the good boy'));
//2.判断是否回文
function isPalindrome(str) {
let left = 0;
let right = str.length - 1;
while (left < right) {
if (str.charAt(left) !== str.charAt(right))
return false;
left++
right--
}
return true
}
// console.log(isPalindrome("amaddam"))
// 3.格式化日期
const dateFormat = function(date, format) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate()
format = format.replace(/yyyy/, year);
format = format.replace(/mm/, month);
format = format.replace(/dd/, day);
return format
}
// console.log(dateFormat(new Date('2022-10-6'), 'yyyy/mm/dd'));
// 4.数组乱序
const disturbIndex = function(array) {
let len = array.length;
while (len) {
let randomIndex = Math.floor(Math.random() * len--);
let temp = array[len];
array[len] = array[randomIndex];
array[randomIndex] = temp;
}
return array;
}
// console.log(disturbIndex([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));
// 5.数组扁平化
function flatten(arr) {
return arr.reduce((prev, every) => {
prev = prev.concat(Array.isArray(every) ? flatten(every) : every);
return prev;
}, [])
}
// console.log(flatten([1, [2, [3, 4, 5]]]));
// 5.解析url
let url = 'http://www.domain.com/?user=anonymous&id=123&id=456&city=%E5%8C%97%E4%BA%AC&enabled';
var parseParam = function(url) {
let paramStr = /.+\?(.+)$/.exec(url)[1];
let paramArr = paramStr.split('&');
let paramObj = {};
paramArr.forEach((str) => {
if (/=/.test(str)) {
let [key, val] = str.split('=');
val = decodeURIComponent(val);
val = /^\d+$/.test(val) ? parseFloat(val) : val;
if (paramObj.hasOwnProperty(key)) {
paramObj[key] = [].concat(paramObj[key], val);
} else {
paramObj[key] = val;
}
} else {
paramObj[str] = true;
}
})
return paramObj;
}
// console.log(parseParam(url));
console