DEV Community

Discussion on: How to Build Binary to Decimal Converter Using JavaScript

Collapse
 
efpage profile image
Eckehard

I tried the same using DML, bit shorter:

<!DOCTYPE html> <html lang="de"> <head> <meta charset="utf-8"> <title>title</title>  <script src="https://efpage.de/DML/DML_homepage/lib/DML-min.js"></script>  <style> body { font-family: sans-serif } </style> </head>  <body> <script> "use strict"; // Make the input elements / key filter function myInput(txt, rule) { // Create text and input field idiv(txt, "width: 100px;") let inp = make("input", { value: 1 }) br() // Key filter inp.onkeydown = (e) => { if (e.key.length == 1) if (!e.key.match(rule)) e.preventDefault() } // Filter keys return inp } // Build the page h1("Binary converter") sidiv("", _box + _bigPadding) let b = myInput("binary", "[0,1]") let d = myInput("decimal", "[0-9]") b.oninput = () => d.value = parseInt(b.value, 2) d.oninput = () => b.value = ( parseInt(d.value,10) >>> 0).toString(2); </script> </body> </html> 
Enter fullscreen mode Exit fullscreen mode

binary