1

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!

1
  • you need to display with in html right Commented Jan 23, 2020 at 17:38

2 Answers 2

1

Use the array join() method to concatenate all the strings in an array into a single string.

let binaryString = ar.join('');

You could also just concatenate to a string instead of creating an array.

function binaryConverter(x) {
  if (x === "") {
    return 0;
  }

  let binaryArray = new Array(128, 64, 32, 16, 8, 4, 2, 1);
  let result = '';
  let newX;

  //pelda: x = 75

  for (let i = 0; i < binaryArray.length; i++) {

    if (binaryArray[i] <= x) {
      result += '1'
      newX = x - binaryArray[i]; // 75 - 64 = 11 | 11 - 8 = 3 | 3 - 2 = 1
      x = newX;

    } else {
      result += '0';
    }
  }

  return result;
}

document.getElementById("input").addEventListener("change", function() {
  var input = parseInt(this.value);
  document.getElementById("result").innerText = binaryConverter(input);
})
Enter number: <input id="input"> 
<br>
Result: <span id="result"></span>

Sign up to request clarification or add additional context in comments.

Comments

0

You could do like this

document.getElementById('demo').innerHTML = ar.join('')

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);

}
binaryConverter(75)
function writeBinaryNumber(ar) {
 document.getElementById('demo').innerHTML = ar.join('')
}
#demo{
 color:red
}
<p id="demo" ></p>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.