Tromba
11-11-2006, 06:40 PM
I'm teaching a general computer literacy course and we just started a section on programming in Javascript. The point is just to give the students some idea of what's involved, see how to think this way and that type of thing. The problem is my programming experience was long ago in Quick Basic. I understand all the book, but I need to be able to answer student questions about why something they do doesn't work.
To start with, I'm typing in all the examples in the book and running them -- this becomes something I can show in class. The end product of these several chapters in the text is a program that allows "purchase" of latte, cappuccino, or Americano (var drink) in several different sizes (var ounce) and strengths (var shots). There also a var taxRate and it will calculate the var price. The starting point is just to show the calculation of one drink, so drink, shots, and ounce are initialized.
I've typed the code exactly from the book, spent several hours going over it, had my wife read it back to me letter by letter and the alert (price); NEVER works. I just can't see what's wrong. I put another alert outside of the <script></script> for what's in the book (alert ("something is still wrong")) and that one I CAN always see (so popups aren't blocked and that kind of stuff)
Here's the exact code from the book (http://herrick.huntingdon.edu/js/bean_V0.htm) (except my extra alert...) unless we've completely missed something proofreading!!!
If I take OUT all the if statements, and initialize price, the thing works fine so the calculation part is OK. Here's the for that (http://herrick.huntingdon.edu/js/bean_V0-2.htm). The correct result appears
<this is my first time here, and I thought posting the URL might be better than just posting the code here, but didn't know if I should put the code here also, so I did....>
<script language = "Javascript">
var drink = "latte";
var shots = 2;
var ounce = 12;
var price;
var taxRate = 0.088;
if (drink == "espresso")
price = 1.40;
If (drink == "latte" || drink == "cappuccino"){
if (ounce == 8)
price = 1.95;
if (ounce == 12)
price = 2.35;
if (ounce == 16)
price = 2.75;
}
if (drink == "Americano")
price = 1.20 + .3 * (ounce/8);
price = price + (shots -1) * .50;
price = price + price * taxRate;
alert(price);
</script>
<script language = "Javascript">
alert ("There is still a problem")
</script>
To start with, I'm typing in all the examples in the book and running them -- this becomes something I can show in class. The end product of these several chapters in the text is a program that allows "purchase" of latte, cappuccino, or Americano (var drink) in several different sizes (var ounce) and strengths (var shots). There also a var taxRate and it will calculate the var price. The starting point is just to show the calculation of one drink, so drink, shots, and ounce are initialized.
I've typed the code exactly from the book, spent several hours going over it, had my wife read it back to me letter by letter and the alert (price); NEVER works. I just can't see what's wrong. I put another alert outside of the <script></script> for what's in the book (alert ("something is still wrong")) and that one I CAN always see (so popups aren't blocked and that kind of stuff)
Here's the exact code from the book (http://herrick.huntingdon.edu/js/bean_V0.htm) (except my extra alert...) unless we've completely missed something proofreading!!!
If I take OUT all the if statements, and initialize price, the thing works fine so the calculation part is OK. Here's the for that (http://herrick.huntingdon.edu/js/bean_V0-2.htm). The correct result appears
<this is my first time here, and I thought posting the URL might be better than just posting the code here, but didn't know if I should put the code here also, so I did....>
<script language = "Javascript">
var drink = "latte";
var shots = 2;
var ounce = 12;
var price;
var taxRate = 0.088;
if (drink == "espresso")
price = 1.40;
If (drink == "latte" || drink == "cappuccino"){
if (ounce == 8)
price = 1.95;
if (ounce == 12)
price = 2.35;
if (ounce == 16)
price = 2.75;
}
if (drink == "Americano")
price = 1.20 + .3 * (ounce/8);
price = price + (shots -1) * .50;
price = price + price * taxRate;
alert(price);
</script>
<script language = "Javascript">
alert ("There is still a problem")
</script>