W funckiji INC DEC przekonerwtowanie działa normalnie lecz w funkcji NEG nie działa nie wiem dlaczego
function INC() {
const select1 = document.querySelector(".select-1").value.trim();
const select2 = document.querySelector(".select-2").value.trim();
const input = document.querySelectorAll("input");
a++;
b++;
hexString = a.toString(16);
hexStringb = b.toString(16);
const p = document.querySelector(".answer3");
if (input[0].value != "") {
p.textContent = `Rejestr ${select1}: ${hexString.toUpperCase()} `;
} else if (input[1].value != "") {
p.textContent = `Rejestr ${select2}: ${hexStringb.toUpperCase()}`;
} else if (input[1].value != "" && input[0].value != "") p.textContent = `Rejestr ${select1}: ${hexString.toUpperCase()} , ${select2}: ${hexStringb.toUpperCase()}`;
}
function DEC() {
const select1 = document.querySelector(".select-1").value.trim();
const select2 = document.querySelector(".select-2").value.trim();
const input = document.querySelectorAll("input");
a--;
b--;
hexString = a.toString(16);
hexStringb = b.toString(16);
const p = document.querySelector(".answer4");
if (input[0].value != "") {
p.textContent = `Rejestr ${select1}: ${hexString.toUpperCase()} `;
} else if (input[1].value != "") {
p.textContent = `Rejestr ${select2}: ${hexStringb.toUpperCase()}`;
} else if (input[1].value != "" && input[0].value != "") p.textContent = `Rejestr ${select1}: ${hexString.toUpperCase()} , ${select2}: ${hexStringb.toUpperCase()}`;
}
function NOT() {
const p = document.querySelector(".answer5");
const cleanStr = out.replace(/zero/gi, "0").replace(/one/gi, "1").replace(/[^01]/g, "");
const select1 = document.querySelector(".select-1").value.trim();
const strLength = Math.floor(out.length / 8) * 8;
p.textContent = `Rejestr ${select1}: ${cleanStr.slice(0, strLength)} `;
}
function NEG() {
const p = document.querySelector(".answer6");
const cleanStr = out.replace(/zero/gi, "0").replace(/one/gi, "1").replace(/[^01]/g, "");
const select1 = document.querySelector(".select-1").value.trim();
const strLength = Math.floor(out.length / 8) * 8;
let a;
a = cleanStr.slice(0, strLength);
let b = parseInt(a, 16);
b++;
const show = b.toString(16);
p.textContent = `Rejestr ${select1}: ${show.toUpperCase()} `;
}
pobranie dany odbywa się tu
case "save1-button":
const input1 = document.querySelectorAll("input");
a = parseInt(input1[0].value, 16);
b = parseInt(input1[1].value, 16);
break;
case "save2-button":
const input2 = document.querySelectorAll("input");
const cd = input2[0].value;
function hex2bin(hex = cd) {
hex = hex.replace("0x", "").toLowerCase();
out = "";
for (var c of hex) {
switch (c) {
case "0":
out += "0000";
break;
case "1":
out += "0001";
break;
case "2":
out += "0010";
break;
case "3":
out += "0011";
break;
case "4":
out += "0100";
break;
case "5":
out += "0101";
break;
case "6":
out += "0110";
break;
case "7":
out += "0111";
break;
case "8":
out += "1000";
break;
case "9":
out += "1001";
break;
case "a":
out += "1010";
break;
case "b":
out += "1011";
break;
case "c":
out += "1100";
break;
case "d":
out += "1101";
break;
case "e":
out += "1110";
break;
case "f":
out += "1111";
break;
default:
return "";
}
}
}
hex2bin();
break;
cały kod html:
<body>
<section id="rejestry">
<div class="Ax">
<h1>Rejestry</h1>
<select class="select-1">
<option value="Al">AL</option>
<option value="Ah">AH</option>
<option value="Bl">BL</option>
<option value="Bh">BH</option>
<option value="Cl">CL</option>
<option value="Ch">CH</option>
<option value="Dl">DL</option>
<option value="Dh">DH</option>
</select>
<input type="text" maxlength="2" /><button class="save-button">Random</button><button class="save1-button">Save DEC INC</button>
<button class="save2-button">Save NOT</button>
</div>
<div class="Bx">
<select class="select-2">
<option value="Al">AL</option>
<option value="Ah">AH</option>
<option value="Bl">BL</option>
<option value="Bh">BH</option>
<option value="Cl">CL</option>
<option value="Ch">CH</option>
<option value="Dl">DL</option>
<option value="Dh">DH</option>
</select>
<input type="text" maxlength="2" /><button class="save-button">Random</button>
</div>
<button class="clear-button">wyczyść</button>
</section>
<article class="Instructions">
<h1>Instrukcje</h1>
<div class="Xchg">
<button class="INC-button">INC</button>
<p class="answer3">Click save before</p>
</div>
<div class="Xchg">
<button class="DEC-button">DEC</button>
<p class="answer4">Click save before</p>
</div>
<div class="Xchg">
<button class="NOT-button">NOT</button>
<p class="answer5">Click save before</p>
</div>
<div class="Xchg">
<button class="NEG-button">NEG</button>
<p class="answer6">Click save before</p>
</div>
</article>
</body>