Almost there, but this blows up of the len is smaller than the length of the string without padding. A small modification solves it ((len-sbl>0)):
var len = 6;
var sb = parseInt(1023).toString(2);
var sbl = sb.length;
var sbFixedLenStart = ('0').repeat((len-sbl>0)?(len-sbl):0) + sb;
var sbFixedLenEnd = sb + ('0').repeat((len-sbl>0)?(len-sbl):0);
console.log(sbFixedLenStart);
console.log(sbFixedLenEnd);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Almost there, but this blows up of the len is smaller than the length of the string without padding. A small modification solves it (
(len-sbl>0)
):MDN usually has polyfills or in this case, a link to polyfills: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
Altho might need some tweaks to better fit Espruino's constraints...