Cipher challenge

Hello, I think I have experienced the worst day today. A break will do me good, no progress for 3hours.

function rot13(str) {

const alpha ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";

let result = "";

while(str.length > 0){

for(let i = 0; i <= 25; i++){

  if(alpha[i] === str[0] && (i > 13)) {

    result += alpha(i-13);

  } if( alpha[i] === str[0] && (i < 13)){

    result += alpha( i + 13);

  } 

}

  return result;

}

}

console.log
1 Like

It happens to the best, keep striving , take a break and then keep striving.
Do you need help?

1 Like

Hello, I need serious help. Woke up to crack it, but woe unto me, I still at the same point.

1 Like

hey first, why are you using two loops?
i think just one would be okay to iterate through it.
this challenge requires you to basically replace a character for another character 13 characters ahead, so you need a way to check this characters and then replace them with characters 13 places ahead.
use of an array would be great at this point and much easier.
You could have alpha as an array of alphabets and then str being an input, you can then iterate through the str check the alphabet and then check the index of the same alphabet in the alpha array and return the alphabet in that index +13, or conversely you can have alpha as an object where an the key-value pairs are an alphabet: alphabet 13 characters ahead. After checking you can push them to the results

I will appreciate because for me it is making sense, resulted to manual shifting of the letters. But this code can’t run.

In my second attempt I have done away with the while loop, yet still not running.

4 th line and 7 th line seems wrong

Try using arrays, you can make result to be an empty array where you will push the new letters

Hello, I took a break. This is a headache
const cipherArr = [
[B, O],

[C, P],

[D, Q],

[E, R],

[F, S],

[G, T],

[H, U],

[I,V],

[J, W],

[K, X],

[L, Y],

[M, Z]

];

Function rot13 = (str) {

let newStr = str.split("");

for(var i = 0; i < newStr.length;i++){

newStr[i] === cipherArr[i][0]) {

newStr[i] = cipherArr[i][1];

}

else if( newStr[i] ===cipherArr[i][0]){

newStr[i]=cipherArr[i][0];

}

}

}

return newStr.join("");

console.log(rot13());