Skip to main content
Commonmark migration
Source Link

#JavaScript (ES6), 45 bytes

JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if n coerced to a Number is neither equal to 0 nor NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator because it's either "0" or
                      //   an expression that already contains 'b'

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if n coerced to a Number is neither equal to 0 nor NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator because it's either "0" or
                      //   an expression that already contains 'b'

JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if n coerced to a Number is neither equal to 0 nor NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator because it's either "0" or
                      //   an expression that already contains 'b'
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if the numeratorn coerced to a Number is notneither equal to 0 ornor NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator because it's either "0" or
                      //   an expression that already contains 'b'

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if the numerator coerced to a Number is not equal to 0 or NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if n coerced to a Number is neither equal to 0 nor NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator because it's either "0" or
                      //   an expression that already contains 'b'
added a commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if the numerator coerced to a Number is not equal to 0 or NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

#JavaScript (ES6), 45 bytes

s=>+(n=s.split`/`[0])?[n*n-1?n:'-'[~n]]+'b':n

Try it online!

###Commented

s =>                  // s = input: "numerator/0"
  +(                  //
    n = s.split`/`[0] // n = numerator, as a string
  ) ?                 // if the numerator coerced to a Number is not equal to 0 or NaN:
    [ n * n - 1 ?     //   if abs(n) is not equal to 1:
        n             //     append the numerator
      :               //   else:
        '-'[~n]       //     append '-' if n = -1, or an empty string otherwise
    ] + 'b'           //   append 'b'
  :                   // else:
    n                 //   just output the numerator
fixed version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading