精易论坛
标题: 教一下 这个JS 如何写 更简便直接得出结果 [打印本页]
作者: 承易 时间: 2023-10-29 10:43
标题: 教一下 这个JS 如何写 更简便直接得出结果
var shishen=[ "比肩","劫财","食神","伤官","偏财","正财","七杀","正官","偏印","正印"] ; var tg=[ "甲","乙","丙","丁","戊","己","庚","辛","壬","癸"] ; var dz=[ "子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"] ; function qshishen ( rizhu,tiangan) { if ( rizhu.includes ( "甲") && tiangan.includes ( "甲") ){ return document.write ( shishen[ 0] + " ") //return dinren[ yuefen1] ;} else if ( rizhu.includes ( "甲") && tiangan.includes ( "乙") ){ return document.write ( shishen[ 1] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "丙") ){ return document.write ( shishen[ 2] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "丁") ){ return document.write ( shishen[ 3] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "戊") ){ return document.write ( shishen[ 4] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "己") ){ return document.write ( shishen[ 5] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "庚") ){ return document.write ( shishen[ 6] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "辛") ){ return document.write ( shishen[ 7] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "壬") ){ return document.write ( shishen[ 8] + " ") } else if ( rizhu.includes ( "甲") && tiangan.includes ( "癸") ){ return document.write ( shishen[ 9] + " ") } // 这样后面还有9个天干对比 这么写肯定不对,只是我不会写,如何用简单的代码 表达出来 else if ( rizhu.includes ( "乙") && tiangan.includes ( "甲") ){ return document.write ( shishen[ 1] + " ") } else{ return document.write ( "提交参数错误 ") } }
qushishen.zip
(1.28 KB, 下载次数: 2)
作者: 湘伦 时间: 2023-10-29 10:53
你的代码可以通过使用数组索引来简化,从而避免大量的 else if 语句。首先,你可以将天干和地支转换为数组索引,然后使用这些索引来访问 shishen 数组中的相应元素。以下是一个简化的版本:
[JavaScript] 纯文本查看 复制代码
var shishen = ["比肩", "劫财", "食神", "伤官", "偏财", "正财", "七杀", "正官", "偏印", "正印"];
var tg = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"];
function qshishen(rizhu, tiangan) {
var rizhuIndex = tg.indexOf(rizhu);
var tianganIndex = tg.indexOf(tiangan);
if (rizhuIndex === -1 || tianganIndex === -1) {
return document.write("提交参数错误<br>");
}
var shishenIndex = (rizhuIndex + tianganIndex) % 10; // 使用模运算确保索引在0-9之间
return document.write(shishen[shishenIndex] + "<br>");
}
这个代码首先查找 rizhu 和 tiangan 在 tg 数组中的索引。如果它们不在数组中(即索引为 -1),则输出错误消息。否则,它将两个索引相加,并使用模运算确保结果在 0 到 9 之间,然后用这个索引来从 shishen 数组中获取相应的元素。
由文心大模型3.5生成 请自测
作者: 时髦猪 时间: 2023-10-29 11:05
[JavaScript] 纯文本查看 复制代码
var shishen=["比肩","劫财","食神","伤官","偏财","正财","七杀","正官","偏印","正印"];
var tg=["甲","乙","丙","丁","戊","己","庚","辛","壬","癸"];
var dz=["子","丑","寅","卯","辰","巳","午","未","申","酉","戌","亥"];
function qshishen(rizhu,tiangan) {
if (rizhu.includes("甲") && tiangan.includes("甲") ||
rizhu.includes("乙") && tiangan.includes("乙") ||
rizhu.includes("丙") && tiangan.includes("丙") ||
rizhu.includes("丁") && tiangan.includes("丁") ||
rizhu.includes("戊") && tiangan.includes("戊") ||
rizhu.includes("己") && tiangan.includes("己") ||
rizhu.includes("庚") && tiangan.includes("庚") ||
rizhu.includes("辛") && tiangan.includes("辛") ||
rizhu.includes("壬") && tiangan.includes("壬") ||
rizhu.includes("癸") && tiangan.includes("癸")
){
return document.write(shishen[0]+ "<br>")
//return dinren[yuefen1];
}
else if (rizhu.includes("甲") && tiangan.includes("乙") ||
rizhu.includes("乙") && tiangan.includes("甲") ||
rizhu.includes("丙") && tiangan.includes("丁")
//后面按照这个格式写就行
){
return document.write(shishen[1]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("丙")){
return document.write(shishen[2]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("丁")){
return document.write(shishen[3]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("戊")){
return document.write(shishen[4]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("己")){
return document.write(shishen[5]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("庚")){
return document.write(shishen[6]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("辛")){
return document.write(shishen[7]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("壬")){
return document.write(shishen[8]+ "<br>")
}
else if (rizhu.includes("甲") && tiangan.includes("癸")){
return document.write(shishen[9]+ "<br>")
} // 这样后面还有9个天干对比 这么写肯定不对,只是我不会写,如何用简单的代码 表达出来
else if (rizhu.includes("乙") && tiangan.includes("甲")){
return document.write(shishen[1]+ "<br>")
}
else{
return document.write("提交参数错误<br>")
}
}
作者: 承易 时间: 2023-10-29 11:15
输入别的 得出结果 不对,麻烦 在帮忙改一下,
就是想要
输入 甲甲 得出 比肩
那么输入 壬甲 就得出 食神
那么 丁庚 就得出 正财
以此类推
不知 我表达清楚了嘛 ?
代码怎么在改一下呢
就是十个天干和十个天干的不同组合 每一个组合 有十个结果,
得出了左边那一栏目的 比肩 劫财 食神。。。。。。
作者: 承易 时间: 2023-10-29 14:07
在 湘伦 这个朋友回答时 说由文心大模型生成,得到启发,在用讯飞星火模型 一点点的问,终于解决了 我需要的答案,看来这个AI模型是未来的疑难杂症的重要帮手,深有感触,因为我不太懂JS代码,写了个简单的,放在AI模型去优化代码。又学到了更精简的代码,看来以后有问题 不是找啥搜索引擎,而是先问AI模型。首选讯飞星火。至此 这个帖子结束 7天后 申请解除回答。顺便把解决后的代码 贴上 留待以后有需要的人参考
var shishen = [ "比肩", "劫财", "食神", "伤官", "偏财", "正财", "七杀", "正官", "偏印", "正印"] ; var tg = [ "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"] ; function qshishen ( rizhu, tiangan) { var rizhuIndex = tg.indexOf ( rizhu) ; var tianganIndex = tg.indexOf ( tiangan) ; if ( rizhuIndex === -1 || tianganIndex === -1) { return document.write ( "提交参数错误 ") ; } const shishenIndex = `${ rizhuIndex} ${ tianganIndex} `;if ( [ //return document.write ( shishen[ 0] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 0] + " ") ;} else if ( [ //return document.write ( shishen[ 1] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 1] + " ") ;} else if ( [ //return document.write ( shishen[ 2] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 2] + " ") ;} else if ( [ //return document.write ( shishen[ 3] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 3] + " ") ;} else if ( [ //return document.write ( shishen[ 4] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 4] + " ") ;} else if ( [ //return document.write ( shishen[ 5] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 5] + " ") ;} else if ( [ //return document.write ( shishen[ 6] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 6] + " ") ;} else if ( [ //return document.write ( shishen[ 7] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 7] + " ") ;} else if ( [ // return document.write ( shishen[ 8] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 8] + " ") ;} else if ( [ //return document.write ( shishen[ 9] + " ") ; return document.write ( rizhu+tiangan+" "+shishen[ 9] + " ") ;} }
作者: 2461454684 时间: 2023-10-29 17:31
你可以使用一个简单的映射关系来简化你的代码。首先,你可以创建一个对象,将天干和对应的索引值进行映射,然后使用这些索引值来获取相应的十神。
下面是一个简化的示例代码:
[JavaScript] 纯文本查看 复制代码
var shishen = ["比肩", "劫财", "食神", "伤官", "偏财", "正财", "七杀", "正官", "偏印", "正印"];
var tg = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"];
function qshishen(rizhu, tiangan) {
var rizhuIndex = tg.indexOf(rizhu);
var tianganIndex = tg.indexOf(tiangan);
if (rizhuIndex !== -1 && tianganIndex !== -1) {
var shishenIndex = (rizhuIndex + tianganIndex) % 10;
return shishen[shishenIndex];
} else {
return "提交参数错误";
}
}
// 示例用法
var rizhu = "甲";
var tiangan = "丙";
var result = qshishen(rizhu, tiangan);
console.log(result); // 输出:食神
这样,你只需要使用两个索引值进行计算,而不需要逐个判断每个组合。注意,这个示例代码只处理了甲到癸的情况,如果需要处理更多的组合,你可以根据这个思路进行扩展。
欢迎光临 精易论坛 (https://125.confly.eu.org/)
Powered by Discuz! X3.4