完成答题逻辑

This commit is contained in:
Andy Leong
2024-01-04 17:31:22 +08:00
parent 7905cb5fb7
commit 313af3437b
7 changed files with 452 additions and 44 deletions

View File

@@ -327,16 +327,48 @@ export function getAstro(month, day) {
return s.substr(month * 2 - (day < arr[month - 1] ? 2 : 0), 2);
}
// 引入BGM
export function createBGM() {
var bgmElement = document.createElement('div');
// 洗牌算法
export function FYShuffle(arr) {
let len = arr.length;
// document.querySelector('app').appendChild(bgmElement)
while (len > 1) {
let rand = Math.floor(Math.random() * len);
len--;
[arr[len], arr[rand]] = [arr[rand], arr[len]] // 采用的数组的结构赋值
}
// <div class="music_icon" @click="musicPlay">
// <audio style="display: none; height: 0" id="bg-music" autoplay="autoplay" preload="auto" :src="audioUrl"
// loop="loop"></audio>
// </div>
return arr;
}
// 选出数组中出现次数最多的值
export function mostValue(arr) {
// 创建一个空对象用于存储值及其出现的次数
let counter = {};
// 遍历数组中的每个元素
for (let i = 0; i < arr.length; i++) {
// 如果值已经在counter对象中增加其计数
// 如果值不在counter对象中设置计数为1
if (counter[arr[i]]) {
counter[arr[i]]++;
} else {
counter[arr[i]] = 1;
}
}
// 找出出现次数最多的值及其出现的次数
let mostFrequentValue = null;
let maxCount = 0;
for (let key in counter) {
if (counter[key] > maxCount) {
mostFrequentValue = key;
maxCount = counter[key];
}
}
// 返回出现次数最多的值和其出现的次数
return { value: mostFrequentValue, count: maxCount };
}