
Cut And
Paste Code For Lesson 12
This week we'll
complete the secret encoder/decoder programs so that only you and your friends
can read your emails!
YOU CAN TRY IT OUT ONLINE RIGHT NOW BY CLICKING
HERE!
Question: How would you change the code so even the teacher
could not read it?
Click here
to go back to our lessons page.
Full sentence ascii encoder:
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click
to encode a message.</button>
<p id="demo"></p>
<script>
function
myFunction() {
var person = prompt("Enter a message to encode.");
if
(person != null) {
var encoded = "";
var n = person.length;
for(i=0;
i<n; i= i+1){
encoded = encoded + person.charCodeAt(i) + " ";
}
document.getElementById("demo").innerHTML
=
"Here is your original:"
+ "<br>" + person + "<br>"+ " Here it is
encoded:" + "<br>" + encoded;
}
}
</script>
</body>
</html>
Full sentence ascii decoder:
<!DOCTYPE html>
<html>
<body>
<button
onclick="myFunction()">Click to decode</button>
<p
id="demo"></p>
<script>
var tempcharacter = "";
function
myFunction() {
var person = prompt("Enter a message to decode.");
if
(person != null) {
var encoded = "";
var n = person.length;
for(i=0;
i<n; i= i+1){
if (person.charAt(i) != " "){
tempcharacter
= tempcharacter + person.charAt(i);
} else {
tempcharacter = String.fromCharCode(Number(tempcharacter));
encoded
= encoded + tempcharacter;
tempcharacter = "";
}
}
document.getElementById("demo").innerHTML
=
"Here is your original:" + "<br>" + person +
"<br>"+ " Here it is decoded:" + "<br>"
+ encoded;
}
}
</script>
</body>
</html>