10 Replies - 5501 Views - Last Post: 14 October 2010 - 12:53 PM

#1 monkadelicd   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Weird problem with Caesar's Cipher script

Posted 13 October 2010 - 09:50 AM

I've made a simple Caesar's Cipher script using a <textarea> box for input and functions for encoding and decoding. When I run the encode and decode function I'm having trouble with A's and B's being decoded improperly. A's get decoded to X's and B's get decoded to W's. It should be Y and Z respectively.

Any ideas on how to fix this?

Here's my code
<html>
<head>
<title>Encode with Caesar's Cipher</title>
</head>
<body>
<script type="text/javascript">
var toReplace = new Array("x", "X", "w", "W", "v", "V", "u", "U", "t", "T", "s", "S", "r", "R", "q", "Q", "p", "P", "o", "O", "n", "N", "m", "M", "l", "L", "k", "K", "j", "J", "i", "I", "h", 

"H", "g", "G", "f", "F", "e", "E", "d", "D", "c", "C", "b", "B", "a", "A", "y", "Y", "z", "Z");
var replaceWith = new Array("z", "Z", "y", "Y", "x", "X", "w", "W", "v", "V", "u", "U", "t", "T", "s", "S", "r", "R", "q", "Q", "p", "P", "o", "O", "n", "N", "m", "M", "l", "L", "k", 

"K", "j", "J", "i", "I", "h", "H", "g", "G", "f", "F", "e", "E", "d", "D", "c", "C", "b", "B", "a", "A");

function encode(message)
{	var m1 = message;
	var i;
	for (i = 0; i < toReplace.length; i++)
	{	while (m1.indexOf(toReplace[i]) > -1)
			m1 = m1.replace(toReplace[i], replaceWith[i]);
	}
	return cipherForm.message.value = m1;
}

function decode(message)
{	var m1 = message;
	var i;
	for (i = toReplace.length - 1; i > -1; i--)
	{	while (m1.indexOf(replaceWith[i]) > -1)
			m1 = m1.replace(replaceWith[i], toReplace[i]);
	}
	return cipherForm.message.value = m1;
}
</script>
<form name="cipherForm">
<textarea name="message" cols="50" rows="7">Enter the message you want to encode/decode with Caesar's Cipher then click on the corresponding button 

below.</textarea><br />
<input type="button" value="Encode" onclick="encode(cipherForm.message.value);" />
<input type="button" value="Decode" onclick="decode(cipherForm.message.value);" />
</form>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Weird problem with Caesar's Cipher script

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4250
  • View blog
  • Posts: 13,470
  • Joined: 08-June 10

Re: Weird problem with Caesar's Cipher script

Posted 13 October 2010 - 11:10 AM

I think the problem is using the replace function, because you may replace letters, that have been already replaced.

I’d loop through the string and replace the letters one by one.

//# hm, why using 2 arrays?
rotX(letter, shift)
{
	var cc = ["a", "b", "c"]; //# etc.
	var idx = cc.indexOf(letter);
	if (-1 == idx) {
    	return letter;
	}
	return cc[(idx + cc.length + shift) % cc.length];
}

This post has been edited by Dormilich: 13 October 2010 - 11:33 PM

Was This Post Helpful? 1
  • +
  • -

#3 monkadelicd   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 08:43 AM

Ok I've re-written the function. I am having trouble since I added the lines to keep punctuation though. Nothing is getting past the first if statement, therefore nothing is encoded.

var alpha = "abcdefghijklmnopqrstuvwxyz";

function encode1(message)
{	var m1 = message;
	var m2 = "";
	var i;
	var a;
	var b;
	for (i = 0; i < m1.length; i++)
	{	if ( m1.charAt(i) == "!" || "#" || "$" || "%" || "&" || "(" || ")" || "-" || ";" || ":" || "'" || "\"" || "," || "." || "/" || "\\" || "?" || " ")
		{	m2 += m1.charAt(i);
			alert(m2);
		}
		else
		{	a = m1.charAt(i);
			b = parseInt(alpha.indexOf(a));
			if (b + 2 >25)
			{	b -= 26;
			}
			b += 2;
			m2 += alpha.charAt(B)/>;
			alert(m2);
		}
	}
	return cipherForm.message.value = m2;
		
}


This post has been edited by monkadelicd: 14 October 2010 - 08:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4250
  • View blog
  • Posts: 13,470
  • Joined: 08-June 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 08:50 AM

thatís not a correct way to write if conditions. should be mentioned in the error console.
if (x == 1 || x == 2)

line #6: what do you need parseInt() for? the result will be the index number or -1.

line #17-#19: thatís what I used the modulus for.
Was This Post Helpful? 1
  • +
  • -

#5 Jstall   User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 08:50 AM

Your if statement should be structured like :
if ( m1.charAt(i) == "!" || m1.charAt(i) == "#" || m1.charAt(i) == "$")




That would explain your problem with the if statement.
Was This Post Helpful? 1
  • +
  • -

#6 monkadelicd   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 09:12 AM

Ok it works properly now. I just used toLowerCase() instead of worrying about upper and lowercase. Here's the code I came up with. Any errors besides being longhand/old style.
<html>
<head>
<title>Encode with Caesar's Cipher</title>
</head>
<body>
<script type="text/javascript">
var alpha = "abcdefghijklmnopqrstuvwxyz";

function encode(message)
{	var m1 = message.toLowerCase();
	var m2 = "";
	var i;
	var a;
	var b;
	for (i = 0; i < m1.length; i++)
	{	if (m1.charAt(i) == "!" || m1.charAt(i) == "#" || m1.charAt(i) == "$" || m1.charAt(i) == "%" || m1.charAt(i) == "&" || m1.charAt(i) == "(" || m1.charAt(i) == ")" || m1.charAt(i) == "-" || m1.charAt(i) == ";" || m1.charAt(i) == ":" || m1.charAt(i) == "'" || m1.charAt(i) == "\"" || m1.charAt(i) == "," || m1.charAt(i) == "." || m1.charAt(i) == "/" || m1.charAt(i) == "\\" || m1.charAt(i) == "?" || m1.charAt(i) == " ")
		{	m2 += m1.charAt(i);
			//alert(m2);
		}
		else
		{	a = m1.charAt(i);
			b = alpha.indexOf(a);
			if (b + 2 > 25)
			{	b -= 26;
			}
			b += 2;
			m2 += alpha.charAt(B)/>;
			//alert(m2);
		}
	}
	return cipherForm.message.value = m2;
		
}

function decode(message)
{	var m1 = message.toLowerCase();
	var m2 = "";
	var i;
	var a;
	var b;
	for (i = 0; i < m1.length; i++)
	{	if (m1.charAt(i) == "!" || m1.charAt(i) == "#" || m1.charAt(i) == "$" || m1.charAt(i) == "%" || m1.charAt(i) == "&" || m1.charAt(i) == "(" || m1.charAt(i) == ")" || m1.charAt(i) == "-" || m1.charAt(i) == ";" || m1.charAt(i) == ":" || m1.charAt(i) == "'" || m1.charAt(i) == "\"" || m1.charAt(i) == "," || m1.charAt(i) == "." || m1.charAt(i) == "/" || m1.charAt(i) == "\\" || m1.charAt(i) == "?" || m1.charAt(i) == " ")
		{	m2 += m1.charAt(i);
			//alert(m2);
		}
		else
		{	a = m1.charAt(i);
			b = alpha.indexOf(a);
			if (b - 2 < 0)
			{	b += 26;
			}
			b -= 2;
			m2 += alpha.charAt(B)/>;
			//alert(m2);
		}
	}
	return cipherForm.message.value = m2;
		
}
</script>
<form name="cipherForm">
<textarea name="message" cols="50" rows="7">Enter the message you want to encode/decode with Caesar's Cipher then click on the corresponding button below.</textarea><br />
<input type="button" value="Encode" onclick="encode(cipherForm.message.value);" />
<input type="button" value="Decode" onclick="decode(cipherForm.message.value);" />
</form>
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#7 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4250
  • View blog
  • Posts: 13,470
  • Joined: 08-June 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 10:48 AM

I still donít get why you use the if conditions Ö
Was This Post Helpful? 0
  • +
  • -

#8 monkadelicd   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 11:57 AM

All of the if conditions or to which ones are you referring? I'm looking to learn.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4250
  • View blog
  • Posts: 13,470
  • Joined: 08-June 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 12:01 PM

the ones on lines #16 and #42
Was This Post Helpful? 0
  • +
  • -

#10 monkadelicd   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 08-July 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 12:50 PM

I used those if statements to pass punctuation straight through to the encoded message and only letters get encoded. How else could I implement this?
Was This Post Helpful? 0
  • +
  • -

#11 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4250
  • View blog
  • Posts: 13,470
  • Joined: 08-June 10

Re: Weird problem with Caesar's Cipher script

Posted 14 October 2010 - 12:53 PM

post #2, lines #6-#7 (return input letter if not in "cipher table")

This post has been edited by Dormilich: 14 October 2010 - 12:54 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1