Solved

Need help with Javascript

Posted on 2016-10-16
4
43 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 57

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

691 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