Skip to main content
Commonmark migration
Source Link

#JavaScript (ES6), 43 bytes

JavaScript (ES6), 43 bytes

###Why doing it this way?

Why doing it this way?

###Commented

Commented

#JavaScript (ES6), 43 bytes

###Why doing it this way?

###Commented

JavaScript (ES6), 43 bytes

Why doing it this way?

Commented

added a link to another version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

For example, this would work for 45 bytes45 bytes:

ItOr this would work for 44 bytes, if a leading comma is acceptable:

f=(n=100)=>n?f(n-1)+(/2/.test(n)?'':[,n]):''

All in all (and until proven otherwise), it turns out to be shorter to skip right away all values of \$n\$ that contain a \$2\$.

For example, this would work for 45 bytes:

It turns out to be shorter to skip all values of \$n\$ that contain a \$2\$.

For example, this would work for 45 bytes:

Or this would work for 44 bytes, if a leading comma is acceptable:

f=(n=100)=>n?f(n-1)+(/2/.test(n)?'':[,n]):''

All in all (and until proven otherwise), it turns out to be shorter to skip right away all values of \$n\$ that contain a \$2\$.

added an explanation
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

###Why doing it this way?

We could iterate from \$1\$ to \$100\$ and test each number with /2/.test(n), which is a rather concise statement. But in this scenario, we'd have to handle empty entries with something like (/2/.test(n)?'':...), which adds a couple more bytes.

For example, this would work for 45 bytes:

f=(n=1)=>n>99?n:(/2/.test(n)?'':[n,,])+f(n+1)

It turns out to be shorter to skip all values of \$n\$ that contain a \$2\$.

###Commented

###Commented

###Why doing it this way?

We could iterate from \$1\$ to \$100\$ and test each number with /2/.test(n), which is a rather concise statement. But in this scenario, we'd have to handle empty entries with something like (/2/.test(n)?'':...), which adds a couple more bytes.

For example, this would work for 45 bytes:

f=(n=1)=>n>99?n:(/2/.test(n)?'':[n,,])+f(n+1)

It turns out to be shorter to skip all values of \$n\$ that contain a \$2\$.

###Commented

added a commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading