HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptMinor

Generate Symmetric Ascii

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
symmetricgenerateascii

Problem

Have you got any suggestions to improve this code?
The idea is to generate a one-line string that is visually symmetric.

Revisions at Github


var display = document.getElementById('display');

var chars = [33,34,39,42,43,45,46,48,58,61,72,73,77,79,84,92,94,95,111,124];
var double_chars = [40,41,47,92,60,62,91,93,123,125];
var double_chars_assoc = {40:41,47:92,60:62,91:93,123:125,
41:40,92:47,62:60,93:91,125:123};

function generateSymmetricAscii() {
    var ascii_string = "";
    var left_side = [];

    for(var i=0; i= 0.5; };
function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i 

Solution

Omit the extra == true in conditions:

if(randomBool())


Use the ternary if operator to compact your code:

left_side.push(randomChoice(randomBool() ? chars : double_chars))


If this seems to dense for you, you can also extract the inner expression like this:

var options = randomBool() ? chars : double_chars;
left_side.push(randomChoice(options))

Code Snippets

if(randomBool())
left_side.push(randomChoice(randomBool() ? chars : double_chars))
var options = randomBool() ? chars : double_chars;
left_side.push(randomChoice(options))

Context

StackExchange Code Review Q#114966, answer score: 4

Revisions (0)

No revisions yet.