11
\$\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\$
6
  • 2
    \$\begingroup\$ Related? \$\endgroup\$ Commented Mar 2, 2018 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\$
    – qqq
    Commented Mar 2, 2018 at 23:40
  • \$\begingroup\$ Also Stack Overflow would be a better place for this, if worded differently \$\endgroup\$
    – qqq
    Commented Mar 2, 2018 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
    Commented Mar 3, 2018 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
    Commented Mar 3, 2018 at 4:09

2 Answers 2

16
\$\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\$
9
\$\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\$
5
  • \$\begingroup\$ Are you not accessing []['map']['constructor'] instead of []['sum']['constructor']? \$\endgroup\$ Commented Mar 3, 2018 at 5:35
  • \$\begingroup\$ Unless I'm reading this wrong, it returns 'barbarians' plural? \$\endgroup\$
    – cole
    Commented Mar 3, 2018 at 5:36
  • 1
    \$\begingroup\$ @cole The question's attempt -- contrary to the question's problem statement -- also seems to output the plural. \$\endgroup\$ Commented Mar 3, 2018 at 5:39
  • \$\begingroup\$ >_< sorry typos \$\endgroup\$
    – ASCII-only
    Commented Mar 3, 2018 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\$ Commented Mar 3, 2018 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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