[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Need help with Javascript

Posted on 2016-10-16
4
Medium Priority
?
50 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 600 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 59

Accepted Solution

by:
Julian Hansen earned 1400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

650 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