1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

C++ to JS (converting languages)

Discussion in 'JavaScript and AJAX' started by 1337hendrix, Jan 9, 2007.

  1. 1337hendrix

    1337hendrix New Member

    Joined:
    Jan 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I need help converting C++ to Javascript.

    Here is the original code:

    Code:
    #include <iostream>
    
    int main ()
    {
    // Variables
    short first;
    short second;
    short previous;
    short current;
    short help;
    
    // Input and communication with the user
    cout << "Insert the first digit: ";
    cin >> first;
    cout << "\n";
    cout << "Insert the second digit: ";
    cin >> second;
    cout << "\n";
    cout << "The necklace is: ";
    
    // Preparing for the loop
    previous = second;
    current = (first + second)%10;
    cout >> first;
    cout >> second;
    
    // And not the WHILE loop
    
    while ( (previous != first) || (current != second) )
    {
    cout >> current;
    help = previous;
    previous = current;
    current = (help + current)%10;
    }
    
    // Writing the last digit
    cout >> current;
    
    return 0;
    }
    Here is my JS Code:
    Code:
    <SCRIPT = "text/javascript">
    
    // Quickstart C++ to JS
    
    First = prompt("Enter a digit: ");
    Second = prompt("Enter a second digit: ");
    
    //Convert
    
    FirstNum = parseInt(First);
    SecondNum = parseInt(Second);
    
    
    //prepare for loop
    
    previousnum = secondnum;
    currentnum = (FirstNum + SecondNum)%10;
    
    //loop
    do{
    extra = previousnum;
    previousnum = currentnum;
    currentnum = (extra + currentnum)%10;
    } while (previousnum !=Firstnum || currentnum != SecondNum)
    
    //Output
    
    document.writeln("first number "+First+"<br>");
    document.writeln("second number "+Second+"<br>");
    document.writeln("Necklace is "+currentnum);
    
    </SCRIPT>
    
    How well did I do?

    It won't load in my browser, so I need someone to run it for me and tell me if I did okay, and how to fix it if I didn't.
     
    Last edited by a moderator: Jan 9, 2007
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    JavaScript is case-sensitive, the errors are on lines 16 and 24, please check the variable names.
     
  3. 1337hendrix

    1337hendrix New Member

    Joined:
    Jan 9, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    But did I get the general gist of the program?

    I see my errors, I'm such a dumbass.
     
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    What do you mean by "But did I get the general gist of the program?" ??
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    You can't (or shouldn't want to) convert that C++. It isn't functional. If that's the original code, it never compiled.
     

Share This Page