Solved

Need help with Javascript

Posted on 2016-10-16
4
29 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
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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now