C++ to JS (converting languages)

1337hendrix's Avatar
Newbie Member
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 shabbir; 9Jan2007 at 14:35.. Reason: Code formating.
pradeep's Avatar, Join Date: Apr 2005
Team Leader
JavaScript is case-sensitive, the errors are on lines 16 and 24, please check the variable names.
1337hendrix's Avatar
Newbie Member
But did I get the general gist of the program?

I see my errors, I'm such a dumbass.
pradeep's Avatar, Join Date: Apr 2005
Team Leader
What do you mean by "But did I get the general gist of the program?" ??
DaWei's Avatar, Join Date: Dec 2006
Team Leader
You can't (or shouldn't want to) convert that C++. It isn't functional. If that's the original code, it never compiled.