From 1418734a41061f30dfc7e1cade7cab5dfaf0d467 Mon Sep 17 00:00:00 2001 From: jarryl Date: Wed, 12 Aug 2020 08:56:57 +0800 Subject: [PATCH] All exercises done except for the last "further" - buy/cancel functionality not built in yet. --- index.html | 4 +- script.js | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 200 insertions(+), 4 deletions(-) diff --git a/index.html b/index.html index 5fa0cb9..d9c7e56 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@

output:

-

+

Where would you like to go, Bali or KL?

- + \ No newline at end of file diff --git a/script.js b/script.js index 5b7b680..795212e 100644 --- a/script.js +++ b/script.js @@ -1,6 +1,202 @@ console.log("hello script js"); +/************************* Finer-grained pricing ***************************** + +var seats = 10; +var seatPrice = 0; +var startPrice = 50; + +var inputHappened = function(currentInput){ + if (seats === 10) { + seats--; + return "The price of your seat is $" + startPrice.toFixed(2); + } else if (seats < 10 && seats >= 6) { + seats--; + seatPrice = 1.03 * startPrice; + return "The price of your seat is $" + seatPrice.toFixed(2) + ". There are " + (seats - 5) + " seat(s) left before the price goes up further."; + } else if (seats < 6 && seats >= 2) { + seats--; + seatPrice = 1.05 * startPrice; + return "The price of your seat is $" + seatPrice.toFixed(2) + ". There are " + (seats - 1) + " seat(s) left before the price goes up further."; + } else if (seats === 1){ + seats--; + return "The price of your seat is $91,000." + } else { + return "Sorry, there are no more available seats."; + } +}; + +*/ + +/************************* Percentage Increase ***************************** + +var seats = 10; +var seatPrice = 50; + var inputHappened = function(currentInput){ - console.log( currentInput ); - return "WOW SOMETHING HAPPENED"; + if (seats === 10) { + seats--; + return "The price of your seat is $" + seatPrice.toFixed(2); + } else if (seats < 10 && seats >= 6) { + seats--; + seatPrice *= 1.03; + return "The price of your seat is $" + seatPrice.toFixed(2) + "."; + } else if (seats < 6 && seats >= 2) { + seats--; + seatPrice *= 1.05; + return "The price of your seat is $" + seatPrice.toFixed(2) + "."; + } else if (seats === 1){ + seats--; + return "The price of your seat is $91,000." + } else { + return "Sorry, there are no more available seats."; + } }; + +*/ + +/************************* Upgraded Fleet ***************************** + + +var seatsEconomy = 15; +var seatsBusiness = 6; +var seatsFirstClass = 4; +var seatPrice = 50; + +var inputHappened = function(currentInput){ + if (currentInput.toLowerCase() === "buy first class"){ + seatsFirstClass--; + if (seatsFirstClass <= 4 && seatsFirstClass >= 1){ + return "The price of your seat is $" + (seatPrice * 1.15).toFixed(2); + } else if (seatsFirstClass === 0) { + var lastSeatPrice = 191000; + return "The price of your seat is $" + lastSeatPrice.toFixed(2) + "."; + } else if (seatsFirstClass < 0) { + return "Sorry, there are no more First Class seats available."; + } + } else if (currentInput.toLowerCase() === "buy business class") { + seatsBusiness--; + if (seatsBusiness <= 6 && seatsBusiness >=4) { + return "The price of your seat is $" + (seatPrice * 1.06).toFixed(2) + "."; + } else if (seatsBusiness < 4 && seatsBusiness > 1 ) { + return "The price of your seat is $" + (seatPrice * 1.1).toFixed(2) + "."; + } else if (seatsBusiness === 1) { + var lastSeatPrice = 91000; + return "The price of your seat is $" + lastSeatPrice.toFixed(2) + "."; + } else if (seatsBusiness < 1) { + return "Sorry, there are no more Business Class seats available."; + } + } else if (currentInput.toLowerCase() === "buy economy class") { + seatsEconomy--; + if (seatsEconomy <= 15 && seatsEconomy >= 8) { + return "The price of your seat is $" + (seatPrice * 1.03).toFixed(2); + } else if (seatsEconomy < 8 && seatsEconomy > 1) { + return "The price of your seat is $" + (seatPrice * 1.05).toFixed(2); + } else if (seatsEconomy === 1) { + var lastSeatPrice = 91000; + return "The price of your seat is $" + lastSeatPrice.toFixed(2) + "."; + } else if (seatsEconomy < 1) { + return "Sorry, there are no more Economy Class seats available."; + } + } else { + return "Your selection is invalid. Please input one of the following: 'buy first class', 'buy business class' or 'buy economy class'."; + } +} + +*/ + +/********************* Fleet of Aircraft ***************************************** +Buy and cancel functionality not implemented yet +*/ + +var state = 0; +var seatsEconomy = 15; +var seatsBusiness = 6; +var seatsFirstClass = 4; +var seatPrice = 50; +var seatsKL = 10; + +var inputHappened = function(currentInput){ + if (state === 0 && currentInput.toLowerCase() === "bali") { + state++; + return "Please choose one of the following: 'buy first class', 'buy business class' or 'buy economy class'." + } else if (state === 0 && currentInput.toLowerCase() === "kl") { + state+=2; + return flightsKL(); + } else if (state === 1 && currentInput.toLowerCase() === "buy first class"){ + return buyFirstClass(); + } else if (state === 1 && currentInput.toLowerCase() === "buy business class"){ + return buyBusiness(); + } else if (state === 1 && currentInput.toLowerCase() === "buy economy class"){ + return buyEconomy(); + } else { + return "Sorry, we do not have any flights there at the moment."; + } +} + + +var buyFirstClass = function(){ + seatsFirstClass--; + if (seatsFirstClass <= 4 && seatsFirstClass >= 1){ + return "The price of your seat is $" + (seatPrice * 1.15).toFixed(2); + } else if (seatsFirstClass === 0) { + var lastSeatPrice = 191000; + return "The price of your seat is $" + lastSeatPrice.toFixed(2) + "."; + } else if (seatsFirstClass < 0) { + return "Sorry, there are no more First Class seats available."; + } +} + + + +var buyBusiness = function() { + seatsBusiness--; + if (seatsBusiness <= 6 && seatsBusiness >=4) { + return "The price of your seat is $" + (seatPrice * 1.06).toFixed(2) + "."; + } else if (seatsBusiness < 4 && seatsBusiness > 1 ) { + return "The price of your seat is $" + (seatPrice * 1.1).toFixed(2) + "."; + } else if (seatsBusiness === 1) { + var lastSeatPrice = 91000; + return "The price of your seat is $" + lastSeatPrice.toFixed(2) + "."; + } else if (seatsBusiness < 1) { + return "Sorry, there are no more Business Class seats available."; + } +} + + +var buyEconomy = function() { + seatsEconomy--; + if (seatsEconomy <= 15 && seatsEconomy >= 8) { + return "The price of your seat is $" + (seatPrice * 1.03).toFixed(2); + } else if (seatsEconomy < 8 && seatsEconomy > 1) { + return "The price of your seat is $" + (seatPrice * 1.05).toFixed(2); + } else if (seatsEconomy === 1) { + var lastSeatPrice = 91000; + return "The price of your seat is $" + lastSeatPrice.toFixed(2) + "."; + } else if (seatsEconomy < 1) { + return "Sorry, there are no more Economy Class seats available."; + } else { + return "Your selection is invalid. Please input one of the following: 'buy first class', 'buy business class' or 'buy economy class'."; + } +} + + +var flightsKL = function(){ + if (seatsKL === 10) { + seatsKL--; + return "The price of your seat is $" + seatPrice.toFixed(2); + } else if (seatsKL < 10 && seatsKL >= 6) { + seatsKL--; + seatPrice *= 1.03; + return "The price of your seat is $" + seatPrice.toFixed(2) + "."; + } else if (seatsKL < 6 && seatsKL >= 2) { + seatsKL--; + seatPrice *= 1.05; + return "The price of your seat is $" + seatPrice.toFixed(2) + "."; + } else if (seatsKL === 1){ + seatsKL--; + return "The price of your seat is $91,000." + } else { + return "Sorry, there are no more available seats."; + } +}; \ No newline at end of file