/    Sign up×
Community /Pin to ProfileBookmark

Converting a simple QBASIC program into Javascript

Call me mad, but I did read the other day an amazing tale from Arthur C.Clarke, [URL=”http://en.wikipedia.org/wiki/The_Nine_Billion_Names_of_God”]The Nine Billion Names of God [/URL]about Tibetan monks looking to write down all the possible 9 letters combination of the Tibetan alphabet, so the real name of God is finally spelled, and the whole purpose of the human race is fulfilled.

For that purpose, and after centuries doing it manually, they finally rent a computer.

I liked the tale and wanted to mimic it using my own laptop. I already wrote a Basic program to swap nine ASCII characters. But my ultimate goal is to post a javascript in my blog so readers can see it working while they read along.

The code I wrote is very simple.

[CODE]
CLS
N = 26
DIM LETRA$(1 TO 26)
FOR I = 1 TO 26 ‘FEEDS 26 ASCII CHARACTERS INTO THE NINE CHARACTERS SWAPPING ROUTINE
READ LETRA$(I)
NEXT I
FOR P1 = 1 TO N ‘NINE CHARACTERS SWAPPING ROUTINE
FOR P2 = 1 TO N
FOR P3 = 1 TO N
FOR P4 = 1 TO N
FOR P5 = 1 TO N
FOR P6 = 1 TO N
FOR P7 = 1 TO N
FOR P8 = 1 TO N
FOR P9 = 1 TO N
PRINT LETRA$(P1); LETRA$(P2); LETRA$(P3); LETRA$(P4); LETRA$(P5); LETRA$(P6); LETRA$(P7); LETRA$(P8); LETRA$(P9)
NEXT P9
NEXT P8
NEXT P7
NEXT P6
NEXT P5
NEXT P4
NEXT P3
NEXT P2
NEXT P1
END
DATA “A”,”B”,”C”,”D”,”E”,”F”,”G”,”H”,”I”,”J”
DATA “K”,”L”,”M”,”N”,”O”,”P”,”Q”,”R”,”S”,”T”
DATA “U”,”V”,”W”,”X”,”Y”,”Z”
[/CODE]

So, this is my question: How could I convert that into Javascript? And, taking advantage of Javascript supporting UTF-8, how could I swap Tibetan characters instead?

There’s about 30 of them, not bit deal.

I hope you like my project and want to give it some friendly hand.

Thanks!

Diego

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@dmboydApr 01.2009 — I figured it might be a fun little coding exercise. :p

var n = 26;
var letra = new Array();
/* I'm not sure about the Tibetan alphabet, but I'm using a quick
* shortcut (integral values of ASCII characters) to generate A-Z. */
for (var i = 0; i < n; ++i)
letra.push(String.fromCharCode(i + 0x41));
for (var p1 = 0; p1 < n; ++p1)
for (var p2 = 0; p2 < n; ++p2)
for (var p3 = 0; p3 < n; ++p3)
for (var p4 = 0; p4 < n; ++p4)
for (var p5 = 0; p5 < n; ++p5)
for (var p6 = 0; p6 < n; ++p6)
for (var p7 = 0; p7 < n; ++p7)
for (var p8 = 0; p8 < n; ++p8)
for (var p9 = 0; p9 < n; ++p9)
document.writeln(letra[p1]
+ letra[p2]
+ letra[p3]
+ letra[p4]
+ letra[p5]
+ letra[p6]
+ letra[p7]
+ letra[p8]
+ letra[p9]
+ "<br>");
Copy linkTweet thisAlerts:
@diegorodriguezauthorApr 02.2009 — That's great, boy! It worked just fine ?

Only two things to refine:

MISSING CODE POINT

Tibetan letters start at U+0F40 and end at U+0F6C, but the last 3 don't show up in Arial, so I limited the alphabet to the first 41 letters. Besides, code point U+0F48 is missing (go figure why) so the alphabet feeding iteration should skip this number. How could we do that?

[CODE]var n = 41;
var letra = new Array();
for (var i = 0; i < n; ++i)
letra.push(String.fromCharCode(i + 0x0f40));[/CODE]


MEMORY OVERRUN

The printout ends after 10256 lines due to memory shortage. The current

[CODE]document.writeln(letra[p1][/CODE]

must be replaced by some in-place or batch printing or cleaning the memory so no limits affect the iterations within the design boundaries. Do you have any idea how to do that?

Once again, many thanks for any hint!
Copy linkTweet thisAlerts:
@haulinApr 08.2009 — i have a slightly different approach based on combinatoric variatons:
[CODE]
function variate(n,k)
{
var word="";
for (var i=0; i<Math.pow(n,k); i++)
{
for (var j=k; j>0; j--)
{
word=String.fromCharCode(Math.floor(i/Math.pow(n,k-j))&#37;n+0x41)+word;
}
document.write(word+"<br>");
word="";
}
}
[/CODE]

with this function you can output sequence of n items from a k-itemed set, so if you want to write all the 9-itemed sequences from 26 possible items, you'd call it [B]variate(26,9)[/B], but already variate(4,9) takes a huge amount of time to display, so i don't know how you want to optimalize that. i just thought it looks nicer than 9 nested for loops ?
Copy linkTweet thisAlerts:
@zsnpMar 03.2017 — In JavaScript, try to avoid repeating the document.writeln() function, because it will cause you to run slow or run out of memory. When you have to write something repeatedly, create an array and push all the data into that temporary array. Then, before the program ends do a single document.writeln(ARRAY.join("")); instruction, and that will print everything in one quick step. Avoid using strings, because they also slow down your JavaScript code. So, don't do this: for (...) { myString += "new data"; } This is going to end in disaster. Do this instead : for (...) { myArray.push("new data"); } This will run faster. And at the end you can dump it all on the screen.
×

Success!

Help @diegorodriguez spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...