Skip to content

Instantly share code, notes, and snippets.

@gormat
Created May 9, 2017 08:35
Show Gist options
  • Save gormat/1f7db213aec110f7afbd0b73ba982d21 to your computer and use it in GitHub Desktop.
Save gormat/1f7db213aec110f7afbd0b73ba982d21 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="output"></div>
<script>
const output = document.getElementById('output');
// This example calcuates binary & for 2 binary numbers
const rules = [
['*0', '*ac'],
['*1', '*bc'],
['*a', 'a*'],
['*b', 'b*'],
['0a', 'a0'],
['0b', 'b0'],
['1a', 'a1'],
['1b', 'b1'],
['a1', '0d'],
['a0', '0d'],
['b1', '1d'],
['b0', '0d'],
['*c', '*'],
['0d', '0'],
['1d', '1'],
['0*', '0', true],
['1*', '1', true]
];
const convert = (str, rules) => {
let ruleFound = false;
for(let i = 0; i < rules.length; i ++) {
if(str.indexOf(rules[i][0]) !== -1) {
const oldstr = str;
str = str.replace(rules[i][0], rules[i][1]);
console.log(oldstr, rules[i], str);
ruleFound = true;
if(rules[i][2])
return;
break;
}
}
if(ruleFound) {
convert(str, rules);
}
}
convert('10100*11111');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment