Page 1 of 1

Follow the Logic

Posted: Fri Oct 17, 2008 10:01 am
by sjoemelfreek
Hello!

The code:

function ff(answer) {
var x = 2;
var y = 5;
var z = 3;
var a = new Array(6, 5, 4, 9, 3, 7);
var v = y * a[y] + z * a[z] - y * x * z + a[y] * a[x] * a[3];
a.reverse;
v = v + a[z] + a[y] + 11 * a[1] + a[0] - 1;
if (answer == v) {
return true;
}
else {
alert("not it!");
return false;
}

First I was frustrated because no matter how I reversed a, either by reversing it's array or by
reversing it in this manner: 0 = 9, 1 = 8, 2 = 7 (I don't know how you would call this)...
I wouldn't get the answer. Then I ignored it because I thought that maybe the statement was incomplete. This gave me the answer. Was this meant as a trick question?

How is it written so that it does work?

Posted: Fri Oct 17, 2008 10:37 am
by tails
Interesting trick!
a.reverse() would be a function call, but a.reverse is just a function.

To solve this challenge, you have only to insert "alert(v);" into the script and press submit to see the value.

Posted: Fri Oct 17, 2008 6:37 pm
by sjoemelfreek
I didn't use a compiler or whatever...
I just wrote down with the instructions of the code.
It seems however that a.reverse is doing nothing;
like tails says: It's just a function...

Mistake or meant to be?

Posted: Mon Nov 03, 2008 11:27 am
by sigi
Meant to be (and very useful).

The value of what you get when you write "a.reverse" is a function. The value of what you get when you write "a.reverse()" is the string "a" reversed. That's a fundamental difference (about as fundamental as "car that drives 1000 miles" is not the same as "trip to Hawaii").

Lets take a different example:

"f(x) = x*x" is a function that squares its argument.

So, after defining it,

"f" refers to a something that squares its parameter. "f" does NOT, however, refer to any square number! The "type" of "f" would be "int -> int" (something that takes an "int" and gives back an "int".

"f(x)", on the other hand applies the above "f" to "x", so it evaluates to "x*x", which effectively means that "f(x)" is the square of "x". The type of "f(x)" is simply "int", because the result of applying "f" is integer.

So, a function value is completely different from the result of applying the function. The former has the type "function ..." while the latter has the type "return type of the function in question". However, this is a very powerful concept, because it allows you to pass actual functions (not their results!) to other functions. It's a big drawback of Java that this is in fact impossible without jumping through a lot of hoops in that language (in most dynamic languages and all functional languages it's easy and straighforward to pass functions around).

Posted: Mon Nov 03, 2008 7:30 pm
by adum
i think my leaving the parentheses off the a.reverse was a brilliant typo =)

Posted: Sun Feb 22, 2009 1:38 pm
by error213
i made an html file i think it's a easiest way to do it

Code: Select all

<html>
<body>
<script type="text/javascript">
var x = 2;
var y = 5;
var z = 3;
var a = new Array(6, 5, 4, 9, 3, 7);
var v = y * a[y] + z * a[z] - y * x * z + a[y] * a[x] * a[3];
a.reverse;
v = v + a[z] + a[y] + 11 * a[1] + a[0] - 1;
document.write(v)
</script>
</body>
</html>

Posted: Fri Mar 06, 2009 7:57 am
by will.i.am
I used my firefox extension "Firebug" to debug this script. I simply set a break point at the first if-statement, typed in a 1 and "debugged" the JavaScript.

No coding and 4 clicks (if firebug allready installed)

Posted: Tue Mar 31, 2009 9:04 pm
by Nestiiii
So a.reverse; gives a "function-pointer" and we do nothing with it. Is this correct?

Posted: Thu Aug 13, 2009 6:15 am
by loderstone
Maybe I should have worked harder.
After all the stuff is done to 'v', I added in:
alert(v);

Printed the answer onto my screen then I typed it in ^^.

Posted: Wed Aug 19, 2009 6:03 pm
by vogel
I used the error console of Firefox:
Ctrl+Shift+J
copy and paste
var x = 2;
var y = 5;
var z = 3;
var a = new Array(6, 5, 4, 9, 3, 7);
var v = y * a[y] + z * a[z] - y * x * z + a[y] * a[x] * a[3];
a.reverse;
v = v + a[z] + a[y] + 11 * a[1] + a[0] - 1;
to the command line and hit "Evaluate"
the answer appears as a message in the error log

I didn't even realised the missing brackets because I hadn't read the code

Posted: Tue Jun 28, 2011 1:33 pm
by moose
@will.i.am: Thanks for the hint. I use Chrome, so the developer-extension is already included (Ctrl + Shift + J). Then change to "Scripts"-> choose "chal.php" and click on the line with (if (answer == v)). Execute the script and look up all variable names on the right. I think I will try some other challenges with this method :D

Re: Follow the Logic

Posted: Sun Jul 31, 2011 2:19 pm
by rain1024
sjoemelfreek wrote:Hello!

The code:

function ff(answer) {
var x = 2;
var y = 5;
var z = 3;
var a = new Array(6, 5, 4, 9, 3, 7);
var v = y * a[y] + z * a[z] - y * x * z + a[y] * a[x] * a[3];
a.reverse;
v = v + a[z] + a[y] + 11 * a[1] + a[0] - 1;
if (answer == v) {
return true;
}
else {
alert("not it!");
return false;
}

First I was frustrated because no matter how I reversed a, either by reversing it's array or by
reversing it in this manner: 0 = 9, 1 = 8, 2 = 7 (I don't know how you would call this)...
I wouldn't get the answer. Then I ignored it because I thought that maybe the statement was incomplete. This gave me the answer. Was this meant as a trick question?

How is it written so that it does work?
Ok, I think it's too open for this answer.

Posted: Wed Dec 07, 2011 5:51 am
by froest2012
a.reverse doesn't work,it is just a joke. :P
So the array after a.reverse is still the original array.
You are cheated.
If you wanna pass it ,just add "alert(v);"

Posted: Tue Mar 08, 2016 3:11 am
by eskombro
I just did copy/paste of the source code. Then I changed the part of the function:

Code: Select all

if (answer == v) {
  return true;
}
else {
  alert("not it!");
  return false;
}
for this:

Code: Select all

alert(v);
And it gave me an alert with the answer :)