8
\$\begingroup\$

I'm attempting to create a function over multiple lines.

Parameters of the challenge:

  1. Function returns 'barbarian'
  2. Only 2 characters per line
  3. 40 lines maximum. I keep trying to use a fat arrow to declare my function, but fat arrows don't seem to work if you don't have them connected.

Source of Challenge: https://www.codewars.com/kata/multi-line-task-plus-plus-hello-world

My current work:

f=
(
)=>

'\
b\
a\
r\
b\
a\
r\
i\
a\
n\
s\
'

This works, but the ')=>' is 3 characters long. I really just want to know how it is possible to even stretch out the creation of a function. I can't find any info on it anywhere as it's obviously not very practical.

\$\endgroup\$
  • 2
    \$\begingroup\$ Related? \$\endgroup\$ – Shieru Asakoto Mar 2 '18 at 23:35
  • 2
    \$\begingroup\$ Welcome to PPCG! Unfortunately, this challenge does not have an objective winning criterion, and I am voting to close this challenge until rectified. In the future, please use the sandbox to receive feedback on your question before it is posted. \$\endgroup\$ – FantaC Mar 2 '18 at 23:40
  • \$\begingroup\$ Also Stack Overflow would be a better place for this, if worded differently \$\endgroup\$ – FantaC Mar 2 '18 at 23:52
  • 5
    \$\begingroup\$ I think this is fine as a question asking for tips to solve a particular code challenge and have voted to reopen. \$\endgroup\$ – Laikoni Mar 3 '18 at 4:03
  • 6
    \$\begingroup\$ I agree with Laikoni that this is definitely asking for advice on dealing with a code layout constraint (i.e., restricted source) rather than asking a programming question like at SO. \$\endgroup\$ – xnor Mar 3 '18 at 4:09
7
\$\begingroup\$

Here is a 38 line solution:

f=
[]
[
'\
m\
a\
p'
][
'\
c\
o\
n\
s\
t\
r\
u\
c\
t\
o\
r'
]`
r\
e\
t\
u\
r\
n\
'\
b\
a\
r\
b\
a\
r\
i\
a\
n\
'`

It creates a function using the Function constructor, which it accesses from [].map.constructor using subscript notation ([]['map']['constructor']). This is the method JSFuck uses to create functions.

\$\endgroup\$
  • \$\begingroup\$ Are you not accessing []['map']['constructor'] instead of []['sum']['constructor']? \$\endgroup\$ – Jonathan Frech Mar 3 '18 at 5:35
  • \$\begingroup\$ Unless I'm reading this wrong, it returns 'barbarians' plural? \$\endgroup\$ – cole Mar 3 '18 at 5:36
  • 1
    \$\begingroup\$ @cole The question's attempt -- contrary to the question's problem statement -- also seems to output the plural. \$\endgroup\$ – Jonathan Frech Mar 3 '18 at 5:39
  • \$\begingroup\$ >_< sorry typos \$\endgroup\$ – ASCII-only Mar 3 '18 at 6:16
  • \$\begingroup\$ This is great! Is there any way to shave it down to even fewer lines? Just curious at this point as this is the first I have heard of constructors. \$\endgroup\$ – theNewfelll Mar 3 '18 at 17:51
14
\$\begingroup\$

35 Lines

f=
0[
c=
'\
c\
o\
n\
s\
t\
r\
u\
c\
t\
o\
r'
][
c]
`
r\
e\
t\
u\
r\
n\
'\
b\
a\
r\
b\
a\
r\
i\
a\
n'
`

Try it online!

Uses the fact that 0 is a number, the constructor of 0 is Number, and the constructor of Number is Function.

32 Lines

0[
c=
'\
c\
o\
n\
s\
t\
r\
u\
c\
t\
o\
r'
][
c]
`
f=
_\
=>
'\
b\
a\
r\
b\
a\
r\
i\
a\
n'
`(
)

Try it online!

This essentially runs

Function(`
f=
_=>
'barbarian'`)()

which uses the IIFE structure. Added bonus is that we can line-break some parts in the function body to reduce the line count.

24 Lines

f=
''
[
'\
t\
r\
i\
m'
][
'\
b\
i\
n\
d'
]`
b\
a\
r\
b\
a\
r\
i\
a\
n`

Try it online!

Inline version:

f=''['trim']['bind']`
barbarian`

Since all we want is to return a string, we can get away with a string method bound to a string. By using trim, we can also safely leave a beginning newline.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.