42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
// 測試權重修復
|
|
console.log('🔄 測試權重修復...');
|
|
|
|
// 模擬可能出現的權重數據類型
|
|
const testCases = [
|
|
{ weight: 25.00 }, // 正常數字
|
|
{ weight: "25.00" }, // 字符串數字
|
|
{ weight: "25" }, // 字符串整數
|
|
{ weight: null }, // null 值
|
|
{ weight: undefined }, // undefined 值
|
|
{ weight: "" }, // 空字符串
|
|
{ weight: "abc" }, // 非數字字符串
|
|
];
|
|
|
|
console.log('\n測試各種權重數據類型:');
|
|
testCases.forEach((item, index) => {
|
|
const originalWeight = item.weight;
|
|
const safeWeight = Number(item.weight) || 0;
|
|
const formattedWeight = safeWeight.toFixed(1);
|
|
|
|
console.log(`${index + 1}. 原始: ${originalWeight} (${typeof originalWeight})`);
|
|
console.log(` 安全轉換: ${safeWeight} (${typeof safeWeight})`);
|
|
console.log(` 格式化: ${formattedWeight}%`);
|
|
console.log('');
|
|
});
|
|
|
|
// 測試總權重計算
|
|
console.log('測試總權重計算:');
|
|
const criteria = [
|
|
{ weight: 25.00 },
|
|
{ weight: "20.00" },
|
|
{ weight: 20 },
|
|
{ weight: "15.00" },
|
|
{ weight: null },
|
|
];
|
|
|
|
const totalWeight = criteria.reduce((sum, item) => sum + (Number(item.weight) || 0), 0);
|
|
console.log(`總權重: ${totalWeight} (${typeof totalWeight})`);
|
|
console.log(`格式化總權重: ${Number(totalWeight).toFixed(1)}%`);
|
|
|
|
console.log('\n🎉 權重修復測試完成!');
|