【问题标题】:Convert From Basic to Javascript?从 Basic 转换为 Javascript?
【发布时间】:2015-10-20 19:48:39
【问题描述】:

晚安,

我需要从 基本进入javascript。 Javascript 没有 支持“转到”指令。我会 如果您能给我翻译,将不胜感激。 非常感谢

10 c = 1666.66
20 b = 1.007897747
30 a = 10000
40 n = 6
50 a = a * b
60 a = a -c
70 n = n -1
80 c = c + 0.01
90 if a <= 0 then 120
100 if n = 0 then 30
110 goto 50
120 print c
130 end

【问题讨论】:

  • 我投票决定将此问题作为离题结束,因为帖子要求进行代码翻译。

标签: javascript basic


【解决方案1】:

此提案具有无限 while 循环与 for 循环和退出的组合。

function x() {
    var a, b, c, n;
    c = 1666.66;
    b = 1.007897747;
    while (true) {
        a = 10000;
        for (n = 6; n--;) {
            a *= b;
            a -= c;
            c += 0.01;
            if (a <= 0) {
                return c;
            }
        }
    }
}
document.write(x());

为了证明结果,您可以复制下面的代码并将其插入到在线 BASIC 解释器中,例如 http://www.quitebasic.com/

10 let c = 1666.66
20 let b = 1.007897747
30 let a = 10000
40 let n = 6
50 let a = a * b
60 let a = a -c
70 let n = n -1
80 let c = c + 0.01
90 if a <= 0 then 120
100 if n = 0 then 30
110 goto 50
120 print c
130 end

【讨论】:

    【解决方案2】:

    这是一种方法

     // store the information in an Object, so that data can be migrated easily
        var dataSet = setInitialValues(); 
    
        // perform the calculation, this is a recursive function  
        performCalculations(dataSet);
        
        // definition for function that create the initial data set
        function setInitialValues(){
        	return {
        			c: 1666.66,
        			b : 1.007897747,
        			a : 10000,
        			n : 6
        		}
        }
        
        // reassign value to a and n, called when n reached 0
        function reSetValues(d){
        	d.a = 10000;
        	d.n = 6;
        	return d	
        }
        
        // recursive function, operates on the data set
        function performCalculations(d){
        	d.a = d.a*d.b;
        	d.a = d.a-d.c;
        	d.n = d.n-1;
        
        	d.c = d.c+0.01;
        	
        	if (d.a<= 0){
        		document.write(d.c)
        	} else if(d.n==0){
        		reSetValues(d);
        		performCalculations(d);
        	} else {
        		performCalculations(d);
        	}
        }

    【讨论】:

    • 谢谢,但它不起作用。 Javascrit 不运行代码。
    • 你在哪里运行的??将代码粘贴到浏览器的控制台中并运行它。结果将显示在控制台中。作为记录,该程序给出的结果是 1713.0999999999578
    • @user5468779 已经更新了 sn-p 的答案,您可以看到解决方案给出了正确的结果。
    猜你喜欢
    • 2018-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    相关资源
    最近更新 更多