Solved

Need help with Javascript

Posted on 2016-10-16
4
39 Views
Last Modified: 2016-11-05
Hi Experts
I need help with Javascript...


function ticket(){
      
var ticketType = prompt("what sort of tickets do you want?");
var ticketQty = parseInt(prompt("how many tickets do you want?"));
var ticketPrice;
switch (ticketPrice){
      case "A":
      case "100":
      break;
      case "B":
      case "75":
      break;
      case "C":
      case "50":
      break;
      case "!A":
      case "!B":
      case "!C":
      document.write("Invalid Ticket Type");
      break;
      case
}
document.write("Ticket type is " + ticketType + "<br>");
document.write("Ticket quantity is " + ticketQty);
}
0
Comment
Question by:Snowbella Hatchiko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 150 total points (awarded by participants)
ID: 41846134
What kind of help do you need?

One obvious error with the code is that your switch is comparing the value of ticketPrice which hasn't been given a value.

Another obvious error is the last line of your switch which is incomplete (see line 3 of the snippet below).
      document.write("Invalid Ticket Type");
      break;
      case
}

Open in new window


What is your objective? What are you trying to do?
0
 
LVL 56

Accepted Solution

by:
Julian Hansen earned 350 total points (awarded by participants)
ID: 41846232
You are using document.write in a function based on user input which is not going to work.

document.write will write to the document on load at the point the script is found.

You probably want something like this
<!doctype html>
<html>
<body>
Tickets <button onclick="ticket()">Click To Buy Tickets</button>
<div id="result"></div>
<script>
var TicketTypes = ['A','B','C'];
function ticket()
{
  var ticketType = prompt("what sort of tickets do you want?");
  var ticketQty = parseInt(prompt("how many tickets do you want?"));
  var valid = false;
  for(var i in TicketTypes) {
    if (ticketType == TicketTypes[i]) {
      valid = true;
      break;
    }
  }
  
  if (valid) {
    document.getElementById('result').innerHTML = 'Ticket Type is: ' + ticketType + '<br/>Ticket Quantity is: ' + ticketQty;
  }
  else {
    document.getElementById('result').innerHTML = 'Invalid Ticket Type';
  }
}
</script>
</body>
</html>

Open in new window

Working sample here
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ModalPopup  question 22 39
Glitching Slide Show 19 28
How do I add counter to html listbox using jquery 2 27
assigning javascript variable to php variable 8 44
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question