Creating a function over multiple lines

9

2

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.

theNewfelll

Posted 2018-03-02T23:30:20.057

Reputation: 93

2Related? – Shieru Asakoto – 2018-03-02T23:35:42.090

2

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.

– FantaC – 2018-03-02T23:40:32.117

Also Stack Overflow would be a better place for this, if worded differently

– FantaC – 2018-03-02T23:52:07.937

@tfbninja Thanks for the heads up. I think I just will have to restructure the question. StackOverflow sent me here in the first place. – theNewfelll – 2018-03-03T00:11:31.943

5I think this is fine as a question asking for tips to solve a particular code challenge and have voted to reopen. – Laikoni – 2018-03-03T04:03:52.073

6I 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. – xnor – 2018-03-03T04:09:34.207

Answers

7

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.

ASCII-only

Posted 2018-03-02T23:30:20.057

Reputation: 4 687

Are you not accessing []['map']['constructor'] instead of []['sum']['constructor']? – Jonathan Frech – 2018-03-03T05:35:15.007

Unless I'm reading this wrong, it returns 'barbarians' plural? – cole – 2018-03-03T05:36:24.873

1@cole The question's attempt -- contrary to the question's problem statement -- also seems to output the plural. – Jonathan Frech – 2018-03-03T05:39:26.767

ockquote>

_< sorry typos

– ASCII-only – 2018-03-03T06:16:51.573

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. – theNewfelll – 2018-03-03T17:51:23.040

14

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.

Bubbler

Posted 2018-03-02T23:30:20.057

Reputation: 16 616