[2016-11-02] Challenge #290 [Intermediate] Blinking LEDs

Part 1 using JavaScript, with JQuery for input/displaying. Will try to get to part 2 soon. Feedback welcome.

`//convert number to binary function convertToBinary(i){ var bin = i.toString(2); return padBinary(bin); }

//need to pad number with necessary 0's as JS strips leading zeros in numbers function padBinary(s){ var i =0; if(s.length < 8){ i = (8 - s.length); } var pad = ''; while(i > 0){ pad = pad + '0'; i = i -1; }

return pad + s; }

//read the input (needs to be on window for JSfiddle) and call displayOut Fn window.readInput = function(){ var input = $('textarea#instructions').val().split("\n");

displayOutput(input); }

/** Take input array and loop over, if it begins with ld set a to the number, if it begins with out, display output **/ function displayOutput(input){ var a;

input.forEach(function(d){ d.trim();

if(d.indexOf('ld') !== -1){
    a = Number.parseInt(d.split(',')[1]);
}else if(d.indexOf('out') !== -1){
    var bin = convertToBinary(a),
        binAr = bin.split(''),
      output = '';

    binAr.forEach(function(b){
        if(b === '0'){
        output = output + '.'
      }else if(b === '1'){
        output = output + '*';
      }
    });

  $('div#output').append(output);
  $('div#output').append("<br>");

}

});

`}

/r/dailyprogrammer Thread