Skip to main content
Commonmark migration
Source Link

#JavaScript ES6, 35 bytes

JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

Alternate version, also 35 bytes

s=>s.replace(/\d+/g,d=>t+=+d,t=0)|t

Another alternate version, 36 bytes

s=>s.split(/\D/).map(d=>t+=+d,t=0)|t

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

Alternate version, also 35 bytes

s=>s.replace(/\d+/g,d=>t+=+d,t=0)|t

Another alternate version, 36 bytes

s=>s.split(/\D/).map(d=>t+=+d,t=0)|t

JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

Alternate version, also 35 bytes

s=>s.replace(/\d+/g,d=>t+=+d,t=0)|t

Another alternate version, 36 bytes

s=>s.split(/\D/).map(d=>t+=+d,t=0)|t
added alternates
Source Link
ETHproductions
  • 50.1k
  • 6
  • 93
  • 240

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

Alternate version, also 35 bytes

s=>s.replace(/\d+/g,d=>t+=+d,t=0)|t

Another alternate version, 36 bytes

s=>s.split(/\D/).map(d=>t+=+d,t=0)|t

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

Alternate version, also 35 bytes

s=>s.replace(/\d+/g,d=>t+=+d,t=0)|t

Another alternate version, 36 bytes

s=>s.split(/\D/).map(d=>t+=+d,t=0)|t
added 93 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 93
  • 240

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

#JavaScript ES6, 35 bytes

s=>eval(s.replace(/\D+/g,'+')+'.0')

How it works

First, we replace each string of non-digits with "+". There are basically four different ways that this could end up:

1. 1b23c456   => 1+23+456
2. a1b23c456  => +1+23+456
3. 1b23c456d  => 1+23+456+
4. a1b23c456d => +1+23+456+

Cases 1 and 2 are taken care of already. But we somehow need to fix the last + so that it doesn't cause an error. We could remove it with .replace(/\+$,""), but that's too expensive. We could append a 0 to the end, but that would affect the last number if the string does not end with a +. A compromise is to append .0, which is both a valid number on its own and doesn't affect the value of other integers.

Here's a few other values that would work as well:

.0
-0
 +0
-""
-[]
0/10
0e-1
.1-.1
added 693 characters in body
Source Link
ETHproductions
  • 50.1k
  • 6
  • 93
  • 240
Loading
Source Link
ETHproductions
  • 50.1k
  • 6
  • 93
  • 240
Loading