Java is to JavaScript as Car is to Carpet
Java/JavaScript Polyglot, 108 107 106 bytes
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Run as Java
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Try it online!
Note: don't trust the highlight as it's incorrect. The real Java, properly interpreted looks like below because \u000A
is interpreted in the very first step of the compilation as \n
, de facto ending the comment that started with the line comment (//
).
//
s->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Run as JavaScript
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
Credits to @CowsQuak for the JS version.
let f=
//\u000As->s.matches("(?i)(?!.*javascript)java.*"/*
a=>/(?!.*javascript)^java/i.test(a/**/)?"car":"carpet"
var a=["java","javafx","javabeans","java-stream","java-script","java-8","java.util.scanner","javascript","java-avascript","javascript-events","facebook-javascript-sdk","javajavascript","jquery","python","rx-java","java-api-for-javascript","not-java"];
for(var s of a) console.log(s.padStart(a.reduce((x,y)=>x.length>y.length?x:y).length) + "=>" + f(s));
How many imaginary bonus points for this answer?
-1 byte thanks to @Nevay in the Java answer.
JavaScript, 50 49 bytes
Saved 1 byte thanks to @ValueInk by rearranging the regex
a=>/javascript|^(?!java)/i.test(a)?"car":"carpet"
Test snippet
let f=
a=>/javascript|^(?!java)/i.test(a)?"carpet":"car"
var a=["java","javafx","javabeans","java-stream","java-script","java-8","java.util.scanner","java-avascript","javascript","javascript-events","facebook-javascript-sdk","javajavascript","jquery","python","rx-java","java-api-for-javascript","not-java"];
for(var s of a) console.log(s.padStart(a.reduce((x,y)=>x.length>y.length?x:y).length) + "=>" + f(s));
Java (OpenJDK 8), 92 82 72 58 57 bytes
s->s.matches("(?i)(?!.*javascript)java.*")?"car":"carpet"
Try it online!
1 byte saved thanks to @Nevay!