Play at https://learningcontent.cisco.com/games/binary/index.html
Paste code below in site's console and then type run()
to automatically solve binary problems. To stop type stop()
in the console and press enter.
The code:
// maps numbers to button idx on calculator
const calculatorMap = [2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
var listinerId = 0;
function run() {
listinerId = setInterval(() => {
// progress to next level
document.querySelector(".window.modal.fade-enter-done>.modal-body>button")?.click();
// get bottom problem
const problem = document.querySelector(".problem:not(.blank-exit)");
// if problem exists solve it
if (problem) {
// get problem wrapers
const div = problem.querySelector(".problem-wrapper>.sub-wrapper");
// check problem type
if (problem.querySelector(".isProblem")) {
// user has to enter digits
// open calculator
div.querySelector(".digits").click();
// bits to number
let sum = 0;
div.querySelectorAll(".bit").forEach((e, i) => {
sum += Number(e.innerText) * 2 ** (7 - i);
});
console.log("answer:", sum);
// select numbers on calculator
Array.from(String(sum), Number).forEach((e) => {
document.querySelector(`.calculator :nth-child(${calculatorMap[e]})`).click();
});
// submit
document.querySelector(`.calculator :nth-child(3)`).click();
} else {
// user has to flip bits
let correct = Number(div.querySelector(".digits").innerText);
// convert correct val to 8 bit array of bool
let correctBin = new Array(8).fill(false);
for (let index = 7; correct > 0 && index >= 0; index--) {
correctBin[index] = Boolean(correct % 2);
correct = Math.floor(correct / 2);
}
console.log("answer:", correctBin);
div.querySelectorAll("button.bit").forEach((e, i) => {
// if wrong state then flip bit
if ((e.innerText == "1") != correctBin[i]) {
e.click();
}
});
}
}
// wait 0.5s
}, 500);
}
function stop() {
clearInterval(listinerId);
}
very nice