I am writing a decimal to binary converter in javascript and i have this issue.
Here is my code.
function binaryConverter(x)
{
if(x === "")
{
return 0;
}
let binaryArray = new Array(128, 64, 32, 16, 8, 4, 2, 1);
let result = new Array();
let newX;
//pelda: x = 75
for(let i = 0; i < binaryArray.length; i++)
{
if(binaryArray[i] <= x)
{
result.push('1');
newX = x - binaryArray[i]; // 75 - 64 = 11 | 11 - 8 = 3 | 3 - 2 = 1
x = newX;
}
else
{
result.push('0');
}
}
writeBinaryNumber(result);
}
function writeBinaryNumber(ar)
{
for (let i = 0; i < ar.length; i++)
{
document.write(ar[i]);
}
}
With document.write() it is works(I only tested it with number 75, i know the first part of binary code is 0, i will fix it.). But i want to connect with my html. I would like to return a variable which contains the chars which is in the array but i can't. In C++ with iterators it is working but i am not familiar with javascript and i used google to search about solution but i couldn't find. If somebody know the solution please write it down! Thanks!