-2

I cannot seem to get my second switch to work, it looks identical to the first one that switch works fine, and everything after the second switch works fine. I cannot get the variable 'discount percentage' to update based of the case. (its just a Uni weekly task) Any ideas?

function ticketQ() {
var ticketType=prompt("What type of tickets do you want?")
var ticketQty=parseInt(prompt("How many tickets do you want?"))
document.write("The ticket type is " +ticketType +"<br>")
document.write("The ticket quantity is " +ticketQty +"<br>")
var ticketPrice=parseInt(0);
switch(ticketType) {
    case (ticketType="A"):
         var ticketPrice=parseInt(100)
         break;
    case (ticketType="B"):
        var ticketPrice=parseInt(75)
        break;
    case (ticketType="C"):
        var ticketPrice=parseInt(50)
        break;
    default:
    document.write("Invalid ticket type" +"<br>");
        break;
}
if (ticketQty >100 || ticketQty <1){
document.write("InvalidQty" +"<br>")
}if (ticketPrice >0 && ticketQty >0 && ticketQty <100){
var grossAmount=parseInt(ticketPrice * ticketQty)
}
var discountPercent=parseInt(0);
switch(grossAmount) {
    case (grossAmount >200):
         var discountPercent=parseInt(50);
         break;
    case (grossAmount >200):
        var discountPercent=parseInt(5);
        break;
//      case (grossAmount ):
//          var discountPercent=parseInt()
 //         break:
 //     case (grossAmount ):
//          var discountPercent=parseInt()
//          break;
    default:
    document.write("wrong" +"<br>")
    break;
}

var discountAmount=parseInt((discountPercent / 100) *grossAmount)
var nettAmount=parseInt(grossAmount - discountAmount)
document.write("Gross Amount: $" + grossAmount + "<br>")
document.write("Discount Percent: " + discountPercent + "%" + "<br>")
document.write("Discount Amount: $" + discountAmount + "<br>")
document.write ("Nett Amount: $" + nettAmount + "<br>")
}
duffy76
  • 9
  • 1
  • `ticketType="A"` doesn’t compare, it _assigns_ `"A"` to `ticketType`. – Sebastian Simon Mar 14 '16 at 13:49
  • 1
    `grossAmount` is a number. `grossAmount >200` is a Boolean. Those values will never be equal to each other. Have a look at the [`switch` documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch) again. The first one is incorrect as well (even though it *seems* to work). – Felix Kling Mar 14 '16 at 13:49
  • 1
    Inside of the case statement, just use case("A"), take out the ticketType. – nurdyguy Mar 14 '16 at 13:51
  • Please read the documentation for the `switch` and `case` statements again. In the first paragraph of the [MDN documentation](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/switch), it shows the syntax of the `case` statement, which is `case value1:`. –  Mar 14 '16 at 13:55
  • In your title you're referring to "parsing an integer", but this has nothing to do with parsing. "Parsing" means "the process of analyzing a string of symbols in computer languages according to a grammar". –  Mar 14 '16 at 14:00
  • See also http://stackoverflow.com/questions/5464362/javascript-using-a-condition-in-switch-case. –  Mar 14 '16 at 14:01

1 Answers1

0

Your switch statements are not right. Here are some that might work

switch(ticketType) {
    case ("A"):
         var ticketPrice=parseInt(100)
         break;
    case ("B"):
        var ticketPrice=parseInt(75)
        break;
    case ("C"):
        var ticketPrice=parseInt(50)
        break;
    default:
    document.write("Invalid ticket type" +"<br>");
        break;
}


switch(true) {
    case (grossAmount >400):
         var discountPercent=parseInt(50);
         break;
    case (grossAmount >200):
        var discountPercent=parseInt(5);
        break;
    default:
    document.write("wrong" +"<br>")
    break;
}

See http://www.ecma-international.org/ecma-262/5.1/#sec-12.11 for the full low-down, or maybe http://www.w3schools.com/js/js_switch.asp for a simpler example

Chris Lear
  • 6,592
  • 1
  • 18
  • 26
  • not too concerned with the ticket type, it displays what it has too (but I will change it) Adding in the (true) worked for the second switch.. cheers Chris – duffy76 Mar 14 '16 at 13:56
  • the `switch (true)` trick is often considered an anti-pattern. –  Mar 14 '16 at 13:57
  • It will display correctly, but only by accident. Part of the reason (I suspect) that you are having a problem with the second case statement is that the first one is very misleading – Chris Lear Mar 14 '16 at 13:58
  • @torazaburo I'm interested in your 'anti-pattern' claim. To me, `switch(true)` isn't a trick. It's just some code. What makes it misleading or problematic? [aside, do you consider it an anti-pattern, or are you just warning me that there might be pattern zealots around that I should watch out for?] – Chris Lear Mar 14 '16 at 14:05
  • Opinions vary widely. Opponents point to the the "true" semantics of `switch`, danger of fall-throughs, bugs related to truthy case values which are not `== true`, etc. Proponents point out the conditions line up and are easier to read. There is a thorough discussion at http://qiita.com/t_uda/items/1969e09a970d71e4cfd6 (in Japanese). Crockford seems against it; jslint will complain. At the end of the day, you are not likely to be struck down by a bolt of lightning if you do this. Personally, I would write `if/then/else` or in some cases some nested ternary conditions. –  Mar 14 '16 at 16:18
  • @torazaburo Thanks. Interesting. I don't think I'm going to learn Japanese just to get the full benefit from that discussion, but I get the general drift of what you're saying. I'm not going to line up on either side of a religious war, but it seems fine to me in practical terms. – Chris Lear Mar 14 '16 at 16:25