-4

Good night,

I need to translate this sample code from Basic into javascript. Javascript does not support the "Go To" instructions. I would be grateful if you could give me a translation. Many thanks

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
  • 1
    I'm voting to close this question as off-topic because post is asking for code translation. – Joseph Oct 20 '15 at 19:51

2 Answers2

0

Here is one way of doing this

 // 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);
     }
    }
sunitj
  • 1,215
  • 1
  • 12
  • 21
  • Thanks, but it doesn't work. Javascrit don't run the code. – user5468779 Oct 21 '15 at 04:30
  • Where did you run this?? Paste the code in console of a browser and run it. The result will be displayed in console. For the record the result this program is giving is 1713.0999999999578 – sunitj Oct 21 '15 at 09:21
  • @user5468779 have updated the answer to snippet and you can see the solution is giving the correct result. – sunitj Oct 21 '15 at 09:27
0

This proposal features an infinite while loop in combination with a for loop and an exit.

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());

To proof the result, you can copy the code below and insert it into an online BASIC interpreter like 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
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392