JavaScript chaining challenge

A forum for good logic/math puzzles.

Moderators: jestingrabbit, Moderators General, Prelates

User avatar
The Snide Sniper
Posts: 65
Joined: Mon Mar 21, 2016 7:25 pm UTC
Location: UTC-6:00

JavaScript chaining challenge

Postby The Snide Sniper » Wed May 18, 2016 2:35 am UTC

The "length" of a chain, for the purposes of this puzzle, is the largest number of operations between an initial value and the statement, so "if(!Array.isArray(input) && shouldBeArray)" has a length of 4, with the respective operations being ".isArray", "(input)", "!", and "&& shouldBeArray". "Array" is not counted because it is an initial value, and "if" is not counted because ot is the statement. ".isArray" and "(input)" are considered seperate operations: the first to acquire the function, and the second to call it.

With that out of the way, the challenge is this: given array arr, write a statement that returns the last value of arr irrespective of length.
Stipulations:
  • Arr has just been acquired and you're chaining off of it, so arr may only be used as a base value once.
  • The statement may not have a length of more than 3.
  • You may not have any additional statements, so no adding methods to the array prototype.
  • Arr is stored elsewhere in the script, so you may not damage the original array.
  • The solution must be inside a [code] tag. Not for BBCode concerns, but for the same-width font.
Last edited by The Snide Sniper on Wed May 18, 2016 12:04 pm UTC, edited 1 time in total.
Also known as "RamenChef" on various other fora.
Me on: Drawception | Explain xkcd | NationStates | The Sluggite Zone | Stack Overflow

User avatar
jaap
Posts: 2087
Joined: Fri Jul 06, 2007 7:06 am UTC
Contact:

Re: JavaScript chaining challenge

Postby jaap » Wed May 18, 2016 5:00 am UTC

Spoiler:

Code: Select all

return arr.reverse()[0];

User avatar
The Snide Sniper
Posts: 65
Joined: Mon Mar 21, 2016 7:25 pm UTC
Location: UTC-6:00

Re: JavaScript chaining challenge

Postby The Snide Sniper » Wed May 18, 2016 12:03 pm UTC

It appears I forgot one of the key stipulations. No hard feelings.
Also known as "RamenChef" on various other fora.
Me on: Drawception | Explain xkcd | NationStates | The Sluggite Zone | Stack Overflow

User avatar
jestingrabbit
Factoids are just Datas that haven't grown up yet
Posts: 5967
Joined: Tue Nov 28, 2006 9:50 pm UTC
Location: Sydney

Re: JavaScript chaining challenge

Postby jestingrabbit » Fri May 20, 2016 11:33 am UTC

I take it that you're saying that something is wrong with jaap's answer, in that it mutates the array.

I'm not sure I understand your length criteria. What would you say the length is of something like

Spoiler:

Code: Select all

arr.reduce( function (total, value){return value;} );
ameretrifle wrote:Magic space feudalism is therefore a viable idea.

User avatar
The Snide Sniper
Posts: 65
Joined: Mon Mar 21, 2016 7:25 pm UTC
Location: UTC-6:00

Re: JavaScript chaining challenge

Postby The Snide Sniper » Fri May 20, 2016 1:24 pm UTC

jestingrabbit wrote:I take it that you're saying that something is wrong with jaap's answer, in that it mutates the array.

I'm not sure I understand your length criteria. What would you say the length is of something like

Spoiler:

Code: Select all

arr.reduce( function (total, value){return value;} );

Technically yes, although I had forgotten to add the "no mutating the array" stipulation.
Spoiler:
That would have a length of 2, (getting and calling the reduce function; the function is an initial value) which is within the boundaries, although it runs afoul of the "one statement only" rule because of the function's return statement.
Also known as "RamenChef" on various other fora.
Me on: Drawception | Explain xkcd | NationStates | The Sluggite Zone | Stack Overflow

User avatar
jestingrabbit
Factoids are just Datas that haven't grown up yet
Posts: 5967
Joined: Tue Nov 28, 2006 9:50 pm UTC
Location: Sydney

Re: JavaScript chaining challenge

Postby jestingrabbit » Fri May 20, 2016 4:46 pm UTC

So that kills anything that takes a callback or doesn't do an (at least shallow) copy, and that leaves

Spoiler:

Code: Select all

arr.slice(-1)[0]
ameretrifle wrote:Magic space feudalism is therefore a viable idea.

operator[]
Posts: 155
Joined: Mon May 18, 2009 6:11 pm UTC
Location: Stockholm, Sweden

Re: JavaScript chaining challenge

Postby operator[] » Fri May 20, 2016 8:11 pm UTC

You could also argue that
Spoiler:
ES6 arrow functions:

Code: Select all

arr.reduce((total, value) => value)
should be okay. Alternatively, you could do:
Spoiler:

Code: Select all

arr.reduceRight(String)

if you know the array consists only of strings, or

Code: Select all

arr.reduceRight(eval)

if it consists only of non-strings (and quines).


Return to “Logic Puzzles”

Who is online

Users browsing this forum: No registered users and 7 guests