Last active
June 18, 2016 23:18
-
-
Save embarq/3dd74d41fe78582ea9c5052913e3b60f to your computer and use it in GitHub Desktop.
Simple number base-systems converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convert() { | |
"use strict"; | |
let initialNotation = parseInt($("#initial-notation").prop('value')); | |
let resultNotation = parseInt($("#result-notation").prop('value')); | |
let initialValue = $("#initial-value").prop('value'); | |
let resultValue = $("#result-value"); | |
let valueWithExcess = $("#with-excess"); | |
let toDecimalNotation = (value, notation) => | |
value.split('') | |
.map((char, i, arr) => | |
parseInt(char) * Math.pow(notation, arr.length - (i + 1))) | |
.reduce((sum, current) => sum + current); | |
// TODO: Fix convert from decimal to binary | |
let toResultNotation = (int, notation) => { | |
let result = []; | |
let convert = function(int, buffer) { | |
let current = Math.floor(int / notation); | |
if (current) { | |
buffer.push(int % notation); // Массив остатков от деления | |
convert(current, buffer); | |
} else { | |
buffer.push(1); | |
while (buffer.length % 4 !== 0) buffer.push(0); | |
return buffer.reverse(); | |
} | |
}; | |
convert(int, result); | |
return result.join(''); | |
}; | |
let decimal = toDecimalNotation(initialValue, initialNotation); | |
let result = toResultNotation(decimal, resultNotation); | |
// console.log( | |
// "initialNotation: %i, decimal: %i, resultNotation: %i, result: %s", | |
// initialNotation, decimal, resultNotation, result); | |
resultValue.prop("value", result); | |
} | |
$("[type=number]").on('keyup', convert); | |
$("select").on('change', convert); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Converter</title> | |
<link rel="stylesheet" type="text/css" href="../static/bootstrap/dist/css/bootstrap.min.css"> | |
<link rel="stylesheet" type="text/css" href="../darkly.min.css"> | |
<link rel="stylesheet" type="text/css" href="../main.css"> | |
</head> | |
<body> | |
<div class="container"> | |
<form> | |
<div class="form-group"> | |
<select id="initial-notation" class="form-field"> | |
<option value="2">Binary</option> | |
<option value="6">Sextaple</option> | |
<option value="8">Octal</option> | |
<option value="10" selected>Decimal</option> | |
</select> | |
<input type="number" value="71" size=20 id="initial-value" class="form-field" autofocus /> | |
</div> | |
<div class="form-group"> | |
<select id="result-notation" class="form-field"> | |
<option value="2" selected>Binary</option> | |
<option value="6">Sextaple</option> | |
<option value="8">Octal</option> | |
<option value="10">Decimal</option> | |
</select> | |
<input type="number" size=20 id="result-value" class="form-field" readonly /> | |
</div> | |
<div class="form-group"> | |
<label for="excess">Code with excess</label> | |
<input type="number" class="form-field" size=18 id="with-excess" readonly /> | |
</div> | |
<div class="form-group"> | |
<input type="button" class="btn convert" onclick="convert()" value="Convert" /> | |
</div> | |
</form> | |
</div> | |
</body> | |
<script type="text/javascript" src="../static/jquery/dist/jquery.min.js"></script> | |
<script type="text/javascript" src="app.js"></script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment