Caesar Cipher Code : Syntax errors


#1

Can anyone help me please this is my code:

var alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ""];

var encrypt = function (sent){

var prompt = ("Please enter a sentence!");
sent = sent.toLowerCase();
var key = parseInt(prompt("Please enter a key"));
var output = "";
for (var i=0;ivar char = sent [i];
var position = alpha.indexOf(char);
var newposition = position + key;
if (newposition>alpha.length-1){
newposition = newposition-alpha.length;
}
var newalpha = alpha[newposition];
output = output + newalpha;
console.log("Old letter:" '+ char +' "New Letter:" + newalpha);

}
console.log ("The text" + 'sent' + "has been encrypted to" '+ output +');
};
var decrypt = function (sent){
var sent = prompt("please enter a sentence!");
sent = sent.toLowerCase();
var key = parseInt(prompt("Please enter a shift"));
var output = "";
for (var i=0;ivar char = sent [i];
var position = alpha.indexOf(char);
var newposition = position - key;
if (newposition<0){
newposition = newposition + alpha.length;
}
var newalpha = alpha [new position]
output = output +newalpha;
//console.log(“Old letter:” + char + “New Letter:” +newalpha);

}
console.log("The text" '+sent+' "has been decrypted to" '+ output +');
};
encrypt();
decrypt();

thank you!


#2

Here this should work. will explain your errors shortly

var alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ""];

var encrypt = function(sent){

    var ask = prompt("Please enter a sentence!");
    sent = sent.toLowerCase();
    var key = parseInt(prompt("Please enter a key"));
    var output = "";
    for (var i=0;i<sent.length;i++){
        var char = sent[i];
        var position = alpha.indexOf(char);
        var newposition = position + key;
        if (newposition>alpha.length-1){newposition = newposition-alpha.length;}
        var newalpha = alpha[newposition];
        output = output + newalpha;
        console.log("Old letter:  '"+ char +"'New Letter:" + newalpha);
    }
    console.log ("The text '" + sent + "' has been encrypted to '" + output +"'");
};
var decrypt = function(sent){
    var sent = prompt("please enter a sentence!");
    sent = sent.toLowerCase();
    var key = parseInt(prompt("Please enter a shift"));
    var output = "";
    for (var i=0;i<sent.length;i++){
            var char = sent[i];
            var position = alpha.indexOf(char);
            var newposition = position - key;
            if (newposition<0){newposition = newposition + alpha.length;}
            var newalpha = alpha [newposition] 
            output = output +newalpha;
            //console.log(“Old letter:” + char + “New Letter:” +newalpha);

    }
console.log("The text '" +sent+ "' has been decrypted to '"+ output +"'");
};
encrypt("The text");
decrypt("The text");

Pointer you need to reevaluate your code design(make it better)

sample 1

Considering the fact that you are prompting the user then perhaps this will do

var alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ""];

var encrypt = function(){

    var ask = prompt("Please enter a sentence!");
    sent = ask.toLowerCase();
    var key = parseInt(prompt("Please enter a key"),10);
    var output = "";
    for (var i=0;i<sent.length;i++){
        var char = sent[i];
        var position = alpha.indexOf(char);
        var newposition = position + key;
        if (newposition>alpha.length-1){newposition = newposition-alpha.length;}
        var newalpha = alpha[newposition];
        output = output + newalpha;
        console.log("Old letter:  '"+ char +"'New Letter:" + newalpha);
    }
    console.log ("The text '" + sent + "' has been encrypted to '" + output +"'");
};
var decrypt = function(){
    var ask = prompt("please enter a sentence!");
    sent = ask.toLowerCase();
    var key = parseInt(prompt("Please enter a shift"),10);
    var output = "";
    for (var i=0;i<sent.length;i++){
        var char = sent[i];
            var position = alpha.indexOf(char);
            var newposition = position - key;
            if (newposition<0){newposition = newposition + alpha.length;}
            var newalpha = alpha [newposition] ;
            output = output +newalpha;
            //console.log(“Old letter:” + char + “New Letter:” +newalpha);

    }
console.log("The text '" +sent+ "' has been decrypted to '"+ output +"'");
};
encrypt();
decrypt();

sample 2

var alpha = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", ""];

var encrypt = function(sent){

    //var ask = prompt("Please enter a sentence!");
    sent = sent.toLowerCase();
    var key = parseInt(prompt("Please enter a key"),10);
    var output = "";
    for (var i=0;i<sent.length;i++){
        var char = sent[i];
        var position = alpha.indexOf(char);
        var newposition = position + key;
        if (newposition>alpha.length-1){newposition = newposition-alpha.length;}
        var newalpha = alpha[newposition];
        output = output + newalpha;
        console.log("Old letter:  '"+ char +"'New Letter:" + newalpha);
    }
    console.log ("The text '" + sent + "' has been encrypted to '" + output +"'");
};
var decrypt = function(sent){
    //var ask = prompt("please enter a sentence!");
    sent = sent.toLowerCase();
    var key = parseInt(prompt("Please enter a shift"),10);
    var output = "";
    for (var i=0;i<sent.length;i++){
        var char = sent[i];
            var position = alpha.indexOf(char);
            var newposition = position - key;
            if (newposition<0){newposition = newposition + alpha.length;}
            var newalpha = alpha [newposition] ;
            output = output +newalpha;
            //console.log(“Old letter:” + char + “New Letter:” +newalpha);

    }
console.log("The text '" +sent+ "' has been decrypted to '"+ output +"'");
};
encrypt("Caesar Cipher at work");
decrypt("Caesar Cipher at work");

You also need to work on your decryption algorithm.You must also put in place handlers for wrong/invalid inputs


#3

If you had to create a flow chart for said code, how would you do so?
Moreover, what would an explanation of the code look like? I'm kinda confused :stuck_out_tongue: