JavaScript Incorrect result from element value comparison incorrect result in formulas,incorrect result in testing

I'm making an html card game and it has an if statement with a greater than or equal to comparison operator. But when the money is lower that the price it still runs as if you have the needed money.

HTML

    <article id="moneysys">
        <p id="ptdabtd" >Click 'Set Cash' To Enter A Starting Ammount. (Button Will Be Deleted Once Finished)</p>
        <button id="scda" onclick="setcash();">Set Cash.</button>
        <br />
        <div class="cash">Cash: <span id="cash">0</span></div>
        <div>Pack Price: <span id="packprice">50</span></div>
    </article>
    <main>
        <center>
            <button class="open" onclick="opencards()">Open Cards.</button>
            <br />
            <img class="card" src="images/blank.png" id="canvas" />
            <img class="card" src="images/blank.png" id="canvas1" />
            <img class="card" src="images/blank.png" id="canvas2" />
            <img class="card" src="images/blank.png" id="canvas3" />
            <img class="card" src="images/blank.png" id="canvas4" />
        </center>
    </main>

JAVASCRIPT

function setcash(){
    var wcash =  prompt("Please Enter How Much Cash You Want To Start With", "Cash Wanted");
    document.getElementById("cash").innerHTML = wcash;
    document.getElementById("scda").remove();
    document.getElementById("ptdabtd").remove();

}

var cards = new Array("images/1.png", "images/2.png", "images/3.png", "images/4.png", "images/5.png","images/6.png", "images/7.png", "images/8.png", "images/9.png");

var pack = document.getElementById("packprice");
var ccash = document.getElementById("cash");

var pc = Number(pack);
var cash = Number(ccash);

function opencards(){
    if (cash >= pc){
        var randomNum = Math.floor(Math.random() * cards.length);
        document.getElementById("canvas").src = cards[randomNum];
        document.getElementById("canvas1").src = cards[randomNum];
        document.getElementById("canvas2").src = cards[randomNum];
        document.getElementById("canvas3").src = cards[randomNum];
        document.getElementById("canvas4").src = cards[randomNum];
        cash = cash - pc;
    } else{
        alert("not")
    }
};
Answer:1

You are not getting the value of pack or ccash variable. Because you not use the innerHTML .

The innerHTML property sets or returns the HTML content (inner HTML) of an element.

var pack = document.getElementById("packprice");
var ccash = document.getElementById("cash");

Replace your code with this

var pack = document.getElementById("packprice").innerHTML;
var ccash = document.getElementById("cash").innerHTML;

Refrence

Answer:2

As I want to unbind(off) the events I wrapped the code inside a function but as I need to see which key is pressed I need to get the event of the event(not sure how this is called. // normal example ...

As I want to unbind(off) the events I wrapped the code inside a function but as I need to see which key is pressed I need to get the event of the event(not sure how this is called. // normal example ...

I am having to complete a exercise in JavaScript where I use RegExp. During testing, I stumbled across this returned array when I did a non global match. var first = "28".match(/[0-9]\s?/); console....

I am having to complete a exercise in JavaScript where I use RegExp. During testing, I stumbled across this returned array when I did a non global match. var first = "28".match(/[0-9]\s?/); console....

I want to rotate an image every time I click on it.. I've created a function to do this but the image only rotate the first time I click on it... Moreover, once the image rotate it change ...

I want to rotate an image every time I click on it.. I've created a function to do this but the image only rotate the first time I click on it... Moreover, once the image rotate it change ...

I am trying to call a JavaScript function from the onclick event of two different buttons. I have dug around and searched for like problems but have not found a solutions. When I click either button I ...

I am trying to call a JavaScript function from the onclick event of two different buttons. I have dug around and searched for like problems but have not found a solutions. When I click either button I ...

  1. unable to call function in ajax success
  2. unable to call function dllinstall
  3. unable to call javascript function from html
  4. unable to call the function dllinstall in the file browseui.dll
  5. unable to call javascript function from code behind
  6. powershell unable to call function
  7. unable to call javascript function
  8. unable to call the function dllinstall in the file webcheck.dll