整数“x”的起始值应该是多少?

The start value of integer "x" should be how many?

提问人:HuMaNBeinG69420 提问时间:4/15/2023 更新时间:4/16/2023 访问量:66

问:

这是一个简单的函数:

function sequence(a,b,c,d){
    var x = 0;
    var y = 0;
    for(var i = a; i<=b; i++){
        y = eval(c);
        x = eval(d);
    }
    return x;
}

A&B 必须是整数,C&D 必须是字符串

此代码获取 1 到 5 的总和:

sequence(1,5,"i","x+y")

结果:

15

此代码获取 1 到 5 的乘积:

sequence(1,5,"i","x*y")

结果:

0

问题出在“”处,“”的值应该是多少?var x = 0;x

sequence(1,5,"i","x+y") >>> 15
sequence(1,5,"i","x*y") >>> 0
sequence(1,5,"i","x-y") >>> -15
sequence(1,5,"i","x/y") >>> 0
sequence(1,5,"i","x**y") >>> 0
sequence(1,5,"i","x%y") >>> 0
JavaScript 函数 序列

评论

0赞 tkausl 4/15/2023
你要做的叫做reduce。必须传入初始值,因为没有一个适合所有用途的单个初始值。
0赞 tatactic 4/15/2023
“我”代表什么???每次调用的结果应该是什么???>>>后的数字?sequence(1,5,"i","x*y")

答:

0赞 tatactic 4/16/2023 #1

我希望当你写这个块时,>>>后面的数字不是预期的结果,否则我不明白你的问题!

sequence(1,5,"i","x+y") >>> 15
sequence(1,5,"i","x*y") >>> 0
sequence(1,5,"i","x-y") >>> -15
sequence(1,5,"i","x/y") >>> 0
sequence(1,5,"i","x**y") >>> 0
sequence(1,5,"i","x%y") >>> 0

请注意,% 代表模而不是百分比,我不知道你对结果的期望是什么!

我不明白参数中的“i”代表什么。 使用某些值时,** 运算符存在问题。 我在下面的片段中评论了这一点。

警告:从字符串执行 JavaScript 存在巨大的安全风险。

当您使用 eval() 时,不良行为者运行任意代码太容易了。

请参见 : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

这是一个仅在 a<b 时有效的片段,我将在下面添加另一个如果 a>b 也有效的片段。

        "use strict";
        function operationFromAtoB(a,b,c){
            // You don't have to declare x here
            var y = "";
            for (var i = a; i <= b; i++) {
                if(i != b){
                    y += (i.toString() + c );
                }else{
                    y += (i.toString())
                }
            }
            var result = y + " = " + eval(y);
            return result;
        }
        
        console.log(operationFromAtoB(1,5,"+"));
        console.log(operationFromAtoB(1,5,"*"));
        console.log(operationFromAtoB(1,5,"-"));
        console.log(operationFromAtoB(1,5,"/"));
        // Next line throws NaN or Infinity if out of range
        console.log(operationFromAtoB(2,4,"**"));
        // 1**2**3**4**5 = NaN
        // 2**3**4**5 = Infinity
        // 2**3**4 = 2.4178516392292583e+24
        console.log(operationFromAtoB(1,5,"%"));

这里有一个片段,如果 a<b 和 a>b 有效:

        function operationFromAtoB(a,b,c){
            var result = "";
            var y = "";
            if(a<b){
                for (var i = a; i <= b; i++) {
                    if(i != b){
                        y += (i.toString() + c );
                    }else{
                        y += (i.toString())
                    }
                }
            }else{
                for (var j = a; j >= b; j--) {
                    if(j != b){
                        y += (j.toString() + c );
                    }else{
                        y += (j.toString())
                    }
                }
            }
            result = y + " = " + eval(y);
            return result;
        }
    
        console.log("a<b !!!")
        console.log(operationFromAtoB(1,5,"+"));
        console.log(operationFromAtoB(1,5,"*"));
        console.log(operationFromAtoB(1,5,"-"));
        console.log(operationFromAtoB(1,5,"/"));
        console.log("a>b !!!")
        console.log(operationFromAtoB(5,1,"+"));
        console.log(operationFromAtoB(5,1,"*"));
        console.log(operationFromAtoB(5,1,"-"));
        console.log(operationFromAtoB(5,1,"/"));