0

Ok. I make a program for calculating the power of 2. I have the html code . But i describe it for you first. There is one input with id='inputin' and below there is a button. In the input you write a number . This number will be the exponent of two and after you push the button it appears a console.log window with the result

here the html :

<div id='main'>
    <input type="number" name="power2" placeholder='Enter the power' id='inputin'><br/>
    <button id="submit" onclick="binaryS()">Do it!</button>

    </div>

and the javascript:

binaryS = function() {
x = document.getElementById("inputin").value;
y=1;
for(i=1;i<=x;i++) {
    y=y*2;
}
console.log (y);
};

Why it doesnt work? Any help? and if you could suggest instead of an ugly console.log window how can i appear it in a new div somewhere in the page? Thnx

6
  • I'd use alert(y) for a quick check (nothing fancy) Commented Jun 28, 2013 at 19:26
  • Won't you get same answer for Y every single time as Y = 1? Commented Jun 28, 2013 at 19:27
  • It works for me. jsfiddle.net/barmar/a9FrL Commented Jun 28, 2013 at 19:28
  • Seems to work perfectly: jsfiddle.net/GolezTrol/rcVrV Commented Jun 28, 2013 at 19:29
  • 2
    Do you understand that console.log() doesn't open a window automatically? You have to view the log manually using Developer Tools. Commented Jun 28, 2013 at 19:29

3 Answers 3

7

Your JavaScript should be:

function binaryS() {
   var x = document.getElementById("inputin").value;
   document.getElementById("outputValue").innerHTML = Math.pow(2,x);
}

Place the following somewhere on your page for the display:

<div id="outputValue">Result</div> 
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Beat me to it when I was building this fiddle, which does essentially the same, only with span and innerText. And without using math. :p
That's my first real answer :D Thanks to this lovely site i rarely have questions to ask!
1

Try this function:

function nearestPow2(){
  return Math.pow(2, Math.round(Math.log(document.getElementById("inputin").value;) / Math.log(2))); 
}

Ref: http://weblog.bocoup.com/find-the-closest-power-of-2-with-javascript/

Comments

0

Without getting into details, your code is working. I have made a single html page to demonstrate it:

<html>
    <head>
        <script type="application/x-javascript">
            binaryS = function() {
                x = document.getElementById("inputin").value;
                y=1;
                for(i=1;i<=x;i++) {
                    y=y*2;
                }
                document.getElementById('result').innerHTML = y
            };
        </script>
    </head>
    <body>
        <div id='main'>
            <input type="number" name="power2" placeholder='Enter the power' id='inputin'><br/>
            <button id="submit" onclick="binaryS()">Do it!</button>
        </div>
        <div id="result" ></div>
    </body>
</html>

I also would suggest you using Math.pow instead of a custom math calculation:

Math.pow( 2, parseInt(document.getElementById("inputin").value));

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.