开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 650|回复: 5
打印 上一主题 下一主题
收起左侧

[求助] 教一下 这个JS 如何写 更简便直接得出结果

[复制链接]
结帖率:100% (10/10)
跳转到指定楼层
楼主
发表于 2023-10-29 10:43:22 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式   山西省太原市
  
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)
结帖率:79% (11/14)

签到天数: 2 天

沙发
发表于 2023-10-29 10:53:05 | 只看该作者   广东省广州市
你的代码可以通过使用数组索引来简化,从而避免大量的 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:15 | 只看该作者   山东省潍坊市
[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>")
        }
}

回复 支持 反对

使用道具 举报

结帖率:100% (10/10)
地板
 楼主| 发表于 2023-10-29 11:15:07 | 只看该作者   山西省太原市
湘伦 发表于 2023-10-29 10:53
你的代码可以通过使用数组索引来简化,从而避免大量的 else if 语句。首先,你可以将天干和地支转换为数组 ...



输入别的 得出结果 不对,麻烦 在帮忙改一下,
就是想要
输入 甲甲 得出 比肩
那么输入 壬甲 就得出 食神
那么 丁庚 就得出 正财
以此类推
不知 我表达清楚了嘛 ?
代码怎么在改一下呢
就是十个天干和十个天干的不同组合 每一个组合 有十个结果,  
得出了左边那一栏目的 比肩 劫财 食神。。。。。。
回复 支持 反对

使用道具 举报

结帖率:100% (10/10)
地下
 楼主| 发表于 2023-10-29 14:07:08 | 只看该作者   山西省太原市
在 湘伦 这个朋友回答时 说由文心大模型生成,得到启发,在用讯飞星火模型 一点点的问,终于解决了 我需要的答案,看来这个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 ([ ' 00', '11', '22', '33', '44', '55', '66', '77', '88', '99'].includes(shishenIndex)) {
//return document.write (shishen[0] + "
");
return document.write (rizhu+tiangan+" "+shishen[0] + "
");
}
else if ([ ' 01', '10', '23', '32', '45', '54', '67', '76', '89', '98'].includes(shishenIndex)) {
//return document.write (shishen[1] + "
");
return document.write (rizhu+tiangan+" "+shishen[1] + "
");
}
else if ([ ' 02', '13', '24', '35', '46', '57', '68', '79', '80', '91'].includes(shishenIndex)) {
//return document.write (shishen[2] + "
");
return document.write (rizhu+tiangan+" "+shishen[2] + "
");
}
else if ([ ' 03', '12', '25', '34', '47', '56', '69', '78', '81', '90'].includes(shishenIndex)) {
//return document.write (shishen[3] + "
");
return document.write (rizhu+tiangan+" "+shishen[3] + "
");
}
else if ([ ' 04', '15', '26', '37', '48', '59', '60', '71', '82', '93'].includes(shishenIndex)) {
//return document.write (shishen[4] + "
");
return document.write (rizhu+tiangan+" "+shishen[4] + "
");
}  
else if ([ ' 05', '14', '27', '36', '49', '58', '61', '70', '83', '92'].includes(shishenIndex)) {
//return document.write (shishen[5] + "
");
return document.write (rizhu+tiangan+" "+shishen[5] + "
");
}  
else if ([ ' 06', '17', '28', '39', '40', '51', '62', '73', '84', '95'].includes(shishenIndex)) {
//return document.write (shishen[6] + "
");
return document.write (rizhu+tiangan+" "+shishen[6] + "
");
}  
else if ([ ' 07', '16', '29', '38', '41', '50', '63', '72', '85', '94'].includes(shishenIndex)) {
//return document.write (shishen[7] + "
");
return document.write (rizhu+tiangan+" "+shishen[7] + "
");
}
else if ([ ' 08', '19', '20', '31', '42', '53', '64', '75', '86', '97'].includes(shishenIndex)) {
// return document.write (shishen[8] + "
");
return document.write (rizhu+tiangan+" "+shishen[8] + "
");
}
else if ([ ' 09', '18', '21', '30', '43', '52', '65', '74', '87', '96'].includes(shishenIndex)) {
//return document.write (shishen[9] + "
");
return document.write (rizhu+tiangan+" "+shishen[9] + "
");
}

}



回复 支持 反对

使用道具 举报

结帖率:0% (0/1)

签到天数: 1 天

6
发表于 2023-10-29 17:31:13 | 只看该作者   福建省莆田市
你可以使用一个简单的映射关系来简化你的代码。首先,你可以创建一个对象,将天干和对应的索引值进行映射,然后使用这些索引值来获取相应的十神。

下面是一个简化的示例代码:

[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); // 输出:食神


这样,你只需要使用两个索引值进行计算,而不需要逐个判断每个组合。注意,这个示例代码只处理了甲到癸的情况,如果需要处理更多的组合,你可以根据这个思路进行扩展。
回复 支持 反对

使用道具 举报

  高级模式
B Color Image Link Quote Code Smilies |上传

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:[email protected]
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表