简体   繁体   中英

How do I convert an string to an Int which is came from the input of an button

I need to convert the string from my innerHTML which are received by an button click (I'm making an calculator) If needed here is my JS:

window.onload = function () {
// your code here
var counter = document.getElementById("scherm");
    var optellen = document.getElementById("1");
    var buttons = document.getElementsByTagName("button");
    var operators = ["+", "-", "*", "/", "=", "C"]

    // maak for loop voor buttons 
    for(var i=0; i<buttons.length; i++)
    {
        buttons[i].addEventListener("click", screen);
    }

    //optellen.addEventListener("click", screen); //+ string + sting aan het einde als je wilt gaan uitrekenen doe je parseInt()

    function screen() {
        if( operators.indexOf(this.innerHTML) != -1)
        {   

            if( operators.indexOf(this.innerHTML) == 5)
            {
                document.getElementById("scherm").innerHTML="";
            }

        }
        else
        {

            counter.innerHTML += this.innerHTML;
        }

        console.log(this.innerHTML);

        //if(counter.innerHTML == this.inner)
        //counter.innerHTML = buttons.innerHTML;
        //
    }

};

as requested HTML: <div id="rekenmachine"> <header><p id="scherm"></p></header> <section id="knoppen"> <div class="row"> <button value="1">1</button> <button value="2" class="pos">2</button> <button value="3" class="pos">3</button> <button value="+" class="pos">+</button> </div> <div class="row"> <button value="">4</button> <button value="" class="pos">5</button> <button value="" class="pos">6</button> <button value="" class="pos">-</button> </div> <div class="row"> <button value="">7</button> <button class="pos">8</button> <button class="pos">9</button> <button class="pos">*</button> </div> <div class="row"> <button value="">C</button> <button value="" class="pos">0</button> <button value="" class="pos">=</button> <button value="" class="pos">/</button> </div> </section>

This should do it.

parseInt("10");

Source: http://www.w3schools.com/jsref/jsref_parseint.asp

have a look at this:

http://www.w3schools.com/jsref/jsref_parseint.asp

(you should do something like: parseInt(counter.innerHTML))

 else {
     var result;
     if(counter.innerHTML != "") { 
         result = parseInt(counter.innerHTML);
      } else {
          result = 0;
      } 
     result += parseInt(this.innerHTML); 
     counter.innerHTML = result; }

You can try

Parsing function. Parsing will stop if hits a character than doesn't recognise. If parsing stop at first symbol then it returns NaN

parseInt("1") = 1
parseInt("1a") = 1
parseInt("a1") = NaN

Type changing.

Unary plus

+"1" = 1
+"1.231" = 1.231
+"1a" = NaN
+"a1" = NaN

Bitwise NOT

~~"1" = 1
~~"1.231" = 1
~~"a1" = 0
~~"1a" = 0
~~"a" = 0

Multiplication operator

"1" * 1 = 1
"1a" * 1 = NaN
"a1" * 1 = NaN
"a" * 1 = NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM