You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS实现四边形类型判定脚本 条件判断逻辑问题求助

JavaScript四边形类型判定脚本修正方案

现有代码的核心问题

  • prompt()返回的结果默认是字符串类型,直接参与数值比较会出现隐式类型转换错误,所有输入必须先转为数值类型
  • 条件判断写法完全不符合逻辑:比如s1 && s2 && s3 == s4的写法是判断s1、s2为真值,且s3等于s4,并非判断四条边相等
  • 判定顺序和规则不符合几何定义:正方形是特殊的矩形/菱形,必须按「特殊到一般」的优先级判断,否则会出现类型误判
  • 菱形判定逻辑完全错误,且缺少平行四边形判定分支、输入合法性校验

四类四边形的判定优先级(从特殊到一般)

判断必须按以下顺序走,避免特殊类型被误判为普通类型:

  • 正方形:四条边长全部相等,且四个内角均为90度
  • 矩形:两组对边分别相等,且四个内角均为90度(排除已判定的正方形)
  • 菱形:四条边长全部相等,两组对角分别相等,邻角和为180度,且内角不全为90度(排除已判定的正方形)
  • 平行四边形:两组对边分别相等,两组对角分别相等,邻角和为180度(排除已判定的正方形、矩形、菱形)

关于switch语句的使用说明

switch仅适合做固定值的全等匹配,不适合当前这种多条件组合的逻辑判断场景,强行使用会大幅增加代码冗余度,当前场景用if/else if分支是最优选择。如果要练习switch语法,可以在判定出最终四边形类型后,用类型字符串作为匹配值走switch输出对应文案,不要把复杂判断逻辑塞进switch的匹配条件里。

修正后可运行代码

支持两种输入模式:需要用户输入就保留prompt部分,需要硬编码就注释掉prompt代码块,直接给边长、角度变量赋值即可。

// 硬编码模式可直接注释掉下面的prompt输入块,手动给变量赋值即可
console.log('Good day! Time to see what shape you are dealing with today.')
// 输入统一转为数值类型
let side1 = Number(prompt("What is the length of the first side? "));
let side2 = Number(prompt("What is the length of the second side?"));
let side3 = Number(prompt("What is the length of the third side?"));
let side4 = Number(prompt("What is the length of the fourth side?"));

console.log("Awesome! Now let's move to the angles.");
let angle1 = Number(prompt("What is the measurement of angle one?"));
let angle2 = Number(prompt("What is the measurement of angle two?"));
let angle3 = Number(prompt("what is the measurement of angle three?"));
let angle4 = Number(prompt("What is the measurement of angle four?"));

// 输入合法性校验
const isValid = (val) => !isNaN(val) && val > 0;
const angleSum = angle1 + angle2 + angle3 + angle4;
if (![side1, side2, side3, side4].every(isValid) || ![angle1, angle2, angle3, angle4].every(isValid) || Math.abs(angleSum - 360) > 0.1) {
    console.log("输入不合法:边长必须为正数,四个内角和必须为360度");
} else {
    let shapeType = "";
    // 提前抽离公共判断逻辑,减少重复代码
    const isAllSideEqual = side1 === side2 && side2 === side3 && side3 === side4;
    const isAllAngle90 = angle1 === 90 && angle2 ===90 && angle3 ===90 && angle4 ===90;
    const isOppositeSideEqual = side1 === side3 && side2 === side4;
    const isOppositeAngleEqual = angle1 === angle3 && angle2 === angle4;
    const isAdjacentAngleSum180 = Math.abs(angle1 + angle2 - 180) <0.1 && Math.abs(angle2 + angle3 -180) <0.1;

    if (isAllSideEqual && isAllAngle90) {
        shapeType = "square";
    } else if (isOppositeSideEqual && isAllAngle90) {
        shapeType = "rectangle";
    } else if (isAllSideEqual && isOppositeAngleEqual && isAdjacentAngleSum180) {
        shapeType = "rhombus";
    } else if (isOppositeSideEqual && isOppositeAngleEqual && isAdjacentAngleSum180) {
        shapeType = "parallelogram";
    } else {
        shapeType = "ordinary quadrilateral that does not belong to the target type";
    }
    console.log(`Your shape is ${shapeType}!`);

    // 如果要练习switch写法,可以替换上面的console.log用下面的代码
    // switch(shapeType) {
    //     case "square":
    //         console.log("Your shape is square!");
    //         break;
    //     case "rectangle":
    //         console.log("Your shape is a rectangle!");
    //         break;
    //     case "rhombus":
    //         console.log("Your shape is a rhombus!");
    //         break;
    //     case "parallelogram":
    //         console.log("Your shape is a parallelogram!");
    //         break;
    //     default:
    //         console.log("Your shape is not in the target type list.")
    // }
}

注意:判定角度相等时加了0.1的误差容忍值,避免浮点数计算精度导致的判断错误。

内容的提问来源于stack exchange,提问作者James Conacher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.03 09:21:41