Please Help With Encryption

project

#1

Hello, I've had the bright idea to try to write an encryption "algorithm" in JavaScript. I was going to see if anyone could help me with a problem I am running into. Here's my code:

var cade_encryption = function() {
    list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
    vanilla = prompt("Please enter a word to encode", "Example");
    for (i = 0; i < vanilla.length; i++){
        chocolate = [];
        chocolate.push(vanilla[i]);
    }
    for (let1 in chocolate) {
        for (j = 0; j < list.length / 2; j++) {
            if (let1 == list[j]) {
                array1 = [];
                array1.push(list[j + j]);
            }
        }
    }
    final = array1.join("");
    return final
}
cade_encryption();

When I run the code with the prompt being "Cade", I believe that it should return "7agi" but it returns "Y".

Thank you very much for assisting me!

-Cade


#2

Well, you have a problem, look at this code:

var cade_encryption = function() {
    list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
    vanilla = prompt("Please enter a word to encode", "Example");
    for (i = 0; i < vanilla.length; i++){
        chocolate = [];
        chocolate.push(vanilla[i]);
    }
    console.log(chocolate);
    for (let1 in chocolate) {
        for (j = 0; j < list.length / 2; j++) {
            if (let1 == list[j]) {
                array1 = [];
                array1.push(list[j + j]);
            }
        }
    }
    final = array1.join("");
    return final
}
cade_encryption();

the only change i made, is adding a console.log, as you can see, your chocolate is only holding one character, why not use .slice() to make a copy of the array? As done here


#3

I see what you're saying, but the issue I'm is the vanilla isn't an array, but chocolate needs to be.


#4

Ah, you are right. Sorry about that, uhm... But you get what i am saying, right?


#5

Yes, I understand what you're saying. :smile: But I don't understand why my loop isn't adding every character of vanilla?


#6

Give me a minute to figure that out


#7

I've got chocolate to be a correct array by using .split(), but my other for loop is being weird. New Code:

var cade_encryption = function() {
    list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
    vanilla = prompt("Please enter a word to encode", "Example");
    chocolate = vanilla.split("");
    console.log(chocolate);
    for (letter in chocolate) {
        for (j = 0; j < list.length / 2; j++) {
            if (letter == list[j]) {
                array1 = [];
                array1.push(list[j + j]);
            }
        }
    }
    final = array1.join("");
    return final
}
cade_encryption();

#8

Well, i guess we are going to have to debug it in the same fashion, including console.log to see what the program is actually doing


#9

Yes, currently I'm testing other loops of the sort or my second for loop.


#10

I would start by moving array1 = []; to the beginning of the function:

var cade_encryption = function() {
    array1 = [];
    list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
    vanilla = prompt("Please enter a word to encode", "Example");
    chocolate = vanilla.split("");
    console.log(chocolate);
    for (letter in chocolate) {
        for (j = 0; j < list.length / 2; j++){
            if (letter == list[j]) {
                
                array1.push(list[j + j]);
            }
        }
    }
    final = array1.join("");
    return final
}
cade_encryption();

If you don't do this, you create a new array1 each time your loop get executed


#11

Ok, I think I've about got a solution, array1 isn't being pushed to right, so I'm going to use a loop.


#12

Did you see my answer?


#13

Yes! Thank you so much! It works!


#14

Good to hear it works, you understand what was wrong?


#15

Yes, I see now that, everytime my loop ran, I created a new variable, therefore deleting the previous one. Thanks for help! :slightly_smiling: