Javascript 二进制表示法中1位的计数

Javascript 二进制表示法中1位的计数,javascript,html,arrays,Javascript,Html,Arrays,我尝试使用下面的代码将十进制输入转换为二进制表示,然后计算其中的1个数 当我运行以下代码时,没有输出,也没有报告错误: function demo() { var arra, i, rem; var Input = document.getElementById('demo'); arra = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

我尝试使用下面的代码将十进制输入转换为二进制表示,然后计算其中的1个数

当我运行以下代码时,没有输出,也没有报告错误:

function demo() {
    var arra, i, rem;
    var Input = document.getElementById('demo');
    arra = [
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    var x = 1;
    while (Input > 0) {
        rem = Input % 2;
        Input = (parseInt(Input / 2));
        if (rem > 0) {
            arra[i] = 1;
            i = i + 1;
        } else {
            i = i + 1;
        }
    }

    while (x < 35) {
        if (arra[x] == 1) {
            document.getElementById("output").innerHTML = "SOB Found";
            x = x + 1;
        } else {
            x = x + 1;
        }
    }
}
HTML代码:

<!doctype html>
<html>
  <head>
    <title>Test Phase</title>
    <script src="SOB.js" type="text/javascript"></script>
  </head>
  <body>
    <input type="text" name="value" id="Input"   />
    <input type="submit"  value="submit"   onClick="demo()"  />
    <p id="output"> </p>
    <script src="SOB.js" type="text/javascript"></script>
  </body>
</html>

您的代码有几处错误。var Input是一个实际上不存在的DOM元素,演示程序不在HTML中,您可能希望执行var Input=document.getElementById'Input'.value; 其次,你没有给i一个初始值,这使得第一个arra[i]未定义:1或当时输入的任何值。 第三,数组从索引0开始,因此var x应该是0

要将这一切结合起来,应该:

function demo() {
    var arra, i = 0, rem;
    //            ^ giving i initial value
    var Input = document.getElementById('Input').value;
    //                                   ^       ^
    arra = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    var x = 0; // start index is 0
    while (Input > 0) {
        rem = Input % 2;
        Input = (parseInt(Input / 2));
        if (rem > 0) {
            arra[i] = 1;
            i = i + 1;
        } else {
            i = i + 1;
        }
    }

    while (x < 35) {
        if (arra[x] == 1) {
            document.getElementById("output").innerHTML = "SOB Found";
            x = x + 1;
        } else {
            x = x + 1;
        }
    }
}

这是可行的,尽管我不理解它的含义。

您可以使用空数组,使用隐式casted to number输入值,使用所有小写字母变量并计算数组中的所有1。然后使用计数进行输出

功能演示{ 变量数组=[],i=0,rem, input=+document.getElementById'input'。值, 计数=0; 当输入>0时{ rem=输入%2; 输入=Math.floorinput/2; 数组[i]=rem; i++; } 对于i=0;i

有几个问题,但您可以通过使用.toString方法用更少的代码来完成:

功能演示{ //通过value属性获取输入值并转换为数字+ var val=+document.getElementByIdinput.value; //转换为二进制表示: var bin=val.toString2; //通过计算1位的数量来计算1位的数量 //查找1时匹配: var ones=bin.match/1/g.length; //使用textContent而不是innerHTML输出结果: document.getElementByIdbinary.textContent=bin; document.getElementByIdones.textContent=个; } 二元的: 一位:

请为您的问题添加一个更好的标题,并尝试更好地解释。另外,您没有ID为demo的元素。也许你想做document.getElementById'Input';?'JavaScript代码“?JavaScript代码有什么问题?@putvande是的,我已经将document.getElementById'demo'调整为document.getElementById'Input',但仍然没有找到我的代码有什么问题