//Generate random colors
const Color = () => `#${
Math.floor(Math.random() * 0xffffff).toString(16)}`
console.log(Color ())
//Copy text to clipboard
const copyToClipboard =
(text) => navigator.clipboard && navigator.clipboard.writeText && navigator.clipboard.writeText(text)
copyToClipboard("Hello World!")
// Get url parameters
const getParamUrl = (key) => {
const url = new URL(location.href)
return url.searchParams.get(key)
}
//Scroll to bottom
const scrollToBottom = (element) =>
element.scrollIntoView({
behavior: "smooth", block: "end" });
//Scroll to top
const scrollToTop = (element) =>
element.scrollIntoView({
behavior: "smooth", block: "start" });
// The array is randomly sorted, and the music player will use the shuffling algorithm to play randomly.
// sort重排序
const shuffle = (arr) => arr.sort(() => Math.random() - 0.5)
const arr = [1, 2, 3, 4, 5]
console.log(shuffle(arr))
// 洗牌算法重排序
function shuffle2(array){
let result = [], random;
while(array.length>0){
random = Math.floor(Math.random()*array.length);
result.push(array[random]);
array.splice(random, 1);
}
return result;
}
console.log(shuffle2(arr))
function shuttle3(array) {
for (i = array.length - 1; i >= 0; i--) {
let random = Math.floor(Math.random() * (i))
let ele= array[random]
array[random] = array[i]
array[i] = ele
}
return array
}