Files
CS-Box/Export/assets/js/script.min.js

1 line
8.6 KiB
JavaScript

if(window.location.pathname.includes("binary")){let e=0,t={"-128":!1,1:!1,2:!1,4:!1,8:!1,16:!1,32:!1,64:!1,128:!1},n=[];const a=document.getElementById("blbN128");function initialize(){setBitValues(),resetBinarySimulator()}function setBitValues(){n=a?[-128,64,32,16,8,4,2,1]:[128,64,32,16,8,4,2,1]}function togglePower(e,t){const n=e<0?`N${Math.abs(e)}`:e,a=document.getElementById(`blb${n}`),r=document.getElementById(`swt${n}`);a&&r&&(a.classList.toggle("poweredOn",t),a.classList.toggle("poweredOff",!t),r.classList.toggle("btnActive",t))}function resetBinarySimulator(){Object.keys(t).forEach((e=>{togglePower(parseInt(e,10),!1),t[e]=!1})),e=0,updateBinary()}function changeBit(n){const a=getBitKey(n),r=t[a];togglePower(n,!r),t[a]=!r,e+=r?-n:n,updateBinary()}function updateBinary(){const a=n.map((e=>t[getBitKey(e)]?"1":"0")).join("");document.getElementById("denaryNumber").innerText=e,document.getElementById("binaryNumber").innerText=a}function customBinaryParser(e){resetBinarySimulator(),(e=e.padStart(8,"0")).split("").forEach(((e,t)=>{"1"===e&&changeBit(n[t])}))}function customDenaryParser(e){const t=a?-128:0,r=a?127:255;if(null===e&&(e=0),e<t||e>r)return alert(`Invalid input! Please enter a denary value between ${t} and ${r}.`),requestDenary();if(resetBinarySimulator(),a&&e<0){let t=Math.abs(e);-128===e?changeBit(-128):(n.forEach((e=>{t>=Math.abs(e)&&(changeBit(e),t-=Math.abs(e))})),twosComplementFlip())}else n.forEach((t=>{e>=Math.abs(t)&&(changeBit(t),e-=Math.abs(t))}))}function shiftBinary(e){const t=document.getElementById("binaryNumber").innerText;let n;"left"===e?n=t.slice(1)+"0":"right"===e&&(n="0"+t.slice(0,-1)),customBinaryParser(n)}function shiftTwosComplement(e){const t=document.getElementById("binaryNumber").innerText;let n;"left"===e?n=t.slice(1)+"0":"right"===e&&(n=t[0]+t.slice(0,-1)),customBinaryParser(n)}function twosComplementFlip(){customBinaryParser(addBinaryNumbers(document.getElementById("binaryNumber").innerText.split("").map((e=>"1"===e?"0":"1")).join(""),"00000001").binaryResult)}function addBinaryNumbers(e,t){let n=0,a="";for(let r=7;r>=0;r--){const o=(parseInt(e[r],10)||0)+(parseInt(t[r],10)||0)+n;a=o%2+a,n=Math.floor(o/2)}return{binaryResult:a.slice(-8),overflow:n?"1":"0"}}function getBitKey(e){return e<0?`N${Math.abs(e)}`:e.toString()}function requestBinary(){let e;do{if(e=prompt("Please enter an 8-bit Binary Value (only 0s and 1s are allowed):"),null===e){e="00000000";break}/^[01]{1,8}$/.test(e)||alert("Invalid input! Binary values must be up to 8 digits long and only contain 0 or 1.")}while(!/^[01]{1,8}$/.test(e));customBinaryParser(e)}function requestDenary(){let e;const t=a?-128:0,n=a?127:255;do{if(e=prompt(`Enter a Denary Value (${t} to ${n}):`),null===e){e=0;break}e=parseInt(e,10),(isNaN(e)||e<t||e>n)&&alert(`Invalid input! Please enter a denary value between ${t} and ${n}.`)}while(isNaN(e)||e<t||e>n);customDenaryParser(e)}document.addEventListener("DOMContentLoaded",initialize)}if(function(){"use strict";!function(){if("requestAnimationFrame"in window&&!/Mobile|Android/.test(navigator.userAgent)){var e=document.querySelectorAll("[data-bss-parallax]");if(e.length){var t,n=[];window.addEventListener("scroll",a),window.addEventListener("resize",a),a()}}function a(){n.length=0;for(var a=0;a<e.length;a++){var o=e[a].getBoundingClientRect(),i=parseFloat(e[a].getAttribute("data-bss-parallax-speed"),10)||.5;o.bottom>0&&o.top<window.innerHeight&&n.push({speed:i,node:e[a]})}cancelAnimationFrame(t),n.length&&(t=requestAnimationFrame(r))}function r(){for(var e=0;e<n.length;e++){var t=n[e].node,a=n[e].speed;t.style.transform="translate3d(0, "+-window.scrollY*a+"px, 0)"}}}()}(),window.location.pathname.includes("hexadecimal")){const e=window.location.pathname.includes("gcse-hexadecimal"),t=e?2:4,n=e?8:16,a={1:0,16:0,256:0,4096:0},r={},o=e?[16,1]:[4096,256,16,1];function updatePlace(e){r[`slider${e}`]&&(a[e]=parseInt(r[`slider${e}`].value,10),updateNumbers())}function updateNumbers(){let e=0,r="",i="";o.forEach((t=>{const n=a[t];e+=n*t,r+=convertToBinary(n),i+=convertToHex(n)})),r=r.slice(-n).padStart(n,"0"),i=i.slice(-t).padStart(t,"0"),document.getElementById("binaryNumber").innerText=r,document.getElementById("denaryNumber").innerText=e,document.getElementById("hexadecimalNumber").innerText=i}function convertToBinary(e){return e.toString(2).padStart(4,"0")}function convertToHex(e){return e.toString(16).toUpperCase()}["slider1","slider16","slider256","slider4096"].forEach((e=>{const t=document.getElementById(e);t&&(r[e]=t,t.addEventListener("input",(t=>{t.stopPropagation(),updatePlace(parseInt(e.replace("slider",""),10))})))})),document.addEventListener("DOMContentLoaded",(()=>{updateNumbers()}))}const LIGHT_MULTIPLIER=100/15,COLORS=["R","G","B"],PLACES=[1,16];let denary=0,redDenary=0,greenDenary=0,blueDenary=0,redBinary="",greenBinary="",blueBinary="",hexadecimal="";const places={R16:0,R1:0,G16:0,G1:0,B16:0,B1:0};function resetColours(){COLORS.forEach((e=>{PLACES.forEach((t=>{resetPlace(e,t)}))}))}function resetPlace(e,t){const n=`${e}${t}`;places[n]=0;const a=LIGHT_MULTIPLIER*places[n]/100;document.getElementById(`blb${n}`).style.opacity=a,updateColours()}function togglePlace(e,t,n){const a=`${e}${t}`,r=places[a];if("up"===n&&r<15||"down"===n&&r>0){places[a]+="up"===n?1:-1;const e=LIGHT_MULTIPLIER*places[a]/100;document.getElementById(`blb${a}`).style.opacity=e,updateColours()}}function updateColours(){redDenary=16*places.R16+places.R1,greenDenary=16*places.G16+places.G1,blueDenary=16*places.B16+places.B1,denary=`${redDenary}, ${greenDenary}, ${blueDenary}`,hexadecimal=`#${convertToHex(places.R16)}${convertToHex(places.R1)}${convertToHex(places.G16)}${convertToHex(places.G1)}${convertToHex(places.B16)}${convertToHex(places.B1)}`,redBinary=`${convertToBinary(places.R16)}${convertToBinary(places.R1)}`,greenBinary=`${convertToBinary(places.G16)}${convertToBinary(places.G1)}`,blueBinary=`${convertToBinary(places.B16)}${convertToBinary(places.B1)}`,document.getElementById("denaryNumber").innerHTML=denary,document.getElementById("hexadecimalNumber").innerHTML=hexadecimal,document.getElementById("colouredHex").style.backgroundColor=hexadecimal,document.getElementById("invertedHex").style.backgroundColor=invertedHex(),document.getElementById("redBinaryNumber").innerHTML=redBinary,document.getElementById("blueBinaryNumber").innerHTML=blueBinary,document.getElementById("greenBinaryNumber").innerHTML=greenBinary}function invertedHex(){return`#${convertToHex(15-places.R16)}${convertToHex(15-places.R1)}${convertToHex(15-places.G16)}${convertToHex(15-places.G1)}${convertToHex(15-places.B16)}${convertToHex(15-places.B1)}`}function convertToHex(e){return e<10?e.toString():String.fromCharCode(55+e)}function convertToBinary(e){return e.toString(2).padStart(4,"0")}function updateHex(e){e?("#"===e.charAt(0)&&(e=e.slice(1)),isHex(e)&&6===e.length?(e.split("").forEach(((e,t)=>{const n=`${COLORS[Math.floor(t/2)]}${t%2==0?16:1}`;places[n]=parseInt(e,16);const a=LIGHT_MULTIPLIER*places[n]/100;document.getElementById(`blb${n}`).style.opacity=a})),updateColours()):(alert("Invalid Entry"),resetColours())):resetColours()}function isHex(e){return/^[0-9A-Fa-f]+$/.test(e)}function requestHex(){updateHex(prompt("Please enter your Hex Value"))}function invertHex(){updateHex(invertedHex())}let inputs={input1:!1,input2:!1},gateValue=!1;function toggleInput(e){const t=`input${e}`;inputs[t]=!inputs[t],updateInputState(`swtInput${e}`,inputs[t]),updateGate()}function updateGate(){const e=document.getElementById("pageHeading").textContent;gateValue=evaluateGate(e),updateGateLight(e,gateValue)}function evaluateGate(e){const{input1:t,input2:n}=inputs;switch(e){case"AND Gate":return t&&n;case"OR Gate":return t||n;case"NOT Gate":return!t;case"NAND Gate":return!(t&&n);case"NOR Gate":return!(t||n);case"XOR Gate":return t!==n;case"XNOR Gate":return t===n;default:return console.error("Unknown Gate Type"),!1}}function updateGateLight(e,t){const n=getLightBulbId(e),a=document.getElementById(n);a&&(a.classList.toggle("poweredOn",t),a.classList.toggle("poweredOff",!t))}function getLightBulbId(e){switch(e){case"AND Gate":return"blbAndGate";case"OR Gate":return"blbOrGate";case"NOT Gate":return"blbNotGate";case"NAND Gate":return"blbNandGate";case"NOR Gate":return"blbNorGate";case"XOR Gate":return"blbXorGate";case"XNOR Gate":return"blbXnorGate";default:return console.error("Unknown Gate Type"),null}}function updateInputState(e,t){const n=document.getElementById(e);n&&n.classList.toggle("btnActive",t)}function resetGate(){inputs.input1=!1,inputs.input2=!1,updateInputState("swtInput1",inputs.input1),updateInputState("swtInput2",inputs.input2);const e=document.getElementById("pageHeading").textContent;"NOT Gate"===e?(gateValue=!0,updateGateLight(e,gateValue)):updateGate()}