如何从 JS(ES6)中的 for 循环修复 NaN?

Posted

技术标签:

【中文标题】如何从 JS(ES6)中的 for 循环修复 NaN?【英文标题】:How to fix a NaN from a forloop in JS (ES6)? 【发布时间】:2017-08-10 21:18:50 【问题描述】:

我正在用 javascript 构建自己的数学课。我将默认精度声明为 8。当我将它传递给函数时,会将 for-loop 中的结果更改为 NaN

class Mathematics 
    constructor(angleType, accuracy) 
        this.PI = 3.1415926535897932384626433832795;
        this.at = angleType || "rad";
        this.acc = accuracy || 8;
    

    Ang(angle, at) 
        let angdeg, angrad;
        switch (at) 
            case "deg" || "d" || "Deg":
                angdeg = angle;
                angrad = angdeg * this.PI / 180;
                break;
            case "rad" || "r" || "Rad":
                angrad = angle;
                break;
        
        return angrad;
    

    Sin(angle, at, acc) 
        let _at = at || this.at,
            angrad = this.Ang(angle, _at),
            ans = 1,
            _acc = acc || this.acc,
            temp = 1;
        for (let i = 2; i <= 2 * _acc; i += 2) 
            temp = temp * (-1) * angrad * angrad / (i * (i + 1));
            ans = ans + temp;
            console.log("sin " + i / 2 + ": " + ans);
        
        return ans;
    

    sin(angle, at, acc)  return this.Sin(angle, at, acc) 

    Cos(angle, at, acc) 
        let _at = at || this.at,
            angrad = this.Ang(angle, _at),
            ans = angrad,
            _acc = acc || this.acc,
            temp = 1;
        for (let i = 2; i <= 2 * _acc; i += 2) 
            temp = temp * (-1) * angrad * angrad / (i * (i - 1));
            ans = ans + temp;
            console.log("cos " + i / 2 + ": " + ans);
        
        return ans;
    

    cos(angle, at, acc)  return this.Cos(angle, at, acc) 

    Tan(angle, at, acc)  return this.Sin(angle, at, acc) / this.Cos(angle, at, acc) 

    tan(angle, at, acc)  return this.Tan(angle, at, acc) 

var MathClass = new Mathematics();

console.log("Class sin: " + MathClass.sin(1));
console.log("Math sin: " + Math.sin(1));
console.log("Class cos: " + MathClass.cos(1));
console.log("Math cos: " + Math.cos(1));
console.log("Class tan: " + MathClass.tan(1) + " an accuration of 8");
console.log("Class tan: " + MathClass.tan(1, "r", 9) + " an accuration of 9");
console.log("Math tan: " + Math.tan(1));

如果您运行 sn-p,您会看到 for-loop 的长度很好,但答案会变成 NaN

您可以将构造函数中的准确度编辑为 9,它可以正常工作,直到您更改最后一个参数:MathClass.tan(1, "r", 5)

【问题讨论】:

【参考方案1】:

您的问题是,此语法不能用作在 JavaScript 中捕获多个案例的简写:

case "rad" || "r" || "Rad":

您需要使用多个case 语句,如下所示:

case "rad":
case "r":
case "Rad":

编辑:或使用if/else和正则表达式:

Ang(angle, at) 
    at = at || this.at

    if (/^r(ad)?$/i.test(at)) 
        return angle
     else if (/^d(eg)?$/i.test(at)) 
        return angle * this.PI / 180
     else 
        throw new Error("Invalid angle type: " + at)
    


演示片段:

class Mathematics 
    constructor(angleType, accuracy) 
        this.PI = 3.1415926535897932384626433832795;
        this.at = angleType || "rad";
        this.acc = accuracy || 8;
    

    Ang(angle, at) 
        let angdeg, angrad;
        switch (at) 
            case "deg":
            case "d":
            case "Deg":
                angdeg = angle;
                angrad = angdeg * this.PI / 180;
                break;
            case "rad":
            case "r":
            case "Rad":
                angrad = angle;
                break;
        
        return angrad;
    

    Sin(angle, at, acc) 
        let _at = at || this.at,
            angrad = this.Ang(angle, _at),
            ans = 1,
            _acc = acc || this.acc,
            temp = 1;
        for (let i = 2; i <= 2 * _acc; i += 2) 
            temp = temp * (-1) * angrad * angrad / (i * (i + 1));
            ans = ans + temp;
            console.log("sin " + i / 2 + ": " + ans);
        
        return ans;
    

    sin(angle, at, acc)  return this.Sin(angle, at, acc) 

    Cos(angle, at, acc) 
        let _at = at || this.at,
            angrad = this.Ang(angle, _at),
            ans = angrad,
            _acc = acc || this.acc,
            temp = 1;
        for (let i = 2; i <= 2 * _acc; i += 2) 
            temp = temp * (-1) * angrad * angrad / (i * (i - 1));
            ans = ans + temp;
            console.log("cos " + i / 2 + ": " + ans);
        
        return ans;
    

    cos(angle, at, acc)  return this.Cos(angle, at, acc) 

    Tan(angle, at, acc)  return this.Sin(angle, at, acc) / this.Cos(angle, at, acc) 

    tan(angle, at, acc)  return this.Tan(angle, at, acc) 

var MathClass = new Mathematics();

console.log("Class sin: " + MathClass.sin(1));
console.log("Math sin: " + Math.sin(1));
console.log("Class cos: " + MathClass.cos(1));
console.log("Math cos: " + Math.cos(1));
console.log("Class tan: " + MathClass.tan(1) + " an accuration of 8");
console.log("Class tan: " + MathClass.tan(1, "r", 9) + " an accuration of 9");
console.log("Math tan: " + Math.tan(1));

【讨论】:

更糟糕的是,"rad" || "r" || "Rad" 是有效语法:它等于 "rad",因为这是一个真实的值。

以上是关于如何从 JS(ES6)中的 for 循环修复 NaN?的主要内容,如果未能解决你的问题,请参考以下文章

forin循环的变量如何变为全局

[ES6深度解析]2:迭代器(Iterators )和for of循环

es6中的数组循环和对象方法

详解js循环和ES6的iterator

es6笔记 Iterator 和 for...of循环

js for in 和 for of的区别