Solved

Need help with Javascript

Posted on 2016-10-16
4
26 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 21

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 51

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this Micro Tutorial viewers will learn how to create navigation buttons that change on rollover, using CSS (Continuation of the CSS Image Sprite tutorial) Create a parent ID for all the list items       - Specify position: absolute and display: block…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…

705 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

17 Experts available now in Live!

Get 1:1 Help Now