Cut And Paste Code For Lesson 11


This week we'll start work on a program to encode your emails so that only you and your friends can read them!

Note:  We'll start by just encoding and decoding ONE letter,but check back soon for the full converter!


Single letter ascii encoder:

 

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click to encode</button>
<p id="demo"></p>
<script>
function myFunction() {
    var person = prompt("Enter a single letter to encode.");
    if (person != null) {
var encoded;
encoded = person.charCodeAt(0);
        document.getElementById("demo").innerHTML =
        "Here is your original:" + "<br>" + person + "<br>"+ " Here it is encoded:" + "<br>" + encoded;
    }
}
</script>
</body>
</html>

 

Single letter ascii decoder:

 

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Click to decode</button>
<p id="demo"></p>
<script>
function myFunction() {
    var person = prompt("Enter a number to decode:");
    if (person != null) {
var encoded;
encoded = String.fromCharCode(person);
        document.getElementById("demo").innerHTML =
        "Here is your original:" + "<br>" + person + "<br>"+ " Here it is decoded:" + "<br>" + encoded;
    }
}
</script>
</body>
</html>

 

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>