{"id":170541,"date":"2024-05-08T17:10:01","date_gmt":"2024-05-08T09:10:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170541.html"},"modified":"2024-05-08T17:10:06","modified_gmt":"2024-05-08T09:10:06","slug":"javascript-%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%8a%a0%e5%87%8f%e4%b9%98%e9%99%a4%e8%bf%90%e7%ae%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/170541.html","title":{"rendered":"javascript \u7a0b\u5e8f\u5982\u4f55\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27041144\/619e2f2c-e03e-40ee-bb6a-6d5bdee591e3.webp\" alt=\"javascript \u7a0b\u5e8f\u5982\u4f55\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\" \/><\/p>\n<p><p>JavaScript \u7a0b\u5e8f\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u901a\u8fc7<strong>\u4f7f\u7528\u7b97\u672f\u8fd0\u7b97\u7b26<\/strong>\u3001<strong>\u6784\u5efa\u51fd\u6570<\/strong>\u3001<strong>\u5904\u7406\u5f02\u5e38<\/strong>\u3001<strong>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/strong>\u7b49\u65b9\u6cd5\u5b8c\u6210\u3002\u7b97\u672f\u8fd0\u7b97\u7b26\u5305\u62ec\u52a0\u53f7\uff08+\uff09\u3001\u51cf\u53f7\uff08-\uff09\u3001\u4e58\u53f7\uff08*\uff09\u548c\u9664\u53f7\uff08\/\uff09\uff0c\u5b83\u4eec\u7528\u4e8e\u6267\u884c\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\u3002\u5728 JavaScript \u4e2d\uff0c\u60a8\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u4e0e\u6570\u5b57\u4e00\u8d77\u6267\u884c\u8ba1\u7b97\uff0c\u6216\u8005\u6784\u5efa\u66f4\u590d\u6742\u7684\u51fd\u6570\u6765\u8fdb\u884c\u66f4\u9ad8\u7ea7\u7684\u6570\u5b66\u5904\u7406\uff0c\u4f8b\u5982\u5904\u7406\u7528\u6237\u8f93\u5165\u6216\u6267\u884c\u590d\u6742\u7684\u7b97\u672f\u3002\u4e3a\u786e\u4fdd\u7a0b\u5e8f\u6b63\u786e\u8fd0\u884c\uff0c\u901a\u5e38\u8fd8\u9700\u8981\u7f16\u5199\u4ee3\u7801\u6765\u5904\u7406\u53ef\u80fd\u51fa\u73b0\u7684\u5f02\u5e38\uff0c\u6bd4\u5982\u9664\u4ee5\u96f6\u7684\u60c5\u51b5\u3002\u6b64\u5916\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5982 Math.js \u8fd9\u6837\u7684\u7b2c\u4e09\u65b9\u5e93\u6765\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u521b\u5efa\u7b80\u5355\u7684\u52a0\u51cf\u4e58\u9664\u51fd\u6570\uff0c\u5e76\u5904\u7406\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u6765\u8be6\u7ec6\u8bf4\u660e\u5982\u4f55\u5728 JavaScript \u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u57fa\u672c\u7684\u7b97\u672f\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b9a\u4e49\u8fd0\u7b97\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u8981\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u56db\u4e2a\u57fa\u672c\u7684\u8fd0\u7b97\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u52a0\u6cd5\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function add(a, b) {<\/p>\n<p>    return a + b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u51cf\u6cd5\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function subtract(a, b) {<\/p>\n<p>    return a - b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4e58\u6cd5\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function multiply(a, b) {<\/p>\n<p>    return a * b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u9664\u6cd5\u51fd\u6570<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function divide(a, b) {<\/p>\n<p>    if(b === 0) {<\/p>\n<p>        throw new Error(&#039;\u4e0d\u80fd\u9664\u4ee5\u96f6&#039;);<\/p>\n<p>    }<\/p>\n<p>    return a \/ b;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u56db\u4e2a\u51fd\u6570\u90fd\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5176\u76f8\u5e94\u7684\u7b97\u672f\u8fd0\u7b97\u7ed3\u679c\u3002\u5bf9\u4e8e\u9664\u6cd5\u51fd\u6570\uff0c\u6211\u4eec\u8fd8\u52a0\u5165\u4e86\u4e00\u4e2a\u68c0\u67e5\uff0c\u786e\u4fdd\u9664\u6570\u4e0d\u4e3a\u96f6\uff0c\u5982\u679c\u4e3a\u96f6\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5904\u7406\u7528\u6237\u8f93\u5165<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u7684\u7a0b\u5e8f\u4e2d\uff0c\u5904\u7406\u6765\u81ea\u7528\u6237\u7684\u8f93\u5165\u662f\u975e\u5e38\u5e38\u89c1\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><p><strong>\u83b7\u53d6\u7528\u6237\u8f93\u5165<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function getUserInput(message) {<\/p>\n<p>    return parseFloat(prompt(message));<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u8fd0\u7b97\u5e76\u8f93\u51fa\u7ed3\u679c<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function calculateAndDisplayResult(operation, a, b) {<\/p>\n<p>    let result;<\/p>\n<p>    try {<\/p>\n<p>        switch (operation) {<\/p>\n<p>            case &#039;add&#039;:<\/p>\n<p>                result = add(a, b);<\/p>\n<p>                break;<\/p>\n<p>            case &#039;subtract&#039;:<\/p>\n<p>                result = subtract(a, b);<\/p>\n<p>                break;<\/p>\n<p>            case &#039;multiply&#039;:<\/p>\n<p>                result = multiply(a, b);<\/p>\n<p>                break;<\/p>\n<p>            case &#039;divide&#039;:<\/p>\n<p>                result = divide(a, b); \/\/ \u6ce8\u610f\uff1a\u5f02\u5e38\u53ef\u80fd\u5728\u8fd9\u91cc\u88ab\u629b\u51fa<\/p>\n<p>                break;<\/p>\n<p>            default:<\/p>\n<p>                throw new Error(&#039;\u672a\u77e5\u7684\u8fd0\u7b97&#039;);<\/p>\n<p>        }<\/p>\n<p>        alert(`\u7ed3\u679c\u662f: ${result}`);<\/p>\n<p>    } catch (error) {<\/p>\n<p>        alert(`\u8fd0\u7b97\u9519\u8bef: ${error.message}`);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>\/\/ \u793a\u4f8b\u4f7f\u7528\uff1a<\/p>\n<p>const num1 = getUserInput(&#039;\u8bf7\u8f93\u5165\u7b2c\u4e00\u4e2a\u6570\u5b57:&#039;);<\/p>\n<p>const num2 = getUserInput(&#039;\u8bf7\u8f93\u5165\u7b2c\u4e8c\u4e2a\u6570\u5b57:&#039;);<\/p>\n<p>const operation = prompt(&#039;\u8bf7\u8f93\u5165\u8fd0\u7b97\u7c7b\u578b\uff08add, subtract, multiply, divide\uff09:&#039;);<\/p>\n<p>calculateAndDisplayResult(operation, num1, num2);<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7 <code>getUserInput<\/code> \u51fd\u6570\u6765\u83b7\u53d6\u7528\u6237\u8f93\u5165\u7684\u6570\u5b57\u3002\u7136\u540e\u5b9a\u4e49\u4e86 <code>calculateAndDisplayResult<\/code>\uff0c\u5b83\u63a5\u53d7\u7528\u6237\u5e0c\u671b\u8fdb\u884c\u7684\u8fd0\u7b97\u7c7b\u578b\u548c\u4e24\u4e2a\u6570\u5b57\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u6267\u884c\u76f8\u5e94\u7684\u8fd0\u7b97\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u9ad8\u7ea7\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u53ef\u80fd\u9700\u8981\u4f9d\u8d56\u4e8e\u7b2c\u4e09\u65b9\u5e93\u3002<\/p>\n<\/p>\n<p><p><strong>\u5f15\u5165\u7b2c\u4e09\u65b9\u5e93 Math.js<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/mathjs\/7.5.1\/math.min.js&quot;&gt;&lt;\/script&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\u53ef\u4ee5\u4f7f\u7528 math.js \u63d0\u4f9b\u7684\u5f3a\u5927\u529f\u80fd\uff0c\u6bd4\u5982\u5206\u6570\u8fd0\u7b97\u3001\u590d\u6570\u8fd0\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528 math.js \u8fdb\u884c\u8fd0\u7b97<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function advancedAdd(a, b) {<\/p>\n<p>    return math.add(a, b);<\/p>\n<p>}<\/p>\n<p>function advancedDivide(a, b) {<\/p>\n<p>    try {<\/p>\n<p>        return math.divide(a, b);<\/p>\n<p>    } catch (error) {<\/p>\n<p>        alert(&#039;\u8fd0\u7b97\u9519\u8bef:&#039;, error.message);<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>\/\/ \u793a\u4f8b\u4f7f\u7528\uff1a<\/p>\n<p>const result = advancedAdd(math.fraction(1, 3), math.fraction(1, 5));<\/p>\n<p>console.log(`\u5206\u6570\u76f8\u52a0\u7684\u7ed3\u679c\u662f: ${result.toString()}`); \/\/ &quot;8\/15&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5f02\u5e38\u548c\u8fb9\u754c\u60c5\u51b5<\/h3>\n<\/p>\n<p><p>\u5f53\u6267\u884c\u7b97\u672f\u8fd0\u7b97\u65f6\uff0c\u603b\u4f1a\u6709\u5f02\u5e38\u6216\u8fb9\u754c\u60c5\u51b5\u9700\u8981\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u5f02\u5e38\u5904\u7406<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5df2\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u793a\u4f8b\u4e2d\u6db5\u76d6\u4e86\u5f02\u5e38\u5904\u7406\u3002\u901a\u8fc7\u4f7f\u7528 <code>try...catch<\/code> \u7ed3\u6784\uff0c\u6211\u4eec\u53ef\u4ee5\u6355\u6349\u5230\u51fd\u6570\u4e2d\u53ef\u80fd\u629b\u51fa\u7684\u9519\u8bef\uff0c\u5e76\u5411\u7528\u6237\u5c55\u793a\u9519\u8bef\u4fe1\u606f\u3002<\/p>\n<\/p>\n<p><p><strong>\u8fb9\u754c\u60c5\u51b5\u5904\u7406<\/strong>\uff1a<\/p>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u9664\u6cd5\u8fd0\u7b97\u65f6\uff0c\u8fb9\u754c\u60c5\u51b5\u4e4b\u4e00\u5c31\u662f\u9664\u6570\u53ef\u80fd\u4e3a\u96f6\u3002\u8fd9\u79cd\u60c5\u51b5\u9700\u8981\u7279\u522b\u68c0\u67e5\uff0c\u56e0\u4e3a\u9664\u4ee5\u96f6\u5728\u6570\u5b66\u4e0a\u662f\u672a\u5b9a\u4e49\u7684\uff0c\u5e76\u4e14\u5728\u8bb8\u591a\u7f16\u7a0b\u73af\u5883\u4e2d\u4e5f\u662f\u975e\u6cd5\u7684\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function <a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>Divide(a, b) {<\/p>\n<p>    if(b === 0) {<\/p>\n<p>        return &#039;\u9519\u8bef\uff1a\u9664\u6570\u4e0d\u80fd\u4e3a\u96f6\u3002&#039;;<\/p>\n<p>    }<\/p>\n<p>    return divide(a, b); \/\/ \u4f7f\u7528\u4e4b\u524d\u5b9a\u4e49\u7684 divide \u51fd\u6570<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>safeDivide<\/code> \u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u5728\u6267\u884c\u9664\u6cd5\u4e4b\u524d\u68c0\u67e5\u9664\u6570\u662f\u5426\u4e3a\u96f6\u3002\u5982\u679c\u662f\u96f6\uff0c\u8fd4\u56de\u9519\u8bef\u6d88\u606f\uff1b\u5982\u679c\u4e0d\u662f\uff0c\u5219\u8c03\u7528\u4e4b\u524d\u5b9a\u4e49\u7684 <code>divide<\/code> \u51fd\u6570\u6765\u6267\u884c\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4ee5\u4e0a\u662f\u5728 JavaScript \u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u7684\u65b9\u6cd5\u3002\u901a\u8fc7<strong>\u660e\u786e\u8fd0\u7b97\u89c4\u5219<\/strong>\u3001<strong>\u5408\u7406\u4f7f\u7528\u51fd\u6570<\/strong>\u3001<strong>\u5f02\u5e38\u5904\u7406<\/strong>\u53ca<strong>\u5916\u90e8\u5e93\u7684\u8f85\u52a9<\/strong>\uff0c\u53ef\u4ee5\u9ad8\u6548\u4e14\u51c6\u786e\u5730\u6267\u884c\u57fa\u672c\u548c\u590d\u6742\u7684\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5728JavaScript\u4e2d\u600e\u6837\u5b9e\u73b0\u52a0\u6cd5\u8fd0\u7b97\uff1f<\/strong><br \/>\nA: \u8981\u5728JavaScript\u4e2d\u6267\u884c\u52a0\u6cd5\u8fd0\u7b97\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u52a0\u6cd5\u64cd\u4f5c\u7b26\u201c+\u201d\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6570\u5b57\u76f8\u52a0\uff0c\u6216\u8005\u5c06\u5b57\u7b26\u4e32\u4e0e\u6570\u5b57\u76f8\u52a0\u6765\u8fdb\u884c\u5b57\u7b26\u4e32\u62fc\u63a5\u3002<\/p>\n<p><strong>Q: \u5728JavaScript\u4e2d\u600e\u6837\u5b9e\u73b0\u51cf\u6cd5\u8fd0\u7b97\uff1f<\/strong><br \/>\nA: \u5728JavaScript\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u51cf\u6cd5\u64cd\u4f5c\u7b26\u201c-\u201d\u6765\u6267\u884c\u51cf\u6cd5\u8fd0\u7b97\u3002\u4f60\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6570\u5b57\u76f8\u51cf\uff0c\u6216\u5c06\u4e00\u4e2a\u6570\u5b57\u51cf\u53bb\u53e6\u4e00\u4e2a\u6570\u5b57\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u5728JavaScript\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u4e58\u6cd5\u548c\u9664\u6cd5\u8fd0\u7b97\uff1f<\/strong><br \/>\nA: JavaScript\u4e2d\u5b9e\u73b0\u4e58\u6cd5\u8fd0\u7b97\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u4e58\u6cd5\u64cd\u4f5c\u7b26\u201c*\u201d\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e24\u4e2a\u6570\u5b57\u76f8\u4e58\u3002\u8981\u6267\u884c\u9664\u6cd5\u8fd0\u7b97\uff0c\u53ef\u4ee5\u4f7f\u7528\u9664\u6cd5\u64cd\u4f5c\u7b26\u201c\/\u201d\uff0c\u5b83\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6570\u5b57\u9664\u4ee5\u53e6\u4e00\u4e2a\u6570\u5b57\u3002\u6ce8\u610f\uff0c\u5982\u679c\u9664\u6570\u4e3a0\uff0c\u5219\u4f1a\u5f97\u5230\u4e00\u4e2a\u7279\u6b8a\u503cNaN\uff08Not a Number\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"JavaScript \u7a0b\u5e8f\u5b9e\u73b0\u52a0\u51cf\u4e58\u9664\u8fd0\u7b97\u901a\u8fc7\u4f7f\u7528\u7b97\u672f\u8fd0\u7b97\u7b26\u3001\u6784\u5efa\u51fd\u6570\u3001\u5904\u7406\u5f02\u5e38\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u7b49\u65b9\u6cd5\u5b8c\u6210\u3002\u7b97\u672f [&hellip;]","protected":false},"author":3,"featured_media":170544,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170541"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=170541"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/170541\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/170544"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=170541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=170541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=170541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}