Follow the Logic
-
- Posts: 15
- Joined: Sun Sep 14, 2008 4:49 pm
Follow the Logic
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?
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?
-
- Posts: 15
- Joined: Sun Sep 14, 2008 4:49 pm
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).
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).
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>
-
- Posts: 1
- Joined: Mon Jun 29, 2009 4:46 pm
I used the error console of Firefox:
Ctrl+Shift+J
copy and paste
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
Ctrl+Shift+J
copy and paste
to the command line and hit "Evaluate"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;
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
@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
Re: Follow the Logic
Ok, I think it's too open for this answer.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?
-
- Posts: 5
- Joined: Sat Dec 03, 2011 11:25 am
I just did copy/paste of the source code. Then I changed the part of the function:
for this:
And it gave me an alert with the answer
Code: Select all
if (answer == v) {
return true;
}
else {
alert("not it!");
return false;
}
Code: Select all
alert(v);