0

this code does not work, at least not complete way it should. I'm trying to run it so an item is selected and then deposit amounts are chosen on the select menu until the proper amount is equaled or exceeded, resulting in change, but I cant seem to get my if statements to work properly. If anyone sees the problem, I would much appreciate the help.

http://jsfiddle.net/YgX4z/

<script type="text/javascript">

var select;

function changedepositedInput(objDropDown) {
//console.log(parseFloat(objDropDown));
var objdeposited = document.getElementById("deposited");
objdeposited.value=parseFloat(objdeposited.value||'0');
var total=parseInt(objdeposited.value||'0');
objdeposited.value = parseFloat(objDropDown.value||'0')+total;

var cost=parseInt('0');
var water = document.getElementById("water");
var soda = document.getElementById("soda");
var coffee = document.getElementById("coffee");
var beer = document.getElementById("beer");

    if (water.checked) {cost=parseInt('75');}
    if (soda.checked) {cost=parseInt('150');}
    if (coffee.checked) {cost=parseInt('100');}
    if (beer.checked) {cost=parseInt('200');}

if (total>cost) {
    var change=document.getElementById("change");
    change.value=total-cost;
    }
    else {
            var change=document.geElementById("change");
            change.value="0";
    }
if (total>=cost) {
    var objdelivered=document.getElementById("delivered");
    objdelivered.value="Yes";
    }
    else {
            var objdelivered=document.getElementById("delivered");
     objdelivered.value="No";
    }
}




 </script>


<h1>Vending Machine Project</h1>
<form name="vendingmachine" action=" ">
Choose Bevrage<br>
    <input name="item" type="radio" id="water" checked="checked">Water 75 cents<br>
    <input name="item" type="radio" id="soda">Soda $1.50<br>
    <input name="item" type="radio" id="coffee">Coffee $1.00<br>
    <input name="item" type="radio" id="beer">Beer $2.00<br>
    <p>
<label>Deposit Money:
    <select name="money" id="money" onchange="changedepositedInput(this)">
        <option value="0">Choose Amount</option>
        <option value="10">10 cents</option>
        <option value="25">25 cents</option>
        <option value="50">50 cents</option>
        <option value="75">75 cents</option>
        <option value="100">$1.00</option>
    </select>
</label>
    </p>
    <p>Total Deposited:<input name="deposited" id="deposited" type="text" readonly="TRUE" value=""></p>
    <p>Change Returned:<input name="change" id="change" type="text" readonly="TRUE" value=" "></p>
    <p>Bevrage Delivered:<input name="delivered" id="delivered" type="text" readonly="TRUE" value=" "></p>
    <p><input type="reset" value="Start Over"></p>

1 Answer 1

4

You have a typo here:

var change=document.geElementById("change");

Should be

var change=document.getElementById("change");

If you test in your fiddle, please don't use function changedepositedInput() as it won't evaluate properly. Instead define your function like this:

changedepositedInput=function(){
};

(Note the semicolon in the end)

Hint

You can use a modern browser's error/javascript console (or developer tools, as its oftentimes called) for debugging.

If you open that console and run your original fiddle, you'd get this error:

[Error] ReferenceError: Can't find variable: changedepositedInput
onchange (_display, line 75)

Once the function definition is changed (as mentioned before in my question), the actual problem shows up:

[Error] TypeError: 'undefined' is not a function (evaluating 'document.geElementById("change")')
changedepositedInput (_display, line 46)
onchange (_display, line 74)

Which points you straight at the typo.

See a working fiddle here

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

3 Comments

Wasn't my eye actually. It was my browser's console :)
You should explain in some detail how you found that error, so OP doesn't go away feeling like they have to ask Stack Overflow about every syntax error, but instead learns how to find those errors independently.
ok, also when the total is added it doesn't work the way I want it to still, though it works for real now. if you check your fiddle link, it states sometimes even though amt deposited is above the cost it still says not delivered and no change. do you have any idea how to fix that?...I certainly don't lol

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.