Stack Overflow Asked by Manish Jha on November 7, 2021
I’m making a binary calculator. Following is my javascript code:
// let operator = document.getElementsByClassName('operator');
let plus = document.getElementById('btnSum');
let minus = document.getElementById('btnSub');
let into = document.getElementById('btnMul');
let divide = document.getElementById('btnDiv');
let resultBox = document.getElementById('res');
var val1;
var val2;
var clicked = false;
plus.addEventListener("click", function(){
clicked = true;
let middleOperator = '+';
resultBox.innerHTML += ' + ';
});
minus.addEventListener("click", function(){
clicked = true;
let middleOperator = '-';
resultBox.innerHTML += ' - ';
});
into.addEventListener("click", function(){
clicked = true;
let middleOperator = '*';
resultBox.innerHTML += ' * ';
});
divide.addEventListener("click", function(){
clicked = true;
let middleOperator = '/';
resultBox.innerHTML += '/';
});
function pushZero(){
if(clicked){
//if middleOperator is pressed
resultBox.innerHTML += '0';
val2 = resultBox.innerText;
}else{
//if middleOperator is not pressed
resultBox.innerHTML += '0';
val1 = resultBox.innerText;
}
}
function pushOne(){
if(clicked){
//if middleOperator is pressed
resultBox.innerHTML += '1';
val2 = resultBox.innerText;
}else{
//if middleOperator is not pressed
resultBox.innerHTML += '1';
val1 = resultBox.innerText;
}
}
//passing array to this function
function binaryToNum(a){
let num = 0;
for(var i=0; i<a.length; i++){
num += a[i]*Math.pow(2, a.length-i-1);
}
return num;
}
//Function to convert a number to binary
function numToBinary(x){
var s='';
let n;
while(x>=1){
n = x%2;
s += `${n}`;
x /= 2;
}
return s;
}
function result(middleOperator){
var ans;
var num1 = binaryToNum(val1);
var num2 = binaryToNum(val2);
if(middleOperator == '+'){
ans = num1 + num2;
}
if(middleOperator == '-'){
ans = num1 - num2;
}
if(middleOperator == '*'){
ans = num1*num2;
}
if(middleOperator == '/'){
ans = num1/num2;
}
var output = numToBinary(ans);
console.log(output);
resultBox.innerHTML = output;
}
When I’m trying to display the result it shows empty output.
While assigning any random string to the output displays the same on the screen.(Thus there is no issue on Html or Dom side).
Also when I’m console.log() gives an empty output. How to solve this?
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP