Skip to main content
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###How it works

Unlike Python, the JS modulo operator returns a number having the same sign as the dividend rather than the divisor. Also, the JS repeat() method throws an error when given a negative number, rather than returning an empty string (and it's significantly longer than a simple * anyway).

These are rather unfavorable behaviors for this challenge. So, we'd better identify in which exact case we are rather than relying on math tricks. (Which doesn't mean that such tricks do not exist, but rather that I failed to find them.)

Below is a table describing the 4 possibles cases, where d is the signed distance between the current character and the previous one:

d           | floor(d / 13) + 2 | direction  | distancerepeat
------------+-------------------+------------+---------
-25 ... -14 |         0         |     <      | d + 26
-13 ... -1  |         1         |     >      | -d  
 +0 ... +12 |         2         |     <      | +d  
+13 ... +25 |         3         |     >      | 26 - d

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###How it works

Unlike Python, the JS modulo operator returns a number having the same sign as the dividend rather than the divisor. Also, the JS repeat() method throws an error when given a negative number, rather than returning an empty string (and it's significantly longer than a simple * anyway).

These are rather unfavorable behaviors for this challenge. So, we'd better identify in which exact case we are rather than relying on math tricks. (Which doesn't mean that such tricks do not exist, but rather that I failed to find them.)

Below is a table describing the 4 possibles cases, where d is the signed distance between the current character and the previous one:

d           | floor(d / 13) + 2 | direction  | distance
------------+-------------------+------------+---------
-25 ... -14 |         0         |     <      | d + 26
-13 ... -1  |         1         |     >      | -d  
 +0 ... +12 |         2         |     <      | +d  
+13 ... +25 |         3         |     >      | 26 - d

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###How it works

Unlike Python, the JS modulo operator returns a number having the same sign as the dividend rather than the divisor. Also, the JS repeat() method throws an error when given a negative number, rather than returning an empty string (and it's significantly longer than a simple * anyway).

These are rather unfavorable behaviors for this challenge. So, we'd better identify in which exact case we are rather than relying on math tricks. (Which doesn't mean that such tricks do not exist, but rather that I failed to find them.)

Below is a table describing the 4 possibles cases, where d is the signed distance between the current character and the previous one:

d           | floor(d / 13) + 2 | direction | repeat
------------+-------------------+-----------+-------
-25 ... -14 |         0         |     <     | d + 26
-13 ... -1  |         1         |     >     | -d  
 +0 ... +12 |         2         |     <     | +d  
+13 ... +25 |         3         |     >     | 26 - d

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

added the 'how it works' section
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###How it works

Unlike Python, the JS modulo operator returns a number having the same sign as the dividend rather than the divisor. Also, the JS repeat() method throws an error when given a negative number, rather than returning an empty string (and it's significantly longer than a simple * anyway).

These are rather unfavorable behaviors for this challenge. So, we'd better identify in which exact case we are rather than relying on math tricks. (Which doesn't mean that such tricks do not exist, but rather that I failed to find them.)

Below is a table describing the 4 possibles cases, where d is the signed distance between the current character and the previous one:

d           | floor(d / 13) + 2 | direction  | distance
------------+-------------------+------------+---------
-25 ... -14 |         0         |     <      | d + 26
-13 ... -1  |         1         |     >      | -d  
 +0 ... +12 |         2         |     <      | +d  
+13 ... +25 |         3         |     >      | 26 - d

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###How it works

Unlike Python, the JS modulo operator returns a number having the same sign as the dividend rather than the divisor. Also, the JS repeat() method throws an error when given a negative number, rather than returning an empty string (and it's significantly longer than a simple * anyway).

These are rather unfavorable behaviors for this challenge. So, we'd better identify in which exact case we are rather than relying on math tricks. (Which doesn't mean that such tricks do not exist, but rather that I failed to find them.)

Below is a table describing the 4 possibles cases, where d is the signed distance between the current character and the previous one:

d           | floor(d / 13) + 2 | direction  | distance
------------+-------------------+------------+---------
-25 ... -14 |         0         |     <      | d + 26
-13 ... -1  |         1         |     >      | -d  
 +0 ... +12 |         2         |     <      | +d  
+13 ... +25 |         3         |     >      | 26 - d

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

saved 2 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649

##JavaScript (ES6), 118 109109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=xd=~~s-(x=parseInts=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c,x=0)

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=xd=~~s-(x=parseInts=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c,x=0)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

##JavaScript (ES6), 118 109 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=x-(x=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c,x=0)

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=x-(x=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c,x=0)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

##JavaScript (ES6), 118 109 107 bytes

The input string is case insensitive.

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

###Test cases

let f =

s=>s.replace(/./g,(c,i)=>(d=~~s-(s=parseInt(c,36)),i)?'<><>'[k=d/13+2|0].repeat([d+26,-d,d,26-d][k])+'*':c)

console.log(f("abcdef"));
console.log(f("zyaf"));
console.log(f("zzzzzz"));
console.log(f("z"));
console.log(f("zm"));
console.log(f("zl"));
console.log(f("alphabet"));
console.log(f("banana"));
console.log(f("abcdefghijklmnopqrstuvwxyz"));
console.log(f("abcdefz"));

saved 9 bytes
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 649
Loading