8
2
The Setup
You are given a simple web page with 11 elements:
- 10
input
elements with IDsi1
thrui10
, in order - one
output
element with IDout
The input elements have value
attributes defined in the HTML source. The value of any given input may be any integer from 0
to 10
inclusive.
The webpage is equipped with the core jQuery 1.10.1 library (as seen in the fiddle), and executes a block of code as soon as the DOM is loaded.
The Challenge
Six specific challenges are presented below. In each case the objective is to compute some function of the input
s and place the computation result into the inner HTML of the output
. Each challenge should be solved independently from the others. The solution to a challenge is the block of code that implements the computation/output (e.g. the code in the "Javascript" window in the fiddle). The length of a solution is the length (in bytes) of this block of code.
All this would seem very simple, if not for some rather interesting restrictions.
Your Code May...
invoke the jQuery function
$()
and pass in argumentsdefine and use variables
use
this
read any property of any jQuery object (with
.length
being the most useful)define functions/lambdas, which may subsequently be invoked, stored in variables, and passed as arguments. Functions may accept arguments and
return
values if needed.invoke any of the jQuery DOM traversal methods
invoke any of the jQuery DOM manipulation methods, excepting
width
,height
,innerWidth
,innerHeight
,outerWidth
,outerHeight
,offset
,position
,replaceAll
,replaceWith
,scrollLeft
,scrollTop
,css
,prop
,removeProp
, which may not be invokeduse the operators: object creation
{}
; array creation / index reference / field reference[]
, function/method invocation()
, string concatenation+
, and assignment=
use string literals
Your Code May Not...
use any operators except for those listed above
use any literals that are not string literals
invoke any function/method other than those specifically excepted above
use any control structure or keyword (e.g.
for
,while
,try
,if
,with
, etc.), exceptingthis
,var
,let
, functions and lambdasmanipulate the DOM in any way that results in the injection of code (see more below)
access any non-user-defined variable or non-user-defined field/property excepting those listed above
The 6 Challenges
Compute the sum of all
input
values that are even, placing the result in the inner HTML of theoutput
.Compute the maximum of all
input
values, placing the result in the inner HTML of theoutput
.Compute the product of all
input
values<= 2
, placing the result in the inner HTML of theoutput
. If all input values are> 2
, place0
into the inner HTML of theoutput
.Compute the modal value (i.e. the value with greatest frequency) of all
input
values, placing the result in the inner HTML of theoutput
. If the modal value is not unique, place any one of the modal values in the inner HTML of theoutput
.Let
I1
be the value of inputi1
,I2
be the value of inputi2
, etc. If the sequence of input valuesI1
..I10
forms a fence withI1 < I2
, place"TRUE"
into the inner HTML out theoutput
; otherwise place"FALSE"
into the inner HTML of the output. Specifically, the fence condition isI1 < I2 > I3 < I4 > I5 < I6 > I7 < I8 > I9 < I10
.Place a comma-separated list of all
input
values, sorted in ascending order, into the inner HTML of theoutput
.
Scoring
The contest winner is the programmer who submits correct solutions to the greatest number of challenges. In the event of a tie, the winner is the programmer with the lowest total solution length (the sum of the lengths of all solutions). Hence this is a minor variant of code golf.
Important Notes
Solutions may mangle the DOM (e.g. delete inputs
, create new elements that appear as visual detritus) so long as the final state of the DOM contains an output
element with ID out
and the correctly-computed value.
Solutions may make use of any advanced jQuery selectors and CSS3, excepting features that evaluate expressions or execute code.
Solutions may not modify the HTML source of the document. All DOM manipulation must occur in the script through jQuery.
Solutions may not inject code of any kind during DOM traversal/manipulation. This includes (but is not limited to) writing out script
elements, writing out event attributes containing code, or exploiting the expression
(IE) or calc
features of CSS3. This challenge is about creative thinking using sets and trees, and masterful use of jQuery; it is not about sneaking code into the DOM or making end runs around the operator restrictions. I reserve the right to disqualify any solution on this basis.
All solutions are realizable, and each can be implemented in fewer than 400 bytes. Your solutions may of course exceed 400 bytes or be far shorter than 400 bytes. This is just my basic guarantee that all 6 problems are solvable using a reasonably small amount of code.
Finally: When in doubt, please ask. :)
Example
Consider the hypothetical challenge: "If 3 or more input
values equal 5, place "TRUE"
into the inner HTML of the output
; otherwise place "FALSE"
into the inner HTML of the output
."
One valid solution is:
F = () => $('body').append( '<a>TRUE</a>' );
$('input').each( F );
F();
$('a:lt(3)').html( 'FALSE' );
$('#out').html( $('a:eq(' + $('input[value="5"]').length + ')') );
May the best jQuerier win! ;)
15-1 not enough jQuery – grc – 2014-10-03T10:06:48.170
3I'd totally use the answers to this question as production code followed by
//La garantia soy yo
– William Barbosa – 2014-10-03T14:57:38.880Since the input fields are technically strings and it doesn't say we need to treat it as numbers, does "ascending order" mean alphabetically? – Ingo Bürk – 2014-10-03T16:48:42.133
1@IngoBürk: The
input
values will always be (the string representations of) integers from0
to10
inclusive. They should be sorted in order of their ascending values when interpreted as integers. This would actually produce the same order as a lexicographic sort, with the exception that10
would come immediately after1
in the latter. That should not be the case for a sort here. – COTO – 2014-10-03T16:54:31.537Alright. Also, something slightly confusing: You write only string literals are allowed, but also that [] and {} are allowed. But these are, technically speaking, array and object literals. It's clear what you mean, though. – Ingo Bürk – 2014-10-03T16:57:49.037
Indeed. I've characterized these as admissible operators rather than admissible literals, even though they're operators that create literals. – COTO – 2014-10-03T17:04:01.213
Are the inputs following a 0 based index ? Which makes the first and third input on the page as even inputs (and likewise) – Optimizer – 2014-10-05T11:14:22.087
@COTO - As array creation is allowed, can I also use array methods like
push
etc ? – Optimizer – 2014-10-05T12:19:25.143@COTO - Also , one last thing, can I create new strings like
a = '';
and then latest doa+='B'
? – Optimizer – 2014-10-05T12:26:00.427@Optimizer: Zero indexing is a mixed bag. If you're working with arguments, arrays, etc., indices are 0-based. Hence
$('input').eq(1)
would filter down to the second element, andx[1]
would select the second element. However, in CSS indexing is 1-based. Hence$('input:eq(1)')
would select the first element, and$('a:nth-child(3)')
would select the 3rd child, etc. – COTO – 2014-10-05T14:33:47.960Yeah, but what do you expect ? – Optimizer – 2014-10-05T14:36:17.670
@Optimizer: You can create arrays, but you cannot use any array methods. I've allowed array and object creation since they facilitate variable storage, but none of their greater functionality should be used. Finally, you can certainly create new strings like
a = '';
, the+=
operator is off-limits, but you can usea = a + 'B';
if needed. – COTO – 2014-10-05T14:37:28.527Also, I get now what you're asking. The "even inputs" are 1-based, hence the second, fourth, sixth, eighth, and tenth are even. The first input is 1. Their IDs indicate their indices. – COTO – 2014-10-05T14:38:42.860
Let us continue this discussion in chat.
– Optimizer – 2014-10-05T18:02:57.883One of the answers accesses the
length
property of arrays and strings. Those are definitely not jQuery objects. Note: "non-user-defined field/property excepting those listed above". If accessing thelength
property of any object / all objects of a predefined type is allowed, it should be made clear in the rules. – GOTO 0 – 2014-10-06T05:18:44.997@GOTO0: The
.length
property of any non-jQuery object is not permitted. I've notified the poster. I've also addressed the.slice()
method being invoked on a string (not permitted). – COTO – 2014-10-06T11:33:36.013