Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-04-2012, 06:13 AM   PM User | #1
RayonYarn
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
RayonYarn is an unknown quantity at this point
Question regarding charCodeat for conversion

So, I am quite the beginner at javascript and, for an assignment, I need to convert text to ascii. My professor mentioned charCodeat with using a for loop and an array, but I'm not sure how to do that. What my ultimate goal is to turn that ascii into binary and then the binary to morse code to use as a beat for a song. Here's what I have so far, but I think I'm way wrong. I think my problem is just trying to incorporate the charCodeat with the rest of the code. Any help?



I looked around and he said that I should do something like this:

<html>
<head>
<title>JavaScript String charCodeAt() Method</title>
</head>
<body>
<script type="text/javascript">
var str = new String( "This is string" );
document.write("str.charCodeAt(0) is:" + str.charCodeAt(0));
document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1));
document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2));
document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3));
document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4));
document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5));
</script>
</body>
</html>
RayonYarn is offline   Reply With Quote
Old 12-04-2012, 08:48 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
No need to be over-elaborate.

Code:
INPUT <input type = "text" id = "inp" size = "80" onblur = "change2ASCII(this)">
<br>
ASCII RESULT <input type = "text" id = "res" size = "80">
<br>
ASCII TO BINARY RESULT <input type = "text" id = "bin" size = "80">

<script type = "text/javascript">
function change2ASCII(which) {
var val = which.value;
var result = [];
var binary = [];
for (var i=0; i<val.length; i++) {
var x = val.charCodeAt(i);
binary[i] = x.toString(2);
result[i] = x + " ";
}

document.getElementById("res").value = result.join(" ");
document.getElementById("bin").value = binary.join(" ");

}
</script>
You define an array either with var arrName = new Array()
or (preferred) var arrName = [];


Your professor should be aware that document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded. So useable only if the character string to be converted is pre-defined.

BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

"In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 12-04-2012 at 09:02 AM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:29 PM.


Advertisement
Log in to turn off these ads.