Follow the Logic

Discussion of challenges you have already solved
Post Reply
sjoemelfreek
Posts: 15
Joined: Sun Sep 14, 2008 4:49 pm

Follow the Logic

Post 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?
tails
Posts: 191
Joined: Tue Jun 10, 2008 7:51 pm
Location: Tokyo

Post 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.
sjoemelfreek
Posts: 15
Joined: Sun Sep 14, 2008 4:49 pm

Post 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?
sigi
Posts: 37
Joined: Sun Oct 26, 2008 4:58 pm

Post 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).
User avatar
adum
Posts: 392
Joined: Thu Apr 19, 2007 12:49 pm
Contact:

Post by adum »

i think my leaving the parentheses off the a.reverse was a brilliant typo =)
error213
Posts: 4
Joined: Wed Oct 15, 2008 12:00 pm

Post 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>
will.i.am
Posts: 2
Joined: Thu Mar 05, 2009 2:19 pm

Post 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)
Nestiiii
Posts: 1
Joined: Tue Mar 31, 2009 12:24 pm

Post by Nestiiii »

So a.reverse; gives a "function-pointer" and we do nothing with it. Is this correct?
loderstone
Posts: 1
Joined: Mon Jun 29, 2009 4:46 pm

Post 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 ^^.
vogel
Posts: 4
Joined: Tue Oct 28, 2008 5:02 pm

Post 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
moose
Posts: 67
Joined: Fri Jul 16, 2010 7:32 pm

Post 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
rain1024
Posts: 27
Joined: Sat Jul 23, 2011 5:20 pm

Re: Follow the Logic

Post 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.
froest2012
Posts: 5
Joined: Sat Dec 03, 2011 11:25 am

Post 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);"
eskombro
Posts: 4
Joined: Thu Mar 03, 2016 3:36 am

Post 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 :)
Post Reply