/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help Please!

I am developing an educational website that uses a Huffman Coding Tree and given user input, it prints the compressed binary on the page. I am using this code:

[CODE]
function toBinary(){
var message = document.getElementById(“word”).value;
for (rep = 0; rep < message.length; rep++)
{
write(message.substring(rep,rep+1));
}
[/CODE]

where “word” is my textarea. I already have variables named a-z that are set correspondingly to their compressed binary strings, and my hope is that if the substring returns an “h” it will print out the binary stored in var “h”. I know that this code as it is, will only print out the letter “h”. Also, if i use:

[CODE]
write(eval(message.substring(rep,rep+1)));
[/CODE]

it only works as I want it to 8 out of the 26 times. If anyone else knows how to fix my code so that it can stay very short like this, I would greatly appreciate it.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@xelawhoOct 24.2015 — In plain terms it sounds like what you are saying is

[CODE]if(message[rep]==="h"){
write h;
}[/CODE]


Does that not do what you want?
Copy linkTweet thisAlerts:
@daveyerwinOct 24.2015 — I am developing an educational website that uses a Huffman Coding Tree and given user input, it prints the compressed binary on the page. I am using this code:
[CODE]
function toBinary(){
var message = document.getElementById("word").value;
for (rep = 0; rep < message.length; rep++)
{
write(message.substring(rep,rep+1));
}
[/CODE]

where "word" is my textarea. I already have variables named a-z that are set correspondingly to their compressed binary strings, and my hope is that if the substring returns an "h" it will print out the binary stored in var "h". I know that this code as it is, will only print out the letter "h". Also, if i use:
[CODE]
write(eval(message.substring(rep,rep+1)));
[/CODE]

it only works as I want it to 8 out of the 26 times. If anyone else knows how to fix my code so that it can stay very short like this, I would greatly appreciate it.[/QUOTE]


Your code should be more like this...

[CODE]
<textarea id=word>asd</textarea>

<script>
hg={}
hg['a']=4
hg['s']=3
hg['d']=2

var message = document.getElementById("word").value;
for (rep = 0; rep < message.length; rep++)
{

alert(hg[message.substring(rep,rep+1)].toString(2));
}
</script>[/CODE]


don't use eval unless you

fully understand what it does
Copy linkTweet thisAlerts:
@MammabuffaloauthorOct 24.2015 — It does do what I want, however it has to be done for every letter individually and is extremely redundant. Im trying too keep it really short.

Perhaps its better if you can see all of my code together.
[CODE]
var a = 0010;
var b = 00000;
var c = 10000;
var d = 00111;
var e = 110;
var f = 010011;
var g = 00001;
var h = 1001;
var i = 1111;
var j = 0100001000;
var k = 01000011;
var l = 00110;
var m = 010010;
var n = 1110;
var o = 001;
var p = 00010;
var q = 0100001010;
var r = 1010;
var s = 1011;
var t = 0101;
var u = 10001;
var v = 0100000;
var w = 010001;
var x = 0100001001;
var y = 00011;
var z = 0100001011;

function format(word, formatStyle)
{
return "<" + formatStyle + ">" + word + "</" + formatStyle + ">";
}



function toBinary()
{
var message = document.getElementById("word").value;
for (rep = 0; rep < message.length; rep++)
{
reps = rep;
if(rep%3 === 0)//fitting it nicely on the page
{
total = total.concat("</br>");
}
if (message.substring(rep,rep+1) === (" "))
{
total = total.concat("011");
}
else
{
//so here I want it to work so that if i enter "h" in the textarea it returns the value of var "h" which is "1001" and so on and so forth
total = total.concat(message.substring(rep,rep+1));
}
}
write(format(total,"wordspan")); //wordspan is my id for my text formatting
}
[/CODE]
Copy linkTweet thisAlerts:
@MammabuffaloauthorOct 24.2015 — I accidentally took out the declaration of var total in the above code. It's there in the real js file.
Copy linkTweet thisAlerts:
@MammabuffaloauthorOct 24.2015 — Solved!
[CODE]
ht();

var binary = ["0010","00000","10000","00111","110","010011","00001","1001","1111","0100001000","01000011","00110","010010","1110","001","00010","0100001010","1010","1011","0101","10001","0100000","010001","0100001001","00011","0100001011"];

function format(word, formatStyle)
{
return "<" + formatStyle + ">" + word + "</" + formatStyle + ">";
}

function toBinary()
{
var message = document.getElementById("word").value;
var total = "";
for (rep = 0; rep < message.length; rep++)
{
reps = rep;
if(rep%3 === 0)
{
total = total.concat("</br>");
}
if (message.substring(rep,rep+1) === (" "))
{
total = total.concat("011");
}
else
{
total = total.concat(binary[message.charCodeAt(rep)-97]);
}
}
write(format(total + "</br>","wordspan"));
}
[/CODE]
Copy linkTweet thisAlerts:
@daveyerwinOct 24.2015 — Solved!
[CODE]
ht();

var binary = ["0010","00000","10000","00111","110","010011","00001","1001","1111","0100001000","01000011","00110","010010","1110","001","00010","0100001010","1010","1011","0101","10001","0100000","010001","0100001001","00011","0100001011"];

function format(word, formatStyle)
{
return "<" + formatStyle + ">" + word + "</" + formatStyle + ">";
}

function toBinary()
{
var message = document.getElementById("word").value;
var total = "";
for (rep = 0; rep < message.length; rep++)
{
reps = rep;
if(rep%3 === 0)
{
total = total.concat("</br>");
}
if (message.substring(rep,rep+1) === (" "))
{
total = total.concat("011");
}
else
{
total = total.concat(binary[message.charCodeAt(rep)-97]);
}
}
write(format(total + "</br>","wordspan"));
}
[/CODE]
[/QUOTE]


generally, You don't have to store the binary as string ...
[CODE]

<textarea id=word>asd</textarea>
<textarea id=bin></textarea>
<script>
hg={}
hg['a']=4
hg['s']=3
hg['d']=2

var message = document.getElementById("word").value;
for (rep = 0; rep < message.length; rep++)
{
var binary = hg[message.substring(rep,rep+1)].toString(2)+" ";
var s = "000000000" + binary;
bin.value+=s.substr(s.length-5);
}
</script>

[/CODE]


but in your case you may have to

because the leading zeros may

be significant ?
×

Success!

Help @Mammabuffalo 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...