Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/forums/javascript/)
-   -   C++ to JS (converting languages) (http://www.go4expert.com/forums/cpp-js-converting-languages-t2488/)

1337hendrix 9Jan2007 14:04

C++ to JS (converting languages)
I need help converting C++ to Javascript.

Here is the original 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:

<SCRIPT = "text/javascript">

// Quickstart C++ to JS

First = prompt("Enter a digit: ");
Second = prompt("Enter a second digit: ");


FirstNum = parseInt(First);
SecondNum = parseInt(Second);

//prepare for loop

previousnum = secondnum;
currentnum = (FirstNum + SecondNum)%10;

extra = previousnum;
previousnum = currentnum;
currentnum = (extra + currentnum)%10;
} while (previousnum !=Firstnum || currentnum != SecondNum)


document.writeln("first number "+First+"<br>");
document.writeln("second number "+Second+"<br>");
document.writeln("Necklace is "+currentnum);


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.

pradeep 9Jan2007 14:59

Re: C++ to JS (converting languages)
JavaScript is case-sensitive, the errors are on lines 16 and 24, please check the variable names.

1337hendrix 9Jan2007 15:01

Re: C++ to JS (converting languages)
But did I get the general gist of the program?

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

pradeep 9Jan2007 15:11

Re: C++ to JS (converting languages)
What do you mean by "But did I get the general gist of the program?" ??

DaWei 22Jan2007 19:19

Re: C++ to JS (converting languages)
You can't (or shouldn't want to) convert that C++. It isn't functional. If that's the original code, it never compiled.

All times are GMT +5.5. The time now is 20:58.