224 lines
6.0 KiB
JavaScript
224 lines
6.0 KiB
JavaScript
function setSize(varWidth, varHeight){
|
|
let box = document.getElementById("webBoxSel");
|
|
let boxHeight = (varHeight / 2);
|
|
let boxWidth = (varWidth / 2);
|
|
|
|
if((boxHeight >= 300) && (boxWidth >= 300))
|
|
{
|
|
box.style.backgroundColor = 'red';
|
|
}else{
|
|
box.style.backgroundColor = 'yellow';
|
|
}
|
|
|
|
box.style.width = boxWidth+'px';
|
|
box.style.height = boxHeight+'px';
|
|
|
|
let textInBox = document.getElementsByClassName('boxTextClass')[0];
|
|
|
|
textInBox.style.color = 'blue';
|
|
textInBox.style.margin = '25%';
|
|
textInBox.style.marginLeft = '25%';
|
|
|
|
}
|
|
|
|
if(document.readyState === "loading"){
|
|
const varW = window.outerWidth;
|
|
const varH = window.outerHeight;
|
|
setSize(varW, varH);
|
|
}
|
|
|
|
window.addEventListener("resize", function (){
|
|
|
|
const varW = window.outerWidth;
|
|
const varH = window.outerHeight;
|
|
setSize(varW, varH);
|
|
});
|
|
|
|
document.getElementById("textValueBtn").addEventListener('click', function(){
|
|
let textInBox = document.getElementById("boxTextID");
|
|
let changeText = document.getElementById("textValue").value;
|
|
|
|
|
|
if(changeText.length == 0)
|
|
{
|
|
alert("Text: Not a valid text for this action.")
|
|
}else{
|
|
textInBox.innerText = changeText;
|
|
}
|
|
});
|
|
|
|
document.getElementById("textSizeBtn").addEventListener('click', function(){
|
|
let textInBox = document.getElementById("boxTextID");
|
|
let changeSize = document.getElementById("textSize").value;
|
|
|
|
if(!isNaN(changeSize))
|
|
{
|
|
alert("Size: Not a valid number for this action.")
|
|
}else{
|
|
textInBox.style.fontSize = changeSize+"px";
|
|
}
|
|
});
|
|
|
|
document.getElementById("textAngleBtn").addEventListener('click', function(){
|
|
let textInBox = document.getElementById("boxTextID");
|
|
let changeAngle = document.getElementById("textAngle").value;
|
|
|
|
if(!isNaN(changeAngle))
|
|
{
|
|
alert("Angle: Not a valid number for this action.")
|
|
}else{
|
|
textInBox.style.transform = "rotate("+changeAngle+"deg)";
|
|
}
|
|
});
|
|
|
|
|
|
document.getElementById("startBinCalc").addEventListener('click', function(){
|
|
let dezNumberToBin1 = tryParseInt(document.getElementById("binValue1").value);
|
|
let dezNumberToBin2 = tryParseInt(document.getElementById("binValue2").value);
|
|
|
|
if(isNaN(dezNumberToBin1) || isNaN(dezNumberToBin2))
|
|
{
|
|
alert("BinaryNumberAddition: At least one number is not valid for this action.")
|
|
}else{
|
|
let bin1 = calcDezToBin(dezNumberToBin1);
|
|
let bin2 = calcDezToBin(dezNumberToBin2);
|
|
|
|
let numbersForText = fillwithZero(bin1.split(''), bin2.split(''));
|
|
let result = additionTwoBinNumbers(bin1, bin2);
|
|
|
|
|
|
document.getElementById('binCalcResult').innerHTML = "<center> "+numbersForText.first.join('') + "<br>+ " + numbersForText.second.join('') + "<br> " + result+" </center>";
|
|
}
|
|
});
|
|
|
|
function calcDezToBin(dezNumber){
|
|
|
|
let tempNumber = dezNumber;
|
|
let remainderString = "";
|
|
|
|
let lastNumber = 0;
|
|
|
|
while(tempNumber != 0)
|
|
{
|
|
lastNumber = Math.floor(tempNumber/2);
|
|
remainderString += tempNumber%2;
|
|
tempNumber = lastNumber;
|
|
}
|
|
|
|
let binaryValue = flipBinString(remainderString);
|
|
return binaryValue;
|
|
//document.getElementById('binCalcResult').innerText = binaryValue;
|
|
}
|
|
|
|
function tryParseInt(str) {
|
|
let retValue = null;
|
|
if(str !== null) {
|
|
if(str.length > 0) {
|
|
if (!isNaN(str)) {
|
|
retValue = parseInt(str);
|
|
return retValue;
|
|
}
|
|
}
|
|
}
|
|
return str;
|
|
}
|
|
|
|
function flipBinString(remainderString)
|
|
{
|
|
let count = remainderString.length;
|
|
let remainders = [];
|
|
|
|
for(let i = 0; i < count; i++)
|
|
{
|
|
remainders[i] = remainderString[i];
|
|
}
|
|
let returnVal = remainders.reverse().join('');
|
|
return returnVal;
|
|
}
|
|
|
|
function additionTwoBinNumbers(bin1, bin2)
|
|
{
|
|
|
|
let binArrays = fillwithZero(bin1.split(''), bin2.split(''));
|
|
let bin1Array = binArrays.first;
|
|
let bin2Array = binArrays.second;
|
|
let binOverflow = Array.from({length: bin2Array.length}, (v, k) => '0');
|
|
let lastOverflowBit = '0';
|
|
|
|
let tempResult = "";
|
|
|
|
function setOverflow(index){
|
|
if(isLastIndex(index))
|
|
{
|
|
lastOverflowBit = '1';
|
|
}else{
|
|
binOverflow[index-1] = '1';
|
|
}
|
|
}
|
|
|
|
for(let i = bin1Array.length-1; i >= 0; i--){
|
|
|
|
if(bin1Array[i] === '0' && bin2Array[i] === '0' && binOverflow[i] === '0') {
|
|
tempResult += '0';
|
|
}else if(bin1Array[i] === '0' && bin2Array[i] === '0' && binOverflow[i] === '1')
|
|
{
|
|
tempResult += '1';
|
|
}else if (((bin1Array[i] === '0' && bin2Array[i] === '1') || (bin1Array[i] === '1' && bin2Array[i] === '0')) && binOverflow[i] === '0')
|
|
{
|
|
tempResult += '1';
|
|
}
|
|
else if (((bin1Array[i] === '0' && bin2Array[i] === '1') || (bin1Array[i] === '1' && bin2Array[i] === '0')) && binOverflow[i] === '1')
|
|
{
|
|
tempResult += '0';
|
|
setOverflow(i);
|
|
}
|
|
else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '0')
|
|
{
|
|
tempResult += '0';
|
|
setOverflow(i);
|
|
}else if((bin1Array[i] === '1' && bin2Array[i] === '1') && binOverflow[i] === '1')
|
|
{
|
|
tempResult += '1';
|
|
setOverflow(i);
|
|
}
|
|
}
|
|
|
|
if(lastOverflowBit === '1')
|
|
{
|
|
tempResult += '1';
|
|
}
|
|
|
|
tempResult = flipBinString(tempResult);
|
|
return tempResult;
|
|
}
|
|
|
|
function isLastIndex(index){
|
|
return index === 0;
|
|
}
|
|
|
|
function fillwithZero(binArray1, binArray2)
|
|
{
|
|
let difference = Math.abs(binArray1.length - binArray2.length);
|
|
|
|
if(difference != 0)
|
|
{
|
|
if(binArray1.length < binArray2.length)
|
|
{
|
|
for(let i = 0; i < difference; i++)
|
|
{
|
|
binArray1.unshift('0');
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for(let i = 0; i < difference; i++)
|
|
{
|
|
binArray2.unshift('0');
|
|
}
|
|
}
|
|
}
|
|
return {
|
|
first: binArray1,
|
|
second: binArray2
|
|
}
|
|
} |