Click to See Complete Forum and Search --> : anyone wanna help on a javascript problem?
I'm trying to decipher a javascript code. Here it is:
<SCRIPT LANGUAGE='JavaScript'>
<!--
function speak() {
var codenm = new String(document.codename.name.value);
var i = 0;
var len = codenm.length;
var xor = 1;
var sol = new Array(82, 113, 98, 100, 102, 78, 98, 111);
var good = "Good to see you friend. Let's get on with this mission...";
var bad = "I'm sorry, i dont recognise you. Goodbye.";
var res = 1;
if (len != 8) res = 0;
for(i = 0; i < len; i++) {
if (res) {
if (sol[i] != (codenm.charCodeAt(i) - xor)) res = 0;
if (xor)
xor = -1;
else
xor = 1;
}
}
if (res)
res = good;
else
res = bad;
alert(res);
}
-->
</script>
Hello Agent Astro, please enter the codename : <br>
<form name=codename>
<input type=text name=name value=''>
<input type=button onClick=speak() value="Enter Name">
</form>
What it basically does is ask for input, and you're supposed to input the right one. It all depends of res being one or zero. The key to the problem is probably the if (sol[i] != (codenm.charCodeAt(i) - xor)) res = 0; statement. I'm just not sure what charCodeAt() does. I think it should convert the array to a character, but then how can you subtract 1 from a character. Any help would be appreciated.
janet loves bill
09-16-2004, 08:11 PM
http://www.jelnet.co.uk/cv/jeljava/!jeljava.htm
http://dmoz.org/Computers/Programming/Languages/JavaScript/Tutorials/
wargames are for learning.................if we tell you the answer, then
you don't learn anything............here's some tutorial links to help you
out............
bwkaz
09-16-2004, 08:16 PM
charCodeAt() returns the character code at the appropriate position in the string. I assume it returns the Unicode representation, since ECMAScript's default character set is Unicode.
You could always Google for "charCodeAt", and probably find a decent description of what exactly it does.
I'm not asking you guys to tell me the answer. I was hoping that someone could help me out with a hint. Actually, let me rephrase my question. I realized that this has to be converted to ASCII. And I also realized that there have to be 8 letters. But the problem then is, that i starts with 0, then increments to 1 right away, so when it goes into the loop, it's set with a value of 1, which means that it gets the 1st value of the array, which is 113, not 82, because the array starts from zero. But then, i only iterates to 7, so that means that 7 characters are produced instead of 8. What happens to 82? Can anyone give me a hint or tell me if I'm on the right track?
yeah, all charCodeAt does is convert a character into its unicode equivalent. So, it should be 83, 112, 99, 99, 103, 77, 99, and 110 converted into Unicode, but it doesn't work, probably because it starts with 112.
bryan.6
09-16-2004, 09:12 PM
But the problem then is, that i starts with 0, then increments to 1 right away, so when it goes into the loop, it's set with a value of 1,
looks like you're going to have to do a bit of reading on for loops too. the first iteration, i will be 0.
X_console
09-16-2004, 10:08 PM
Why don't you write a JavaScript that looks like this:
var message = "Hello World";
var i = 0;
for (i = 0; i < message.length; i++)
alert(message.charCodeAt(i));
So what happens here? "i" is a counter that starts at 0, which is the starting index for Strings. In this case, "Hello World" is a String. charCodeAt(n) is a method in the String class that returns the Unicode representation of the character at position "n".
So in essence what happens is it will go through the entire String and print out the Unicode representation of each character in that String until the String ends.
Now for the code that's given to you, it makes the following comparison:
if (sol[i] != (codenm.charCodeAt(i) - xor))
"sol" is an array that contains Unicode variable. "codenm" is the String input you entered through the textfield. "xor" is an integer with the value 1. So each character in the String you entered is compared with each value in the array. If you entered "hacker" for instance, the unicode value of "h" is obtained, and is subtracted by 1. This value is then compared to the first element in the array "sol", in this case: 82. If at anytime the Unicode value of the "i"th index of the array is not equal to the Unicode value of the "i"th character, then "res" is forever set to 0 (aka: false).
OK, I figured it out, but it was weird. What I did was basically guess, because I put a alert(res) right after the for loop so then I plugged in every letter individually and if it gave out a zero, I was wrong and if it was a one, I was right. Now that I solved it I understand it. The only number that increased was 82, because it was the only one where the value of xor was one. After every single time after that, xor was -1. I failed to see that and I thought it alternated, meaning it went 83, 112, 99, 99, 103, 77, 99, 110. Gotta pay attention to detail!!!