0
Inspired by this site, this question could potentially be a doozie. People on StackOverflow often overuse jQuery as an answer. Therefore, we need a way to recognize programmatically if a code snippet uses jQuery.
Given a string (which may or may not contain new lines,) of valid Javascript, determine if jQuery is used.
You determine if jQuery is used if the $
or jQuery
functions are called.
Input quirks:
- Input will always be valid Javascript.
- Input can contain new lines.
- Input can omit a semicolon where it would automatically be inserted
- There will always be only one argument to the jQuery function.
- Dollar signs could exist elsewhere (strings, comments) so be careful.
- No base64 conversion functions will/can be used in input.
- No
eval
will/can be used in input. - Any referenced variable or function can be assumed to be defined, but will be guaranteed not to produce errors.
- Code snippets will always produce either a truthy or a falsey value, there will be no ambiguous code.
- Comments can exist in either form.
Rules:
- No Standard Loopholes
- This is code-golf, the shortest answer wins.
- You must write a program or a function that takes a string as input and returns a truthy value if it uses jQuery, and a falsey value if it does not.
- If someone can provide a valid input that breaks your code or causes it to return an incorrect value, it is not a winning answer.
Example Test cases:
include, but are not limited to:
$('div') // true
console.log("$5.99"); // false
$ += 3; // false, the `$` variable clearly has a different value type here
var a = "span" /* this is a comment */
$(a).each(function(){
jQuery(this);
}); // true
Good luck, and remember, everything's better with a little extra jQuery!
So if I understand this right,
if(0){ $('div') }
returns false since the$
function is never actually called? – jrich – 2016-05-14T04:15:58.263Also, may we assume that every program halts? (i.e. no
while(1); $('div')
) – jrich – 2016-05-14T04:19:53.530Just to clarify, we don't have to handle things like
$.get()
, right? – Maltysen – 2016-05-14T04:23:07.5234
Note that we discourage bonuses in code golf challenges
– Alex A. – 2016-05-14T04:35:40.493jrich, that is correct, and you may assume that programs halt. Maltysen, you do not have to handle a scenario where the jQuery function is not directly called. Alex, noted, removing bonuses. – Sam Weaver – 2016-05-14T13:24:28.017
What should I add to make this question more clear? – Sam Weaver – 2016-05-14T13:29:00.190
Will all programs be deterministic? I don't think we can reasonably be expected to handle something like
if(Math.random() < 0.5) $('div')
– jrich – 2016-05-14T15:50:14.080"Code snippets will always produce either a truthy or a falsey value, there will be no ambiguous code." – Sam Weaver – 2016-05-14T16:14:56.557
If I'm correctly understanding your answers to jrich, you're requiring answers to solve the halting problem. – Peter Taylor – 2016-05-14T18:52:56.683
@PeterTaylor It means that you won't have to solve the halting problem; all programs given will halt. – LegionMammal978 – 2016-05-14T18:57:53.607
@LegionMammal978, "that is correct seems to be the reply to "
if(0) { $('div') }
returns false". By replacing0
with a more complex condition you can make it require solving the halting problem. – Peter Taylor – 2016-05-14T19:01:58.5302@LegionMammal978, ok, so pick a non-trivial problem, diagonalise it, and use
$
only if the solution found is even. Assuming that it finds a solution doesn't give you enough information. – Peter Taylor – 2016-05-14T19:32:45.410@Legion understand, this is asking me to write a program which evaluates the result of arbitrary JS code, or perhaps parses it into an abstract syntax tree and decides what the outcome will be. This rather requires a) JavaScript or a language written in JS or b) C++ with libjavascript. – cat – 2016-05-14T23:32:52.157
@LegionMammal978 Bah, humbug. *Closed as off-topic -- questions asking us to find or recommend a book, tool or software library are off-topic on PPCG* :D – cat – 2016-05-15T00:50:57.273