-
Notifications
You must be signed in to change notification settings - Fork 175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Trattoria pizzeria #147
base: main
Are you sure you want to change the base?
Trattoria pizzeria #147
Changes from all commits
0a8f1ba
f8bdcbe
34fa16c
7e57f29
72ccb2e
2a6dbda
5d6e1e1
de8bdbf
68b2643
bcc13e2
6cfea33
5621a77
13eeb75
d3a784e
190c261
62a86ff
25fa631
d6f2952
f9c6e49
f5ed524
9766125
8aa9d68
04a7338
6ce1b34
c3df5a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,37 @@ | ||
# Project Name | ||
# Trattoria Pizzeria | ||
|
||
Replace this readme with your own information about your project. Start by briefly describing the assignment in a sentence or two. Keep it short and to the point. | ||
An interactive JavaScript-based food ordering system where users select their name, food choices, and order details. The program is built using JavaScript's alert() and prompt() methods to interact with the user. Conditional statements and functions were used to handle different choices. | ||
|
||
The project consists of five steps: | ||
|
||
- Welcome and Introduction: Welcoming the user and collecting their name. | ||
|
||
- Food Choice: Letting the user select a food type (Pizza, Pasta, Salad). | ||
|
||
- Subtype Choice: Choosing a subtype based on the previous food choice. | ||
|
||
- Age Confirmation: Confirming whether the food is for an adult or a child. | ||
|
||
- Order Confirmation: Asking the user to confirm their order or decline. | ||
|
||
## The problem | ||
|
||
Describe how you approached to problem, and what tools and techniques you used to solve it. How did you plan? What technologies did you use? If you had more time, what would be next? | ||
The approach to the problem was structured into logical steps, focusing on user interaction through prompts and alerts. Each step progressively built on the previous one, guiding the user through a food ordering process. | ||
|
||
Planning: | ||
|
||
- Background Image: Preloaded to ensure smooth user experience. | ||
|
||
- Input Handling: Designed a reusable getValidSelection() function to validate user choices for food and subtypes. | ||
|
||
- Iterative Process: Built the order process step by step (welcome, food choice, subtype, age, confirmation). | ||
|
||
- Edge Case Handling: Added error handling for invalid inputs (e.g., non-numeric age, incorrect food options). | ||
|
||
Technologies: | ||
|
||
- JavaScript, HTML, CSS | ||
|
||
## View it live | ||
|
||
Have you deployed your project somewhere? Be sure to include the link to the deployed project so that the viewer can click around and see what it's all about | ||
https://trattoria-pizzeria.netlify.app/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,160 @@ | ||
// Start here | ||
// preloads the background img to avoid delays | ||
// URL of the background image | ||
const bgImageUrl = "trattoria.jpg"; | ||
|
||
// Step 1 - Welcome and introduction | ||
// Your code goes here | ||
alert( | ||
`Welcome to our Javascript Pizzeria. Ready to Start? - Click 'OK' to begin.` | ||
) | ||
// Create a new Image object to preload the image | ||
const img = new Image(); | ||
img.src = bgImageUrl; | ||
|
||
// Step 2 - Food choice | ||
// Your code goes here | ||
// Wait for the background image to load | ||
img.onload = () => { | ||
// Set the background image once it has been loaded | ||
document.body.style.backgroundImage = `url(${bgImageUrl})`; | ||
|
||
// Step 3 - Subtype choice | ||
// Your code goes here | ||
// Function to get valid user selection | ||
// Two parameters (promptText: for questions), (options: for valid inputs) | ||
const getValidSelection = (promptText, options) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really like this approach, wish I had thought of it ⭐ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only thing I thought of while trying your bot was that as a user I couldn't cancel during the order process. Clicking "Cancel" just prompted the "Invalid option" alert over and over. Maybe find a way to check if the user has clicked "Cancel" and then cancel the order instead. |
||
// 1. Ask the user for input | ||
const userSelection = prompt(promptText); | ||
// 2. Check if input is valid | ||
if (options.includes(userSelection)) { | ||
// 3. Return input if valid | ||
return userSelection; | ||
} else { | ||
alert("Invalid selection. Please choose a valid option."); | ||
return getValidSelection(promptText, options); // Recursive call, ensures that the user keeps getting prompted until they provide a valid input | ||
} | ||
}; | ||
|
||
// Step 4 - Age | ||
// Your code goes here | ||
// Function to start the order process--------------------------------------- | ||
const startOrder = () => { | ||
// Step 1 - Welcome and introduction | ||
alert( | ||
"Welcome to Trattoria Pizzeria. Ready to Start? - Click 'OK' to begin." | ||
); | ||
|
||
// Step 5 - Order confirmation | ||
// Your code goes here | ||
let userName; | ||
|
||
// Keep prompting the user until a valid name is entered | ||
while (!userName) { | ||
userName = prompt("Please enter your name:"); | ||
if (!userName) { | ||
alert("No name entered. Please enter your name to continue."); | ||
} | ||
} | ||
|
||
alert(`Hi ${userName}, nice to meet you!`); | ||
|
||
// Step 2 - Food choice | ||
const foodOptions = ["1", "2", "3"]; | ||
const foodTypeOptions = { | ||
1: "Pizza", | ||
2: "Pasta", | ||
3: "Salad", | ||
}; | ||
|
||
const userSelection = getValidSelection( | ||
`What type of food would you like to order? | ||
Please select a number: | ||
|
||
1 - Pizza | ||
2 - Pasta | ||
3 - Salad`, | ||
foodOptions | ||
); | ||
|
||
const foodType = foodTypeOptions[userSelection]; | ||
|
||
// Step 3 - Subtype choice | ||
const subtypeOptions = { | ||
Pizza: { | ||
1: "Napolitana", | ||
2: "Hawaiian", | ||
3: "Pepperoni", | ||
}, | ||
Pasta: { | ||
1: "Carbonara", | ||
2: "Vongole", | ||
3: "Lasagna", | ||
}, | ||
Salad: { | ||
1: "Greek Salad", | ||
2: "Insalata Russa", | ||
3: "Giardiniera", | ||
}, | ||
}; | ||
|
||
const subtypePrompt = { | ||
Pizza: `Select a Pizza type | ||
Enter a number: | ||
|
||
1 - Napolitana | ||
2 - Hawaiian | ||
3 - Pepperoni`, | ||
Pasta: `Select a Pasta type | ||
Enter a number: | ||
|
||
1 - Carbonara | ||
2 - Vongole | ||
3 - Lasagna`, | ||
Salad: `Select a Salad type | ||
Enter a number: | ||
|
||
1 - Greek Salad | ||
2 - Insalata Russa | ||
3 - Giardiniera`, | ||
}; | ||
|
||
const subtypeOptionsList = Object.keys(subtypeOptions[foodType]); | ||
|
||
const subtypeSelection = getValidSelection( | ||
subtypePrompt[foodType], | ||
subtypeOptionsList | ||
); | ||
|
||
const subtype = subtypeOptions[foodType][subtypeSelection]; | ||
|
||
// Step 4 - Age | ||
let age; | ||
let cost; | ||
let ageLabel; | ||
|
||
while (true) { | ||
age = parseInt( | ||
//convert a string into an integer | ||
prompt("Is this food for a child or an adult? Type your age:"), | ||
10 | ||
); | ||
//checks if a value is Not a Number (NaN) | ||
if (isNaN(age)) { | ||
alert("Invalid input. Please type a valid number."); | ||
} else { | ||
ageLabel = age >= 18 ? "adult" : "child"; //ternary operator, a shorthand way of writing an if-else | ||
cost = age >= 18 ? "€15" : "€10"; | ||
break; // stops the loop from running further | ||
} | ||
} | ||
|
||
// Step 5 - Confirm order details | ||
const confirmation = confirm( | ||
`You have ordered ${foodType} (${subtype}) for one ${ageLabel}. That will be ${cost}. Click "OK" to confirm or "Cancel" to modify.` | ||
); | ||
|
||
if (confirmation) { | ||
alert("Thank you for your order! Your meal will be prepared."); | ||
} else { | ||
alert("Order cancelled. We hope to see you again!"); | ||
} | ||
}; // closing startOrder function---------------------------------------- | ||
|
||
// Add event listener to start the order process when "here" is clicked | ||
document.getElementById("start-order").addEventListener("click", (event) => { | ||
event.preventDefault(); //the default action is stopped i.e promt | ||
startOrder(); | ||
}); | ||
}; | ||
|
||
// Anonymous function handles errors if the image fails to load | ||
img.onerror = () => { | ||
alert("Failed to load background image."); | ||
}; |
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very smart to preload the background image 👍